Recursive Function - Bisection

I have written code to compute the flow of a fluid in a pipe using the bisection method. This uses a while loop and works. However, I have been provided with code for a recursive function – which although perhaps not as efficient in this case – I would like to test.
I am having problems calling the function though. I have named a .m file with MyBisection and I have the correlation equation, the upper and lower bounds of the interval, and also the tolerance I want to use for the problem.

3 个评论

Original question by Tony Rankin retrieved from Google Cache:
Recursive Function - Bisection
I have written code to compute the flow of a fluid in a pipe using the bisection method. This uses a while loop and works. However, I have been provided with code for a recursive function – which although perhaps not as efficient in this case – I would like to test.
I am having problems calling the function though. I have named a .m file with MyBisection and I have the correlation equation, the upper and lower bounds of the interval, and also the tolerance I want to use for the problem.
It is rude to delete your question when someone has spent a great deal of time to answer your problem. Others can benefit from the solution. But when you remove the question, you remove all context for the answer.
(Answers Dev) Restored edit

请先登录,再进行评论。

回答(1 个)

Your problem is with your function.
E=1;
D=5;
Re=1;
f=@(x)-1./sqrt(x)-2.*log10(((E/D)/3.7)+2.51./(Re*sqrt(x)));%is this the equation? E, D, and Re will have to be provided before the function call
R=MyBiscection(f,0,1,1e-4);

4 个评论

Typo: you have one too many c's.
MyBiscection % should be
MyBisection
I suspect you've modified Bisection_Trial significantly from the MyBisection function you posted originally. Can you post your current version of Bisection_Trial so we can see the whole function not just isolated pieces?
I believe your problem is S=0 and log10(0)=-inf and a cannot equal 0 either.This works just fine.
p = 0.9*1000;
mu = 8*0.001;
E = .001;%change to something small other than zero
D = 0.1016;
S = E/D;
Q = (2000*42*3.785*10^-3)/(24*60*60);
A = (pi*(D.^2))/4;
U = Q/A;
Re = (p*D*U)/mu;
f = @(x) -1./sqrt(x)-2.*log10((S)/3.7)+2.51./(Re*sqrt(x));
R = Bisection_Trial(f,.01,1,1e-4);%change a to something small .01
function R=Bisection_Trial(f,a,b,tol)
if sign(f(a)) == sign(f(b))
error('Scalars a and b do not bound a root.')
end
m = (a+b)/2;
if abs(f(m)) < tol
R = m;
elseif sign(f(a)) == sign(f(m))
R = MyBisection(f,m,b,tol);
elseif sign(f(b)) == sign(f(m))
R = MyBisection(f,a,m,tol);
end
end
First, please don't put "clear all" in your script files. When you need to clear the variables, execute it at the prompt in the MATLAB Command Window.
Second, your code does not handle the case where the absolute value of f(m) is not less than tol and the sign of f(m) is not the same as either the sign of f(a) or the sign of f(b).
sp1 = sign(1) % +1
sp1 = 1
sm1 = sign(-1) % -1
sm1 = -1
sc = sign(3+4i) % complex
sc = 0.6000 + 0.8000i
To detect this I might put some code in your function to display the values of a, m, and b along with f(m) before entering your if / elseif section, something along the lines of this untested code:
fm = f(m);
result = table(a, m, b, fm, 'VariableNames', ["a", "m", "b", "f(m)"]);
disp(result)
Or maybe pass this between the recursive calls and augment it in each one so you can see the progress.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Debugging and Analysis 的更多信息

产品

版本

R2020a

编辑:

Rik
2021-6-1

Community Treasure Hunt

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

Start Hunting!

Translated by