function [x,it] = newton(x,tol)
%%
%% primitive Newton solver
%% 
%% 
error = abs(f(x));

it = 0;
while abs(error) > tol
    fx = f(x);
    dfx = df (x);
    x = x - fx/dfx; 
    it = it + 1;
    error = abs(f(x));
    %% print current information
    format long; [it,x,error], format;
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function v = f(x)
  v = x.^2-1;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function v = df (x)
  v = 2*x;
end

