script wont run seems to be stuck in while loop

I cant find out what is wrong with this script. Can anyone help out?
x=10^(-7);
count=0;
alp=1.24*10^(-10);
dx=10^(-9);
SIG0=0;
SIG1=0;
SIG2=0;
SIG3=0;
SIG4=0;
SIG5=0;
SIG6=0;
SIG7=0;
SIG8=0;
SIG9=0;
for i=1:9
n=i-1;
No=67465.6113/(2^(n/2)*(factorial(n))^(0.5));
while x>(-1.91*10^(-8)*(n+0.5)) && x<(1.91*10^(-8)*(n+0.5))
if n==0
SIG0=SIG0+(No^2)*(10^(-x^2/(2*alp^2)))^2*dx
elseif n==1
SIG1=SIG1+(No^2)*(2*x/alp)^2*(10^(-x^2/(2*alp^2)))^2*dx;
elseif n==2
SIG2=SIG2+(No^2)*(4*(x/alp)^2-2)^2*(10^(-x^2/(2*alp^2)))^2*dx;
count=count+1;
elseif n==3
SIG3=SIG3+(No^2)*(8*(x/alp)^3-12*(x/alp))^2*(10^(-x^2/(2*alp^2)))^2*dx;
elseif n==4
SIG4=SIG4+(No^2)*(16*(x/alp)^4-48*(x/alp)^2+12)^2*(10^(-x^2/(2*alp^2)))^2*dx;
elseif n==5
SIG5=SIG5+(No^2)*(32*(x/alp)^5-160*(x/alp)^3+120*(x/alp))^2*(10^(-x^2/(2*alp^2)))^2*dx;
elseif n==6
SIG6=SIG6+(No^2)*(64*(x/alp)^6-480*(x/alp)^4+720*(x/alp)^2-120)^2*(10^(-x^2/(2*alp^2)))^2*dx;
elseif n==7
SIG7=SIG7+(No^2)*(128*(x/alp)^7-1344*(x/alp)^5+3360*(x/alp)^3-1680*(x/alp))^2*(10^(-x^2/(2*alp^2)))^2*dx;
elseif n==8
SIG8=SIG8+(No^2)*(256*(x/alp)^8-3584*(x/alp)^6+13440*(x/alp)^4-13440*(x/alp)^2+1680)^2*(10^(-x^2/(2*alp^2)))^2*dx;
elseif n==9
SIG9=SIG9+(No^2)*(512*(x/alp)^9-9216*(x/alp)^7+48384*(x/alp)^5-80640*(x/alp)^3+30240*(x/alp))^2*(10^(-x^2/(2*alp^2)))^2*dx;
end
x=x+dx;
end
end
SIG0;
SIG1;
SIG2,
SIG3;
SIG4;
SIG5;
SIG6;
SIG7;
SIG8;
SIG9;
count

回答(1 个)

Well, you know your own code domain and what it is supposed to do better than anyone. It is too detailed for me to just glance at and understand clearly what is intended, but if you say it remains in the loop infinitely then clearly:
x>(-1.91*10^(-8)*(n+0.5)) && x<(1.91*10^(-8)*(n+0.5))
is always evaluating to true.
Now that I look at the body of your while loop neither x nor n is changing in any of the branches of the loop so if the code ends up in the first place due to the above statement evaluating to true then the statement will always evaluate to true so far as I can see.
You are changing things within the while loop that have no impact on the condition against which the while loop is being tested.

2 个评论

Thanks I have managed to run through the script however it does not seem like they g o into the elseif
I have updated the script. Thanks! Thanks for the first help Adam!

请先登录,再进行评论。

类别

帮助中心File 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