%
% Find roots if f(x) = 0 using the Newton-Raphson Method
%
function [x0,err,it,y,H] = newton(f,df,x0,dx,dy,maxit)

%Input  
%       f      - the object function input as a string 'f'
%       df     - the derivative of f input as a string 'df'
%       x0     - the initial approximation to a zero of f
%       dx     - the tolerance for x0
%       dy     - the tolerance for the function values y
%       maxit  - the maximum number of iterations
%Output x0     - the Newton-Raphson approximation to the zero
%       err    - the error estimate for x0
%       itr    - the number of iterations
%       y      - the function value f(x0)
%       H      - the History vector of the iterations

 H = [x0 0 0];
 for it = 1:maxit
    x1 = x0-feval(f,x0)/feval(df,x0);
    err = abs(x1-x0);  
    rerr = 2*err/(abs(x1)+dx);
    x0 = x1;
    y = feval(f,x0);
    H = [H; x0 err y];
    if (err<dx) || (rerr<dx) || (abs(y)<dy),break,end
end