Write a function newton f fprime x0 tol that implements Newt
Write a function newton (f, fprime, x0, tol) that implements Newton\'s iteration for rootfinding on a scalar function: The first two inputs are handles to functions computing f and f\', and the third input is an initial root estimate. Continue the iteration until either is less than tol.
Solution
Reuired Matlab function is
function root=newton(f,fprime,x0,tol)
if(abs(f(x0))<=tol)
root = x0;
return;
else
while(abs(f(x0))>tol)
xnew=x0-(f(x0)/fprime(x0));
x0=xnew;
end
root = x0;
end
end
Example run:
Smaple Input
f=@(x) cos(x)-3*x+1
fprime=@(x) -sin(x)-3
tol=10^-3
x0=1
newton(f,fprime,x0,tol)
Sample Output:
ans = 0.6071
