can anyone tell me why this code doesn't stop running

w=1;
d=200;
B=530;
Ca=300;
Cb=300;
Fh=70;
L=600;
da=100;
db=100;
iteri=0;
iterj=0;
iterk=0;
x=0;
Tk=Ca-Cb;
df=abs(Tk-Fh);
while df>0.8
iterk=iterk+1;
x=x+0.1;
while abs(da)>0.0001
iteri=iteri+1;
Ba=B+x;
Dca=L-Ba-sqrt(d*(d+2*Ca))+Ca*acosh(1+(d/Ca))
Ja=-d/(sqrt(d*(d+2*Ca)))+acosh(1+(d/Ca))-d/(Ca*sqrt(((1+(d/Ca))^2)-1))
da=Dca/Ja;
Ca=Ca-da;
end
while abs(db)>0.0001
iterj=iterj+1;
Bb=B-x;
Dcb=L-Bb-sqrt(d*(d+2*Cb))+Cb*acosh(1+(d/Cb))
Jb=-d/(sqrt(d*(d+2*Cb)))+acosh(1+(d/Cb))-d/(Cb*sqrt(((1+(d/Cb))^2)-1))
db=Dcb/Jb;
Cb=Cb-db;
end
Tk=Ca-Cb;
df=abs(Tk-Fh);
end

 采纳的回答

Well, the answer is that df is never <= 0.8. Actually it becomes stable at 67.9.
But I understand that does not help you. Maybe you can share what type of algorithm this is / should be and scientists and that area could comment?

1 个评论

Looks like doesn't actually compute a convergence value but the end result maybe???

请先登录,再进行评论。

更多回答(1 个)

I'd guess that one of the 3 "while" conditions is not ever false so you're getting an infinite loop. You can find out which one after you look at this link: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ This is why I say time and time again that while loops must always use a failsafe, like a loop counter where you break out if it ever gets bigger than you might possibly expect, like a million or something
loopCounter = 1;
maxCount = 1000000;
while someCondition && loopCounter < maxCount% While loop with failsafe
% Some code you want to run goes here.
% Now at the end of the loop, increment the loop counter.
loopCounter = loopCounter + 1;
end
% Alert user if while loop finished only because of the failsafe.
if loopCounter >= maxCount
message = sprintf('Warning: loop did not finish after % iterations', loopCounter-1);
uiwait(warndlg(message));
end

类别

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