Iterative Loop for value convergence
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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.)
0 个评论
另请参阅
类别
在 Help Center 和 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!