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

 채택된 답변

Andreas Goser
Andreas Goser 2015년 3월 11일

0 개 추천

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개)

Image Analyst
Image Analyst 2015년 3월 11일
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에 대해 자세히 알아보기

태그

질문:

2015년 3월 11일

댓글:

dpb
2015년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by