multiply vector of (2x1) to each of the (2x2) matrix in a (2x2x145) double data

5 views (last 30 days)
I want to multiply a vector of (2x1) to each of the (2x2) matrix in a (2x2x145) double data,F. (the result is variable K).
M1 = [2 0;0 2];
M2 = [0 2;1 3];
M3 = [0 1;2 3];
M4 = [0 0;0 4];
F0 = [2 0;0 2];
odefun = @(t,y) part_a(t,y,M1,M2,M3,M4);
tspan = [10 0];
[T,F] = ode45(odefun,tspan,F0(:));
F = reshape(F.',2,2,[]);
K=(1/2)*[0 -2]*F;
Error using *
Arguments must be 2-D, or at least one argument must be scalar. Use TIMES (.*) for elementwise multiplication, use PAGEMTIMES to apply matrix multiplication to the pages of N-D arrays, or use
TENSORPROD to find a product between two N-D arrays.
function dy = part_a(t,y,M1,M2,M3,M4)
F = reshape(y,size(M1));
FA = -M1-M2*F-F*M3+F*M4*F;
dy = FA(:);
end

Accepted Answer

Stephen23
Stephen23 on 10 Nov 2022
Edited: Stephen23 on 10 Nov 2022
"I want to multiply a vector of (2x1) to each of the (2x2) matrix in a (2x2x145) double data,F."
M1 = [2 0;0 2];
M2 = [0 2;1 3];
M3 = [0 1;2 3];
M4 = [0 0;0 4];
F0 = [2 0;0 2];
odefun = @(t,y) part_a(t,y,M1,M2,M3,M4);
tspan = [10 0];
[T,F] = ode45(odefun,tspan,F0(:));
F = reshape(F.',2,2,[]);
K=(1/2)*pagemtimes([0,-2],F)
K =
K(:,:,1) = 0 -2 K(:,:,2) = -0.0001 -2.0000 K(:,:,3) = -0.0001 -2.0000 K(:,:,4) = -0.0002 -1.9999 K(:,:,5) = -0.0002 -1.9999 K(:,:,6) = -0.0005 -1.9998 K(:,:,7) = -0.0007 -1.9998 K(:,:,8) = -0.0010 -1.9997 K(:,:,9) = -0.0012 -1.9996 K(:,:,10) = -0.0025 -1.9992 K(:,:,11) = -0.0037 -1.9988 K(:,:,12) = -0.0050 -1.9984 K(:,:,13) = -0.0062 -1.9979 K(:,:,14) = -0.0124 -1.9959 K(:,:,15) = -0.0186 -1.9939 K(:,:,16) = -0.0248 -1.9919 K(:,:,17) = -0.0309 -1.9900 K(:,:,18) = -0.0611 -1.9808 K(:,:,19) = -0.0904 -1.9723 K(:,:,20) = -0.1190 -1.9646 K(:,:,21) = -0.1469 -1.9575 K(:,:,22) = -0.2626 -1.9331 K(:,:,23) = -0.3673 -1.9182 K(:,:,24) = -0.4625 -1.9106 K(:,:,25) = -0.5495 -1.9083 K(:,:,26) = -0.6483 -1.9103 K(:,:,27) = -0.7367 -1.9167 K(:,:,28) = -0.8160 -1.9261 K(:,:,29) = -0.8869 -1.9371 K(:,:,30) = -0.9620 -1.9508 K(:,:,31) = -1.0272 -1.9648 K(:,:,32) = -1.0834 -1.9787 K(:,:,33) = -1.1316 -1.9916 K(:,:,34) = -1.1806 -2.0053 K(:,:,35) = -1.2208 -2.0174 K(:,:,36) = -1.2535 -2.0279 K(:,:,37) = -1.2799 -2.0368 K(:,:,38) = -1.3060 -2.0456 K(:,:,39) = -1.3255 -2.0525 K(:,:,40) = -1.3400 -2.0580 K(:,:,41) = -1.3507 -2.0622 K(:,:,42) = -1.3608 -2.0660 K(:,:,43) = -1.3672 -2.0686 K(:,:,44) = -1.3708 -2.0704 K(:,:,45) = -1.3727 -2.0715 K(:,:,46) = -1.3739 -2.0721 K(:,:,47) = -1.3743 -2.0724 K(:,:,48) = -1.3742 -2.0726 K(:,:,49) = -1.3738 -2.0727 K(:,:,50) = -1.3730 -2.0724 K(:,:,51) = -1.3722 -2.0722 K(:,:,52) = -1.3715 -2.0724 K(:,:,53) = -1.3708 -2.0724 K(:,:,54) = -1.3698 -2.0718 K(:,:,55) = -1.3691 -2.0715 K(:,:,56) = -1.3691 -2.0720 K(:,:,57) = -1.3690 -2.0722 K(:,:,58) = -1.3679 -2.0712 K(:,:,59) = -1.3674 -2.0709 K(:,:,60) = -1.3678 -2.0717 K(:,:,61) = -1.3681 -2.0723 K(:,:,62) = -1.3669 -2.0710 K(:,:,63) = -1.3664 -2.0705 K(:,:,64) = -1.3671 -2.0716 K(:,:,65) = -1.3676 -2.0723 K(:,:,66) = -1.3666 -2.0712 K(:,:,67) = -1.3662 -2.0707 K(:,:,68) = -1.3667 -2.0715 K(:,:,69) = -1.3671 -2.0720 K(:,:,70) = -1.3664 -2.0712 K(:,:,71) = -1.3661 -2.0709 K(:,:,72) = -1.3664 -2.0714 K(:,:,73) = -1.3667 -2.0717 K(:,:,74) = -1.3662 -2.0711 K(:,:,75) = -1.3660 -2.0709 K(:,:,76) = -1.3663 -2.0713 K(:,:,77) = -1.3665 -2.0716 K(:,:,78) = -1.3659 -2.0709 K(:,:,79) = -1.3657 -2.0706 K(:,:,80) = -1.3662 -2.0714 K(:,:,81) = -1.3666 -2.0718 K(:,:,82) = -1.3657 -2.0707 K(:,:,83) = -1.3654 -2.0703 K(:,:,84) = -1.3663 -2.0714 K(:,:,85) = -1.3668 -2.0721 K(:,:,86) = -1.3658 -2.0709 K(:,:,87) = -1.3655 -2.0704 K(:,:,88) = -1.3662 -2.0714 K(:,:,89) = -1.3667 -2.0721 K(:,:,90) = -1.3660 -2.0712 K(:,:,91) = -1.3657 -2.0708 K(:,:,92) = -1.3662 -2.0713 K(:,:,93) = -1.3665 -2.0717 K(:,:,94) = -1.3660 -2.0711 K(:,:,95) = -1.3658 -2.0709 K(:,:,96) = -1.3661 -2.0713 K(:,:,97) = -1.3663 -2.0716 K(:,:,98) = -1.3659 -2.0710 K(:,:,99) = -1.3657 -2.0707 K(:,:,100) = -1.3661 -2.0713 K(:,:,101) = -1.3664 -2.0717 K(:,:,102) = -1.3657 -2.0707 K(:,:,103) = -1.3654 -2.0704 K(:,:,104) = -1.3662 -2.0714 K(:,:,105) = -1.3667 -2.0720 K(:,:,106) = -1.3657 -2.0707 K(:,:,107) = -1.3653 -2.0703 K(:,:,108) = -1.3662 -2.0714 K(:,:,109) = -1.3668 -2.0722 K(:,:,110) = -1.3659 -2.0711 K(:,:,111) = -1.3656 -2.0706 K(:,:,112) = -1.3662 -2.0714 K(:,:,113) = -1.3666 -2.0719 K(:,:,114) = -1.3660 -2.0712 K(:,:,115) = -1.3658 -2.0709 K(:,:,116) = -1.3661 -2.0713 K(:,:,117) = -1.3664 -2.0716 K(:,:,118) = -1.3659 -2.0710 K(:,:,119) = -1.3658 -2.0708 K(:,:,120) = -1.3661 -2.0713 K(:,:,121) = -1.3663 -2.0716 K(:,:,122) = -1.3658 -2.0708 K(:,:,123) = -1.3656 -2.0706 K(:,:,124) = -1.3662 -2.0713 K(:,:,125) = -1.3665 -2.0718 K(:,:,126) = -1.3656 -2.0706 K(:,:,127) = -1.3653 -2.0702 K(:,:,128) = -1.3662 -2.0714 K(:,:,129) = -1.3668 -2.0722 K(:,:,130) = -1.3658 -2.0709 K(:,:,131) = -1.3654 -2.0704 K(:,:,132) = -1.3662 -2.0714 K(:,:,133) = -1.3667 -2.0721 K(:,:,134) = -1.3660 -2.0712 K(:,:,135) = -1.3657 -2.0708 K(:,:,136) = -1.3662 -2.0713 K(:,:,137) = -1.3664 -2.0717 K(:,:,138) = -1.3660 -2.0711 K(:,:,139) = -1.3658 -2.0709 K(:,:,140) = -1.3661 -2.0713 K(:,:,141) = -1.3663 -2.0716 K(:,:,142) = -1.3663 -2.0715 K(:,:,143) = -1.3662 -2.0714 K(:,:,144) = -1.3662 -2.0714 K(:,:,145) = -1.3661 -2.0713
function dy = part_a(t,y,M1,M2,M3,M4)
F = reshape(y,size(M1));
FA = -M1-M2*F-F*M3+F*M4*F;
dy = FA(:);
end

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by