Hi I am having trouble coding a MATLAB function to perform N
Hi. I am having trouble coding a MATLAB function to perform Newton\'s Method to find a root that stops once a tolerance is reached. Could you please help me with this? Thanks.
Solution
function [x,iter]=newton(x0,f,fp)
N = 100; eps = 1.e-5; % define max. no. of iterations and eps is error
maxvalue = 10000.0; % define value for divergence
xx1 = x0;
while (N>0)
xn = xx-f(xx1)/fp(xx1);
if abs(f(xn))<eps
x=xn;iter=100-N;
return;
end;
if abs(f(xx1))>maxvalue
disp([\'no of iterations = \',num2str(iter)]);
error(\'Solution got diverges\');
break;
end;
N = N - 1;
xx = xn;
end;
error(\'there is No convergence\');
break;
