# Recursive Function - Bisection

11 ビュー (過去 30 日間)
Tony Rankin 2021 年 3 月 1 日

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 件のコメント表示非表示 2 件の古いコメント
Rena Berman 2021 年 5 月 6 日 20:34

サインインしてコメントする。

### 回答 (1 件)

David Hill 2021 年 3 月 1 日
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 件のコメント表示非表示 3 件の古いコメント
Steven Lord 2021 年 3 月 1 日
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.

サインインしてコメントする。

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by