function [x,F,J] = newton(func,x0);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Prototype: [x,F,J] = newton(func,x0)
%
% Purpose: Just applies Newton's method to the problem F(x)=0.
%
% Input: x0 = initial guess at the minimizer.
% func = string containing the name of a m-file that
% defines the function F(x) and its jacobian J(x),
% with prototype: [F,J] = func(x)
%
% Output: x = solution of F(x)=0
% F,J = F and J at the solution x
%
% Author: M. Holst (implementation follows P. Gill's notes closely)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tol = sqrt(eps); % tolerance for ||F||_2
itmax = 50; % limit on function evaluations
iter = 0;
x = x0;
[F,J] = feval(func,x);
normF = norm(F);
fprintf(' iter ||F||_2\n' )
fprintf(' %3g %14.7e\n', iter, normF);
while ( (normF >= tol) & (iter < itmax) )
p = - J\F;
xnew = x + p;
[F,J] = feval( func,xnew );
normF = norm( F );
x = xnew;
iter = iter + 1;
fprintf(' %3g %14.7e\n', iter, normF);
end