Why this for loop showing this error?

1 次查看(过去 30 天)
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
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
l1 = -3
Unrecognized function or variable 'I2'.

回答(1 个)

Vilém Frynta
Vilém Frynta 2023-5-2
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 个评论
Walter Roberson
Walter Roberson 2023-5-2
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by