필터 지우기
필터 지우기

Iterative Loop for value convergence

조회 수: 4 (최근 30일)
James
James 2015년 4월 8일
답변: Roger Stafford 2015년 4월 8일
Im attempting to use a while loop to calculate values that need to converge. I have the following code written, excluding constants.
if true
%%Initial Calculations
while abs(Wrd-Wrdnew)>0.00001 && abs(Wfd-Wfdnew)>0.00001
Bnr=(CI*br*dr/Wrd)*((1+5*(deltar/hr))/(1+3*(br/dr)));
Bnf=(CI*bf*df/Wfd)*((1+5*(deltaf/hf))/(1+3*(bf/df)));
a=exp(-0.1*Bnr); c=exp(-0.1*Bnf); b=exp(-7.5*s);
NTr=Wrd*(0.88*(1-a)*(1-b)-(1/Bnr)-((0.5*s)/sqrt(Bnr))) NTf=Wfd*(0.88*(1-c)*(1-b)-(1/Bnr)-((0.5*s)/sqrt(Bnf)))
NT=2*NTf+2*NTr;
DBP=NT
Wfdt=((Wtot*cog)-(DBP*dbh))/WB
Wrdt=Wtot-Wfdt
Wrd=Wrdt/2
Wfd=Wfdt/2
Wrdnew=Wrdt/2;
Wfdnew=Wfdt/2;
end % code end
But after i try to loop the values, I noticed that the loop only loops once since the values have been redefined to be the same. How should I define the values at the end to still allow me to use the value in the loop, but also have the conditions of the loop not be equal after one loop like they are now.

답변 (1개)

Roger Stafford
Roger Stafford 2015년 4월 8일
Three pieces of advice:
1) Put Wrd = Wrdnew and Wfd = Wfdnew at the beginning of your while-loop and remove Wrd = Wrdt/2 and Wfd = Wfdt/2 from the end of the loop.
2) Change the while condition to an OR instead of an AND.
3) Before entering the loop set Wrdnew and Wfdnew to your initial estimates and set Wrd and Wfd in such a way that they are bound to satisfy the while condition to ensure at least one trip through the loop.
The values of Wrdnew and Wfdnew will be your final answers at exit (if convergence is successful.)

카테고리

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