MATLAB Answers

How can I multiply matrices using a for loop then extract their elements after the loop?

3 views (last 30 days)
Stephen Porter
Stephen Porter on 28 Nov 2020
Answered: Divija Aleti on 1 Dec 2020
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
Stephen Porter
Stephen Porter on 29 Nov 2020
I appericiate the help. I attached a picture of the matrix I am trying to loop over. Basically, I have an inital value of f that will give me an inital values for the entries in the matrix, hence an inital matrix. After each iteration, I want to update the value of f which will update the value of the matrix. So, I need to multiply the new matrix by the previous one with the stating matrix being that initial matrix.
Structure of matrix:
Formula for f (entire denominator will stay constant after each iteration):
Formula for w:
W(i) = sqrt((Rho(i)*Lambda)/pi);
Formula for Rho:
Rho(i) = (2*B(i)) / sqrt(4 - ((A(i)+D(i))^2));
where A(i), B(i), C(i), D(i) are the values of the entries of the ABCD matrix after each iteration.
So, I will calculate each entrie after each iteration, then calcukate Rho, then w, the f, then a new matrix and so on.

Sign in to comment.

Answers (1)

Divija Aleti
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.

Community Treasure Hunt

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

Start Hunting!

Translated by