Newton-Raphson Method

40 views (last 30 days)
Tony Rankin
Tony Rankin on 15 Mar 2021
Commented: Cris LaPierre on 16 Mar 2021
I have the following Newton-Raphson method code.
Code.
I am attempting to call it using the following
Input.
I am receiving the following error
Error.
Can someone please help me with this? I know the number of iterations and friction factor, but I am expected to turn the loop I did into a function like this and it should work but there is something a little wrong.
  3 Comments
Cris LaPierre
Cris LaPierre on 15 Mar 2021
@Tony Rankin, removing the substance of your question once it has been answered is not the point of the forum. The questions and answers are preserved to provide help to others down the road who have a similar question. Could you be kind enough to restore the details of your question?
Cris LaPierre
Cris LaPierre on 16 Mar 2021
Original context restored below from the cache.
I have the following Newton-Raphson method code.
function [R] = newton(f,df,x0,tol)
% R is an estimation of the root of f using the Newton-Raphson method
% f is colebrook equation for turbulent flow
% df is the first derivative of the colebrook equation
% x0 is the initial estimate for the root
% tol is the accepted tolerance
if abs(f(x0)) < tol
R = x0; % end loop, takes x0 as root of f
else
R = newton(f,df,x0-(f(x0)/df(x0)),tol); % make a recursive call
end
error = abs(f(x0)) % calculate the absolute error
end
I am attempting to call it using the following
error = 1;
p = 0.9*1000;
mu = 8*0.001;
e = 0;
D = 4*0.0254;
Q = (2000*42*3.785*10^-3)/(24*60*60);
A = (pi*(D.^2))/4;
Re = (p*D*Q)/(A*mu);
f = @(x) -1/sqrt(x)-2.*log10(((e./D)./3.7)+2.51/(Re*sqrt(x)));
df = @(x) -1/2*(x).^3/2*(1+((2*2.51)/log10(e./D)./3.7)+(2.51/(Re*sqrt(x)*Re)));
R = newton(f,df,0.01,1e-9)
I am receiving the following error
Out of memory. The likely cause is an infinite recursion within the program.
Error in newton (line 27)
if abs(f(x0)) < tol
Can someone please help me with this? I know the number of iterations and friction factor, but I am expected to turn the loop I did into a function like this and it should work but there is something a little wrong.
Colebrook equation
This is meant to be the derivative of the Colebrook equation. I hope I had input it correctly.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 15 Mar 2021
Edited: Cris LaPierre on 15 Mar 2021
From what I can see, the result of f(x0) keeps cycling between the same set of values, none of which results in your stopping condition tol.
Iter x0 f(x0)
2 -2.1477e+07 13.9626 + 1.3646i
3 -2.1477e+07 - 5.5096e-22i 13.9626 - 1.3646i
4 -2.1477e+07 - 9.4040e-38i 13.9626 - 1.3646i
5 -2.1477e+07 + 5.5096e-22i 13.9626 + 1.3646i
6 -2.1477e+07 - 9.4040e-38i 13.9626 - 1.3646i
...
I would check your equations for f and df.
  1 Comment
Tony Rankin
Tony Rankin on 15 Mar 2021
Edited: Tony Rankin on 15 Mar 2021
f = @(x) -1/sqrt(x)-2.*log10(((e./D)./3.7)+2.51/(Re*sqrt(x)));
df = @(x) 1/(2*x^(3/2)) + 2.51/(Re*x^(3/2)*log(10)*(e/(3.7*D) + 2.51/(Re*x^(1/2))));
I rechecked the equation which as in the OP I thought could be incorrect. It is now - after multiple changes through trial and error - working.
Just posting this here in case anyone will benefit from the derivative of the Colebrook equation.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!