function [x,H]=newton(f,df,x0,tol) %NEWTON newton method %X=NEWTON(F,DF,X0,TOL) gives by the Newton method an approximation %X of the zero of the function F with derivative DF. %X0 is an initial guess for the zero. %The approximation X is such that ABS(F(X))<=TOL. % %[X,H]=NEWTON(F,DF,X0,TOL) also gives a two columns matrix H: %The first column has the successive iterates generated by the method %and the second column has the values of F on these iterates. x=x0; fx=f(x); H=[x fx]; while norm(fx)>tol dfx=df(x); x=x-fx/dfx; fx=f(x); H(end+1,:)=[x fx]; end