How to use two different variables in few complex equation in for loop?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
CHUN-HUNG Liu
el 3 de Oct. de 2019
Comentada: CHUN-HUNG Liu
el 6 de Oct. de 2019
I have some problem in MATLAB
Now,I'm solving the problem about the transmission of silver superlens by MATLAB.but when I use for loop and get some trouble. could anyone helpme, thank you
lambda=365e-9
k0=2*pi/lambda
epsilon1=2.193+0.009i
epsilonm=-2.16078+0.273i
epsilon3=2.193+0.009i
for i=10:12
dm=i
for j=0:15*k0
kx=j
d1=10e-9
d3=10e-9
kz1=((epsilon1*k0^2)-kx.^2).^0.5
kz3=((epsilon3*k0^2)-kx.^2).^0.5
kzm=((epsilonm*k0^2)-kx.^2).^0.5
r12=((epsilonm*kz1)-(epsilon1*kzm))./((epsilonm*kz1)+(epsilon1*kzm))
r23=((epsilon3*kzm)-(epsilonm*kz3))./((epsilon3*kzm)+(epsilonm*kz3))
t12=(2*(epsilonm*kz1))./((epsilonm*kz1)+(epsilon1*kzm))
t23=(2*(epsilon3*kzm))./((epsilon3*kzm)+(epsilonm*kz3))
data(i)=(t12.*t23.*exp(i*kzm*dm))./((1+r12.*r23.*exp(i*2*kzm*dm)))
end
end
0 comentarios
Respuesta aceptada
Cris LaPierre
el 3 de Oct. de 2019
The variable data will only contain values for the case when j==15*k0. If you want to store all values, data must be a matrix. Your assigment would have to be something like data(i,j) = ...
Something like this might work. However, the values of data are all NaN. This is because your numbers are extremely large.
lambda=365e-9;
k0=2*pi/lambda;
epsilon1=2.193+0.009i;
epsilonm=-2.16078+0.273i;
epsilon3=2.193+0.009i;
for i=10:12;
dm=i
for j=0:15
kx=j*k0;
d1=10e-9;
d3=10e-9;
kz1=((epsilon1*k0^2)-kx.^2).^0.5;
kz3=((epsilon3*k0^2)-kx.^2).^0.5;
kzm=((epsilonm*k0^2)-kx.^2).^0.5;
r12=((epsilonm*kz1)-(epsilon1*kzm))./((epsilonm*kz1)+(epsilon1*kzm));
r23=((epsilon3*kzm)-(epsilonm*kz3))./((epsilon3*kzm)+(epsilonm*kz3));
t12=(2*(epsilonm*kz1))./((epsilonm*kz1)+(epsilon1*kzm));
t23=(2*(epsilon3*kzm))./((epsilon3*kzm)+(epsilonm*kz3));
data(i-9,j+1)=(t12.*t23.*exp(i*kzm*dm))./((1+r12.*r23.*exp(i*2*kzm*dm)));
end
end
Más respuestas (1)
Cris LaPierre
el 6 de Oct. de 2019
It's i that you changed, so you shouldn't have to do anything with j.
In this case, I'd recommend first creating a variable for your time, and then use the indeces of that variable as your loop counter. For example
var1=1:0.01:10;
for i=1:length(var1)
.
.
.
data(i,j+1)=
end
I would actually recommed doing something similar with j. That way, you can adjust the number ranges you use without having to change downstream code. It makes your code much more robust.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!