필터 지우기
필터 지우기

using for loop with syms

조회 수: 2 (최근 30일)
Ashi Khajotia
Ashi Khajotia 2023년 4월 19일
댓글: Ashi Khajotia 2023년 4월 19일
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
Askic V
Askic V 2023년 4월 19일
편집: Askic V 2023년 4월 19일
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;

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2023년 4월 19일
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
Dyuman Joshi
Dyuman Joshi 2023년 4월 19일
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 2023년 4월 19일
OkkI just realised the answer. Thank you very much :D

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by