matrix multiplication after a for loop

1 view (last 30 days)
hani daniel
hani daniel on 16 Mar 2016
Edited: per isakson on 18 Mar 2016
Dear Help, I have an element of a 2x2 matrix that needs to ne updated in a for loop, i=0, 100. After the iterations, I need to multiply all the sub matrix elements to calculate the resultant matrix. I am not sure how to complete the following code. Thank you. HD.
% series matrix calculation in a for loop
clear all
b=1;
c=5.12;
d=3.04
dz=0.1
G=1.2
L=10
% evlaute matrix elements at increments idz
for i=0,1,100;
idz=i*dz;
Fi11=1+g*cos(idz/L);
Fi12=b;
Fi21=c;
Fi22=d;
Fi=[Fi11,Fi12,Fi21,Fi22];
end
% not sure how to multiply all 100 matrix idz elements to calculate
% final matriz F
% F=F1*F2*......F100
plot (idz,F);

Answers (3)

Christoph
Christoph on 16 Mar 2016
This should do it:
b=1;
c=5.12;
d=3.04
dz=0.1
G=1.2
L=10
F0=[0,b;c,d];
F =[0,b;c,d];
for i=1:100
Fi = F0;
idz=i*dz;
Fi(1,1)=1+G*cos(idz/L);
F = F * Fi;
end

hani daniel
hani daniel on 17 Mar 2016
Edited: per isakson on 18 Mar 2016
Christoph, Thank you. Being new to Matlab, I am still struggling with this problem. Essentially, I simplified the code for clarity and tried to follow your suggestions. The modified program (shown below) can now update the matrix element (A11) in increments of 1. However, I still do not know how to multiply correctly the resulting sequential three matrices. thank you for your help.
clear all
b=2;
c=3;
d=4;
a0=1;
for i=0:1:2;
A=[a0,b;c,d];
a(i+1)=a0+i;
Ai=[a(i+1),b;c,d]
end
A=prod(Ai
  1 Comment
Christoph
Christoph on 18 Mar 2016
You multiply the matrices sequentially within the loop, not after it. Have you tried my code, what didn't work? I suggest you run my code line-by-line in debug mode (just click on the line number left to the first line), it should help you understand how loops work in general and adjust the code to do what you want.
But maybe it's best if you look into some programming fundamentals first. You should really understand that code in general (not just Matlab) works differently than mathematical formulas. a(i+1)=a0+i; inside a loop will actually grow a vector which you didn't even create before. I think Matlab should be able to handle it, but it's extremely poor practice and actually won't even run in most languages.

Sign in to comment.


hani daniel
hani daniel on 17 Mar 2016
Christoph, Thank you. Being new to Matlab, I am still struggling with this problem. Essentially, I simplified the code for clarity and tried to follow your suggestions. The modified program (shown below) can now update the matrix element (A11) in increments of 1. However, I still do not know how to multiply correctly the resulting sequential three matrices. thank you for your help.
clear all
b=2;
c=3;
d=4;
a0=1;
for i=0:1:2;
A=[a0,b;c,d];
a(i+1)=a0+i;
Ai=[a(i+1),b;c,d]
end
A=prod(Ai)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!