# Newton-Raphson Method

조회 수: 30(최근 30일)
Tony Rankin 2021년 3월 15일
댓글: Cris LaPierre 2021년 3월 16일
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표시숨기기 이전 댓글 수: 2
Cris LaPierre 2021년 3월 16일
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.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Cris LaPierre 2021년 3월 15일
편집: Cris LaPierre 2021년 3월 15일
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표시숨기기 없음
Tony Rankin 2021년 3월 15일
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.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by