Convergence of a variable in matlab
21 views (last 30 days)
Show older comments
Kushagra Saxena
on 9 Dec 2014
Commented: Katlyn Rivera
on 24 Feb 2022
I have a problem that requires convergence of a variable delTa (temp difference in annulus of a wellbore) whose initial value is assumed, with a delTa2 value obtained by using the initial guess delTa value, but through a different route.
Start off with an assumed value of delTa, lets say delTa = 0
while true
Gr = (rci-rins)^3*g*rhoa^2*beta*delTa/mua^2; % calc interim var. RHS vars known
hc = 0.049*(Gr*Pr)^0.333*Pr^0.074*ka/(rins*log(rci/rins)); % RHS vars are known
hc = hc*0.3;
Uto = (rto*(1/(rti*ht)+log(rto/rti)/kt+1/hc(i,j)+log(rco/rci)/kc+...
log(rwb/rco)/kcem))^-1; % RHS vars are known
LR = (2*pi/(cp*w))*(rto*Uto*ke/(ke+rto*Uto*TD)); % using some var calc from above
Q = -LR*w*cp*delTa % Calc Q with var calc from above and using delTa value
delTa2 = -Q/(2*pi*rto*hc); % delTa2 calculated from a similar but diff equation
err = delTa2-delTa; % if assumed value of delTa is right, then err = 0
if abs(err) < 0.0001
break;
else
% modify delTa
end
end
delTa2 is dependent on Q. Q depends on LR. LR depends on Uto. Uto depends on hc which depends on Gr. Gr depends on delTa (initial guess). How do I modify my delTa to a new and more accurate value in the if-else block at the end? What technique can I use here?
Please suggest a way to make these values converge. This is my first short at convergence using MATLAB.
Thanks!
1 Comment
Torsten
on 9 Dec 2014
Use MATLAB's fzero to determine delTa as a zero of the function
f(x)=x+Q(x)/(2*pi*rto*hc(x))
where Q and hc are calculated as above.
Best wishes
Torsten.
Accepted Answer
John D'Errico
on 9 Dec 2014
Edited: John D'Errico
on 9 Dec 2014
Ok, I'll admit to being both confused and bemused at your question.
After all, while I have been known to get down on my knees and pray that a specific piece of computer code start working to my satisfaction, my prayers have never worked. Likewise, I have never been able to use blackmail, extortion, threats of physical violence against my computer. (Yeah, like none of you has EVER tried these things?) So the idea of MAKING a recalcitrant algorithm work seems a bit of a stretch.
Seriously, the algorithm is yours to design. It appears as if you are trying to use a fixed point iteration scheme, with a hope and a prayer that it "converges". While it might do so, or might not, there is actual theory that can help to tell us when such a scheme will work. If your only question is IF it has converged, surely testing the difference between delTa and delTa2 seem reasonable. When that difference is sufficiently close to 0, you would arguably be done. Of course, if it has not "converged" so the while loop continues, it seems like a reasonable idea to replace delTa with delTa2. Otherwise, your while loop would never terminate, since it starts with the same value at every iteration. But who am I to question this? :)
Of course, there are optimizers & root finders, already written in MATLAB. Tools like fzero, etc, which are designed to do the grunt work for you, rather than forcing you to write algorithms where you have no idea if they will or will not converge.
It does seem to me a valid question as to why would you not use fzero here? Or even simpler, a plot, where you look at the difference (delTa-delTa2) as a function of delTa. What does it tell you?
More Answers (0)
See Also
Categories
Find more on Historical Contests 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!