function [res,iter,y]=fnewton(func,dfunc,x0,tol) % func is the name of an m-file (in single quotes) which is the function % for which we are trying to find a zero. % dfunc is the name of a m-file which contains the derivative of func. % x0 is the initial geuss at a root. % tol is the tolerance to compute x within. % res is the result, the approximation to the root % iter is the number of iterations taken iter=0; x=x0; % y(1)=x; d=feval(func,x)/feval(dfunc,x); while abs(d)>tol iter=iter+1; x=x-d; % y(iter+1)=x; d=feval(func,x)/feval(dfunc,x); end res=x;