Why this for loop showing this error?
1 view (last 30 days)
Show older comments
clc;close all;clear all
syms r ph phi
lam=532 *10^-9;
k=2*pi/lam;
omega=30;
w0=0.002;
rho=1;p=1;
z=100;
R=0.06;
m1=1
N=3;
for l1=-N:1:N
l1
%to calculate E0
for s1=0:m1
E = 1./(w0^2) + (1i*k)./(2*z);
Con1 = (1i./(2.*lam.*z)).^2 .*exp(-1i.*k.*r.*r./z) .* ((1./(2.*1i.*sqrt(E))).^(2*m1));
E1 = exp((((omega/2)-(r.*cos(ph))).^2)./E).* exp((((omega/2)-(r.*sin(ph))).^2)./E).*hermiteH(s1,1i*(omega./2 - r*cos(ph))./sqrt(E)).*hermiteH(m1-s1,1i*(omega./2 - r*sin(ph))./sqrt(E)) - exp((((-omega/2)-(r.*cos(ph))).^2)./E).* exp((((-omega/2)-(r.*sin(ph))).^2)./E).*hermiteH(s1,-1i*(-omega./2 + r*cos(ph))./sqrt(E)).*hermiteH(m1-s1,-1i*(-omega./2 + r*sin(ph))./sqrt(E));
I1 = Con1.*((1i*p).^(m1-s1)).*nchoosek(m1,s1).*E1;
end
% to calculate E0*
for s2=0:l1
EE = 1./(w0^2) - (1i*k)./(2*z);
Con2 = (-1i./(2.*lam.*z)).^2 .*exp( 1i.*k.*r.*r./z) .* ((-1./(2.*1i.*sqrt(EE))).^(2*l1));
E2 = exp((((omega/2)-(r.*cos(phi))).^2)./EE).* exp((((omega/2)-(r.*sin(phi))).^2)./EE).*hermiteH(s2,1i*(omega./2 - r*cos(phi))./sqrt(EE)).*hermiteH(l1-s2,1i*(omega./2 - r*sin(phi))./sqrt(EE)) - exp((((-omega/2)-(r.*cos(phi))).^2)./EE).* exp((((-omega/2)-(r.*sin(phi))).^2)./EE).*hermiteH(s2,-1i*(-omega./2 + r*cos(phi))./sqrt(EE)).*hermiteH(l1-s2,-1i*(-omega./2 + r*sin(phi))./sqrt(EE));
I2 = Con2.*((1i*p).^(l1-s2)).*nchoosek(l1,s2).*E2;
end
%to calculate CSD
I = I1.*I2;
IQ = I.*exp(1i.*(l1-m1).*(ph-phi)).*exp((-2^(5/6)).*((1 - cos(ph-phi)).^(5/6)).*(r.^(5/3)))./(rho.^(5/3)).*r;
fun = matlabFunction(IQ,'Vars',[ph,phi,r]);
result_1 = abs(integral3(@(ph,phi,r)fun(ph,phi,r),0,2*pi,0,2*pi,0,R))
end
0 Comments
Answers (1)
Vilém Frynta
on 2 May 2023
I have tried your code and it seems like your for loop isn't triggered at all. Which means, none of the calculations happen and none of the variables exist.
This for loop:
for s2=0:l1
EE = 1./(w0^2) - (1i*k)./(2*z);
Con2 = (-1i./(2.*lam.*z)).^2 .*exp( 1i.*k.*r.*r./z) .* ((-1./(2.*1i.*sqrt(EE))).^(2*l1));
E2 = exp((((omega/2)-(r.*cos(phi))).^2)./EE).* exp((((omega/2)-(r.*sin(phi))).^2)./EE).*hermiteH(s2,1i*(omega./2 - r*cos(phi))./sqrt(EE)).*hermiteH(l1-s2,1i*(omega./2 - r*sin(phi))./sqrt(EE)) - exp((((-omega/2)-(r.*cos(phi))).^2)./EE).* exp((((-omega/2)-(r.*sin(phi))).^2)./EE).*hermiteH(s2,-1i*(-omega./2 + r*cos(phi))./sqrt(EE)).*hermiteH(l1-s2,-1i*(-omega./2 + r*sin(phi))./sqrt(EE));
I2 = Con2.*((1i*p).^(l1-s2)).*nchoosek(l1,s2).*E2;
end
Variables EE, E2, I2 do not exist after this for loop. For loop doesn't run.
1 Comment
Walter Roberson
on 2 May 2023
for l1=-N:1:N
so l1 is going to start negative. Then 0:l1 is going to be empty, so as @Vilém Frynta indicates, the for s2 loop body is not going to be executed -- at least not until l1 reaches 0.
See Also
Categories
Find more on Loops and Conditional Statements 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!