%%%%% Newton's method

u0=.5; % starting guess
tol=1e-6; %tolerance
stp=100; % stopping n

g=@(u) u; % function g(u)
dg=@(u) 1; % function dg/du (u)

% Newton's method
u1=u0; % next guess (for starting)
g1=g(u1);
nit=0;
while (abs(g1)>tol && nit<stp)
    u0=u1; %replace old guess with newly computed one
    u1=0;%disp(u1);
    g1=g(u1);
    nit=nit+1;
end

if abs(g1)<=tol, disp(['Converged to within tolerance, in ' int2str(nit) ' iterations.']);end
if nit>=stp, disp('Did not converge, reached maximum number of iterations.');end

disp(['Value of |g(u)| is ' num2str(g1)]);
disp(['Value of u is ' num2str(u1)]);
disp(['With tolerance ' num2str(tol) ' and max number of iterations ' int2str(stp)]);
