function sol = newton(f,df,x0,tol)
% Newtons method for solution of the equation
% f(x) = 0
% to an accuracy tol<1. The initial guess is input as x0.
% f and its derivative df must be saved as m-files.

maxits=100;

x1 = x0-feval(f,x0)/feval(df,x0);
its = 1;

while (abs(x1-x0)>tol)&(its<maxits)
    x0 = x1;
    x1 = x0-feval(f,x0)/feval(df,x0);
    its=its+1;
end

its
sol=x1;