function root=newton(f,df,x,imax,tol) 
% Input:    x       initial guess at the root 
%           f       function to be evaluated 
%           df      derivative of f with respect to x 
%           imax    iteration maximum 
%           tol     convergence criterion 
% Output:   root    position of the root 

xold=x;
fold=f(xold)
dx=df(x)
dfold=df(xold)
root=x

% CHECK: verify that the derivative is not zero. 
count=0; 
while abs(f(root))>tol && count<imax
    if df(x)==0           
        error('Problem')  
    else
% Newton's algorithm 
dx=-fold*(xold)/dfold*(xold);   
xnew=xold-f(xold)/df(xold);
xold=xnew
root=xnew
     end 
    count=count+1;     
    % calculate equation 4     
    fprintf('Iteration - %i,\t Error - %g\n',count,norm(f(xold))) 

end 
end