# Newton Raphson Method ,Help needed

• Sep 28th 2008, 10:23 AM
mithilesh
Newton Raphson Method ,Help needed
Hi, i have been asked to create a function newton.m which takes as input f(x),f'(x) and root esitmate x0 and throws out the true root of f(x) using the newton raphson method.
after i have created my file,i have to find the root of the 75^(1/3) which is the cuberoot of 75

here is where i have reached thus far:

dont know whats going wrong.

function [root,x] = newton(a,tol)
f(a)=75^(1/3)
fp(a)=0
test = abs(f(a));
i = 0;
x(i+1) = a;
%while test > tol
for i=1:30
a = a - f(a)/fp(a);
test = abs(f(a));
i = i+1;
x(i+2) = a;
end
i

root = a;

can anyone tell me wherei m going wrong,here is what i get when i try to run the file:

>> newton(1,1e-6)

f =

4.2172

fp =

0

??? Attempted to access f(-Inf); index must be a positive integer or logical.

Error in ==> newton at 11
test = abs(f(a));
• Sep 28th 2008, 12:19 PM
CaptainBlack
Quote:

Originally Posted by mithilesh
Hi, i have been asked to create a function newton.m which takes as input f(x),f'(x) and root esitmate x0 and throws out the true root of f(x) using the newton raphson method.
after i have created my file,i have to find the root of the 75^(1/3) which is the cuberoot of 75

here is where i have reached thus far:

dont know whats going wrong.

function [root,x] = newton(a,tol)
f(a)=75^(1/3)
fp(a)=0
test = abs(f(a));
i = 0;
x(i+1) = a;
%while test > tol
for i=1:30
a = a - f(a)/fp(a);
test = abs(f(a));
i = i+1;
x(i+2) = a;
end
i

root = a;

can anyone tell me wherei m going wrong,here is what i get when i try to run the file:

>> newton(1,1e-6)

f =

4.2172

fp =

0

??? Attempted to access f(-Inf); index must be a positive integer or logical.

Error in ==> newton at 11
test = abs(f(a));

First you are looking for the cube root of 75, so we want the root of the equation:

$f(x)=x^3-75$

and

$f'(x)=5x^2$

Then the NR ittereation is:

$x_{new}=x_{old}-\frac{f(x_{old})}{f'(x_{old})}$

so we start with functions defining $f$ and $f'$

Code:

function rv=ffcubert(x)   rv=x^3-75;
Code:

function rv=ffdcubert(x)   rv=3*x^2;
Then the NR itteration:

Code:

function rt=NRroot(f,fdot,x0,tol) %================================================ % f the function we wish to find the root of (function handle) % fdot the derivative of f % x0 initial guess at the root % tol stopping tolerance %================================================   xx=x0;   err=1e6;   while err>tol     fxx=feval(f,xx);     fxxdot=feval(fdot,xx);     xnew=xx-fxx/fxxdot;     err=abs(xnew-xx);     xx=xnew;   end   rt=xx;
Then the call command:

Code:

 >> NRroot(@ffcubert,@ffdcubert,1,0.01) ans= 4.2172 >>
RonL
• Sep 28th 2008, 01:34 PM
mithilesh
Thank you,captain black. and also i had one more request,if u can look at the problem in very urgent section.
i m having a problem plotting the root,
any sort of help will be appreciated

thank you again.