function [sol,fval] = newtonalgorithm0(fun,dfun,x0,tolerance) if nargin < 3 disp('Newton algorithm requires at least three input arguments.'); return; end if nargin < 4 tolerance = 1e-6; end f0 = feval(fun,x0); if abs(f0) < tolerance sol = x0; fval = f0; k = 0; return; end k = 0; error=1; x1=x0+1; while error > tolerance f0 = feval(fun,x0); df0 = feval(dfun,x0); x1 = x0 - f0/df0; error = abs(x1 - x0); x0 = x1; k = k+1 end sol = x1; fval = feval(fun,sol); %iters = k;