Iterative Loop for value convergence

4 views (last 30 days)
James
James on 8 Apr 2015
Answered: Roger Stafford on 8 Apr 2015
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.

Answers (1)

Roger Stafford
Roger Stafford on 8 Apr 2015
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.)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!