Want to use nested For loop

6 visualizaciones (últimos 30 días)
Ashi Khajotia
Ashi Khajotia el 20 de Abr. de 2023
Comentada: Ashi Khajotia el 20 de Abr. de 2023
Hello,
I wish to change value of k in my code after running the code for all j values and then, I wish to plot for the curves for different k values in a single window. But I am confused in putting the index values.
n0 = 1;
n1 = 2;
n2 = 1;
n3 = 2;
d1 = 0.1;
d2 = 0.2;
lam = 3:0.01:7;
th = 0;
phi1 =(d1./lam).*sqrt((n1));
phi2 = (d2./lam).*sqrt((n2));
z = zeros(size(lam));
for k = 1:1:6
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]^(k))*D3;
z(j) = M(2,1)./M(1,1);
end
end
plot(lam,z);

Respuesta aceptada

chicken vector
chicken vector el 20 de Abr. de 2023
You need to add a second dimension to your z array:
n0 = 1;
n1 = 2;
n2 = 1;
n3 = 2;
d1 = 0.1;
d2 = 0.2;
lam = 3:0.01:7;
th = 0;
phi1 =(d1./lam).*sqrt((n1));
phi2 = (d2./lam).*sqrt((n2));
z = zeros(length(lam),6); % each column corresponds to a different value of k
for k = 1:1:6
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]^(k))*D3;
z(j,k) = M(2,1)./M(1,1); % add column variable 'k' to z
end
end
plot(lam,z);
Now z is a 401x6 array where each column corresponds to a different value of k.
For example, you can access your curve at k=3 as:
k = 3;
curve3 = z(:,k);
Notice that this code will generate 6 identical lines therefore you will see only one, but that depends on how you define z.
When you do M(2,1)./M(1,1) the value of k cannot influence the result because it applies identically to both numerator (M(2,1)) and denominator (M(1,1)).

Más respuestas (2)

Mathieu NOE
Mathieu NOE el 20 de Abr. de 2023
hello
here you are
also you are plotting a complex valued array, so I assume you wanted to take the bas of it first
abs(z) seems to be always equal to 0.5 whatever k and lam (reduced the spacing for faster rendering)
n0 = 1;
n1 = 2;
n2 = 1;
n3 = 2;
d1 = 0.1;
d2 = 0.2;
lam = 3:0.1:7;
th = 0;
phi1 =(d1./lam).*sqrt((n1));
phi2 = (d2./lam).*sqrt((n2));
for k = 1:1:6
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]^(k))*D3;
z(j) = M(2,1)./M(1,1);
end
leg_str{k} = ['k = ' num2str(k)];
plot(lam,abs(z));hold on
end
legend(leg_str);

Walter Roberson
Walter Roberson el 20 de Abr. de 2023
n0 = 1;
n1 = 2;
n2 = 1;
n3 = 2;
d1 = 0.1;
d2 = 0.2;
lam = 3:0.01:7;
th = 0;
phi1 =(d1./lam).*sqrt((n1));
phi2 = (d2./lam).*sqrt((n2));
z = zeros(size(lam));
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);
for k = 1:1:6
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]^(k))*D3;
z(j) = M(2,1)./M(1,1);
end
plot(ax1, lam, real(z), 'DisplayName', "real k = " + k);
hold(ax1, 'on');
plot(ax2, lam, imag(z), 'DisplayName', "imag k = " + k);
hold(ax2, 'on');
end
hold(ax1, 'off');
legend(ax1, 'show');
hold(ax2, 'off');
legend(ax2, 'show')
The reason you only see one line on each graph is that all of the values are the same to within the tolerance of the graph.

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by