function x = NewtonD04(F,JF,x,tol,maxIt,varargin)
% D-dimensional Newton'n method to 
% solve the system F(x) = 0
% F(x)=[F1(x); F2(x); ... ; Fn(x)]
% x = [ x1; x2; ... ; xn]
% JF is the Jacobian matrix of F at x
% x is the initial guess
% tol is the tolerance
% maxIt is the maximum number of iterations
% Usage: x = NewtonD04('fcnEg5p15','fcnJEg5p15',[1;2],1e-6,20,...)
% where ... represents additional parameters for the function and the
% Jacobian matrix (assume the same set of parameters).
% K. Ming Leung, 02/08/04

%disp([0 x']);
iter=1; dif=10;
while (dif > tol & iter <=maxIt)
    dx = -feval(JF,x,varargin{:}) \ feval(F,x,varargin{:});
    dif = norm(dx);
    x = x + dx;
   % disp([ iter x' dif]);
    iter = iter + 1;
end;
if iter > maxIt
    disp('Maximum iteration exceeded without convergence!');
else
    disp('Newton method has converged.');
end
