How to use two different variables in few complex equation in for loop?
Show older comments
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
More Answers (1)
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.
1 Comment
CHUN-HUNG Liu
on 6 Oct 2019
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!