function [ x, it ] = newton_system1 ( f, fp, x, xtol, ftol, itmax )

%*****************************************************************************80
%
%% newton_system1 uses Newton's method to solve a system of nonlinear equations.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    22 September 2019
%
%  Input:
%
%    function handle F: a pointer to a function.  This can be
%    an anonymous function or the name of a MATLAB M-file function.
%
%    function handle FP: a pointer to a function that evaluates the
%    jacobian matrix of F.
%    
%    real x(n): a starting point.
%
%    real xtol: the interval size tolerance.  
%    The test ||x-xold|| <= xtol is made.
%
%    real ftol: the residual tolerance.  
%    The test ||f(x)|| <= ftol is made.
%
%    integer itmax: the maximum number of steps allowed.
%    The test it <= itmax is made.
%
%  Output:
%
%    real x(n): the estimated root.
%
%    integer it: the number of steps.
%
  it = 0;

  while ( true )

    it = it + 1;
 
    xold = x;
    dx = - fp ( x ) \ f ( x );
    x = x + dx;

    if ( norm ( dx ) <= xtol && norm ( f ( x ) ) <= ftol )
      return
    end

    if ( itmax <= it )
      return
    end
  end

  return
end

