How to use two different variables in few complex equation in for loop?

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Cris LaPierre
Cris LaPierre le 3 Oct 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
  1 commentaire
CHUN-HUNG Liu
CHUN-HUNG Liu le 6 Oct 2019
Thank you for your reply.
And I have another question
If I want to record the data to get More details.
Something (first loop)like this :
i=1:0.01:10
.
.
.
then,data(i-9,j+1) will be rewrite --->
I change ''j+1'' into ''(j*10)-9'' => data(i-9,(j*10)-9)=...
finally,it say something woring:
Index in position 2 is
invalid. Array indices
must be positive integers
or logical values.
How can I use code to discribe this?
thanks for your helping.

Connectez-vous pour commenter.

Plus de réponses (1)

Cris LaPierre
Cris LaPierre le 6 Oct 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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by