Clear Filters
Clear Filters

using for loop with syms

2 views (last 30 days)
Ashi Khajotia
Ashi Khajotia on 19 Apr 2023
Commented: Ashi Khajotia on 19 Apr 2023
Hello,
I would like to use for loop with syms,
So there a variable lam with which phi1 and phi2 will change and so do matrix M, now I want to use different M elements of each iteration in z. Can anyone help me in using For loop here?
syms n0 n1 n2 ns phi1 phi2
n0 = 1;
n1 = 2;
n2 = 1;
n3 = 2;
d1 = 0.1;
d2 = 0.2;
lam = 3:1:7;
th = 0;
phi1 =(d1./lam).*sqrt((n1));
phi2 = (d2./lam).*sqrt((n2));
for j = 1:length(lam)
D0 = [2 2; n0 -n0];
D1 = [2 2;n1 -n1];
D2 = [2 2; n2 -n2];
D3 = [2 2;n3 -n3];
P1 = [exp(-1i.*phi1) 1; 1 exp(1i.*phi1)];
P2 = [exp(-1i*phi2) 1; 1 exp(1i*phi2)];
M = D0*([D1*P1*D2*P2]^(2))*D3;
Msubs = subs(M);
Mvalue = double(Msubs);
z = M(2,2)./M(1,1);
end
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
  1 Comment
Askic V
Askic V on 19 Apr 2023
Edited: Askic V on 19 Apr 2023
Hi Ashi,
first of all this line will produce an error:
% M = D0*([D1*P1*D2*P2]^(2))*D3;
Executing the whole script:
syms n0 n1 n2 ns phi1 phi2
n0 = 1;
n1 = 2;
n2 = 1;
n3 = 2;
d1 = 0.1;
d2 = 0.2;
lam = 3:1:7;
th = 0;
phi1 =(d1./lam).*sqrt((n1));
phi2 = (d2./lam).*sqrt((n2));
for j = 1:length(lam)
D0 = [2 2; n0 -n0];
D1 = [2 2;n1 -n1];
D2 = [2 2; n2 -n2];
D3 = [2 2;n3 -n3];
P1 = [exp(-1i.*phi1) 1; 1 exp(1i.*phi1)];
P2 = [exp(-1i*phi2) 1; 1 exp(1i*phi2)];
M = D0*([D1*P1*D2*P2]^(2))*D3;
Msubs = subs(M);
Mvalue = double(Msubs);
z = M(2,2)./M(1,1);
end
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
This is because D1*P1 is a matrix with dimensions 2x6, and D2 is a matrix of dimensions 2x2.
How Matrix M shuld look like? What is your intention here?
Just to be able to execute the line properly, you can do something like this:
M = D0*([D1*P1*(D2*P2)']^(2))*D3;

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 19 Apr 2023
syms n0 n1 n2 ns phi1 phi2
n0 = 1;
n1 = 2;
n2 = 1;
n3 = 2;
d1 = 0.1;
d2 = 0.2;
lam = 3:1:7;
th = 0;
phi1 =(d1./lam).*sqrt((n1));
phi2 = (d2./lam).*sqrt((n2));
for j = 1:length(lam)
D0 = [2 2; n0 -n0];
D1 = [2 2;n1 -n1];
D2 = [2 2; n2 -n2];
D3 = [2 2;n3 -n3];
P1 = [exp(-1i.*phi1(j)) 1; 1 exp(1i.*phi1(j))];
P2 = [exp(-1i*phi2(j)) 1; 1 exp(1i*phi2(j))];
M = D0*([D1*P1*D2*P2]^(2))*D3;
Msubs = subs(M);
Mvalue = double(Msubs);
z(j,1) = M(2,2)./M(1,1);
end
plot(lam, real(z), lam, imag(z));
legend({'real', 'imaginary'})
  2 Comments
Dyuman Joshi
Dyuman Joshi on 19 Apr 2023
I'm not sure why OP needs to use symbolic variables here
%syms n0 n1 n2 ns phi1 phi2
n0 = 1;
n1 = 2;
n2 = 1;
n3 = 2;
d1 = 0.1;
d2 = 0.2;
lam = 3:1:7;
th = 0;
phi1 =(d1./lam).*sqrt((n1));
phi2 = (d2./lam).*sqrt((n2));
z = zeros(size(lam));
for j = 1:numel(lam)
D0 = [2 2; n0 -n0];
D1 = [2 2;n1 -n1];
D2 = [2 2; n2 -n2];
D3 = [2 2;n3 -n3];
P1 = [exp(-1i.*phi1(j)) 1; 1 exp(1i.*phi1(j))];
P2 = [exp(-1i*phi2(j)) 1; 1 exp(1i*phi2(j))];
M = D0*([D1*P1*D2*P2]^(2))*D3;
z(j) = M(2,2)./M(1,1);
end
plot(lam, real(z), lam, imag(z));
legend({'real', 'imaginary'})
Ashi Khajotia
Ashi Khajotia on 19 Apr 2023
OkkI just realised the answer. Thank you very much :D

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox 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!