How can I multiply matrices using a for loop then extract their elements after the loop?
1 view (last 30 days)
Show older comments
I have calculated a 2x2 matrix, but need to multiply the matrix by itself so many times using a for loop. Then, I want to extract the elements of the matrix following each iteration, use it to calculate something else, and plot the results. I cannot seem to get the values to update the way i want. This is the code I used:
f = 2.88;
ABCD =@(f) [2.44,0.288;(-7.2-(2.44/f)),(-0.44-(0.288/f))]; %ABCD Matrix
M_ABCD = ABCD(f)
A0 = M_ABCD(1,1)
B0 = M_ABCD(1,2)
C0 = M_ABCD(2,1)
D0 = M_ABCD(2,2)
R0 = (2*B0) / (A0 - D0)
Rho0 = (2*B0) / sqrt(4 - ((A0+D0)^2))
Lambda = 0.0001;
W0 = sqrt((Rho0*Lambda)/pi)
q_inv0 = (1/R0) +((j*Lambda)/(pi*(W0^2)));
q0 = 1/q_inv0
%det(M_ABCD)
for i=1:500
M = M_ABCD^(i);
A(i) = M(1,1);
B(i) = M(1,2);
C(i) = M(2,1);
D(i) = M(2,2);
R(i) = (2*B(i)) / (A(i) - D(i));
Rho(i) = (2*B(i)) / sqrt(4 - ((A(i)+D(i))^2));
W(i) = sqrt((Rho(i)*Lambda)/pi);
q_inv(i) = (1/R(i)) +((j*Lambda)/(pi*(W(i)^2)));
q(i) = 1/q_inv(i);
%plot(real(W))
plot(1./R)
end
4 Comments
Answers (1)
Divija Aleti
on 1 Dec 2020
Hi Stephen,
I understand that you want to use the elements of the matrix obtained after each iteration to calculate other values and plot the results.
Have a look at the following code, which uses the initial 'f' value to get the initial matrix, extracts its elements, updates the 'f' value in each iteration to get a new matrix and multiplies the new matrix with the previous one and so on.
ABCD =@(f) [2.44,0.288;(-7.2-(2.44/f)),(-0.44-(0.288/f))]; %ABCD Matrix
Lambda = 0.0001;
I0 = 2.43;
n2 = 10.5*10^(-16);
%det(M_ABCD)
f(1) = 2.88;
M = eye(2);
for i=1:500
M_ABCD = ABCD(f(i));
M = M*M_ABCD;
A = M(1,1);
B = M(1,2);
C = M(2,1);
D = M(2,2);
R(i) = (2*B) / (A - D);
Rho(i) = (2*B) / sqrt(4 - ((A+D)^2));
W(i) = sqrt((Rho(i)*Lambda)/pi);
q_inv(i) = (1/R(i)) +((j*Lambda)/(pi*(W(i)^2)));
q(i) = 1/q_inv(i);
f(i+1) = (W(i)^2)/(4*n2*(10^-2)*I0);
stable_check(i) = abs((A+D)/2);
end
figure(1)
subplot(3,1,1)
plot(real(W))
ylabel('W')
subplot(3,1,2)
plot(1./R)
xlabel('Round Trips')
ylabel('R')
subplot(3,1,3)
plot(Rho)
xlabel('Round Trips')
ylabel('Rho')
I hope this helps you get the required output.
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!