matrix multiplication after a for loop
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (3개)
Christoph
2016년 3월 16일
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
hani daniel
2016년 3월 17일
편집: per isakson
2016년 3월 18일
댓글 수: 1
Christoph
2016년 3월 18일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!