matrix multiplication after a for loop
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (3)
Christoph
le 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 commentaires
hani daniel
le 17 Mar 2016
Modifié(e) : per isakson
le 18 Mar 2016
1 commentaire
Christoph
le 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.
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!