Newton-Raphson Method
Show older comments
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
Tony Rankin
on 15 Mar 2021
Edited: Tony Rankin
on 15 Mar 2021
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
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.
Accepted Answer
More Answers (0)
Categories
Find more on Newton-Raphson Method 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!