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

1 view (last 30 days)
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

Accepted Answer

Cris LaPierre
Cris LaPierre on 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 Comment
CHUN-HUNG Liu
CHUN-HUNG Liu on 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.

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!