# Why this for loop showing this error?

조회 수: 1 (최근 30일)
Athira T Das 2023년 5월 2일
댓글: Walter Roberson 2023년 5월 2일
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 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이전 댓글 -1개 표시이전 댓글 -1개 숨기기
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