matrix multiplication after a for loop
1 view (last 30 days)
Show older comments
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);
0 Comments
Answers (3)
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
0 Comments
hani daniel
on 17 Mar 2016
Edited: per isakson
on 18 Mar 2016
1 Comment
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.
See Also
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!