Please refer to this code.
function main x0= 1; % initial value eps= 1e-6; % accuracy nmax= 50; % maximum iteration x= newton(@(x) func(x), @(x) dfunc(x), x0, eps, nmax) end
function x= newton(fun, dfun, x0, eps, nmax) for i= 1: nmax x1= x0-fun(x0)/dfun(x0); if abs(x1-x0) <= eps x= x1; return; end x0= x1; end disp('Completed unsuccessfully'); x= x1; end
function f= func(x) f= sin(x)-x; end function f= dfunc(x) f= cos(x)-1; end