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:

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

and

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

Then the NR ittereation is:

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

so we start with functions defining $\displaystyle f$ and $\displaystyle 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