function out=NewtonMethod(F,x0,numits,tol) % Inputs to Newton's Method: % F- Name of a function that returns BOTH % values of g(x) and g'(x) % x0= Initial value of x. % numits= Maximum number of iterations % tol = Tolerance (like 1e-6) measures % change between iterates of x % Example: We require two outputs, so we need an m-file % function [y,dy]=MyFunc(x) % y=x^3+x-1; % dy=3*x^2+1; % end % Then: yout=NewtonMethod(@MyFunc,0,100,5e-5); % Output: Newton used 5 iterations % yout=0.6825 for k=1:numits [g,gprime]=F(x0); if gprime==0 error('Derivative is zero'); end xnew=x0-g/gprime; d=abs(xnew-x0); if d