function [ x, em, ierror, k ] = tphi1u ( x, abserr, kmax, f )

%*****************************************************************************80
%
%% tphi1u() implements the Traub phi - 1,1(u) function.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 May 2024
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Joseph Traub,
%    Iterative Methods for the Solution of Equations,
%    ISBN: 0828403120,
%    LC: QA297.T7.
%    Prentice Hall, 1964, page 235.
%
%  Input:
%
%    real X: an estimate for the root.
%
%    real ABSERR, an error tolerance.
%
%    integer KMAX, the maximum number of iterations allowed.
%
%    real external F, the name of the routine that
%    evaluates the function or its derivatives, of the form
%      function f ( x, ider )
%
%  Output:
%
%    real X:  an approximate root for which abs ( F(X) ) <= ABSERR.
%
%    real EM, an estimate of the multiplicity of the root.
%
%    integer IERROR, error indicator.
%    0, no error occurred.
%    nonzero, an error occurred, and the iteration was halted.
%
%    integer K, the number of steps taken.
%

%
%  Initialization.
%
  ierror = 0;
  k = 0;
  u1 = f ( x, 0 ) / f ( x, 1 );
  x = x - u1;
  fx = f ( x, 0 );
  dfx = f ( x, 1 );

  fu1 = f ( u1, 0 );
%
%  Iteration loop:
%
  while ( true )
%
%  If the error tolerance is satisfied, then exit.
%
    if ( abs ( fx ) <= abserr )
      break
    end

    k = k + 1;

    if ( kmax < k )
      ierror = 2;
      return
    end

    if ( dfx == 0.0 )
      ierror = 3;
      return
    end

    u = fx / dfx;
    fu = f ( u, 0 );

    em = ( u - u1 ) / ( fu - fu1 );
    em = max ( em, 1.0 );
%
%  Set the increment
%
    dx = - em * fx / dfx;
%
%  Remember current data for next step.
%
    u1 = u;
    fu1 = fu;
%
%  Update the iterate and function values.
%
    x = x + dx;
    fx = f ( x, 0 );
    dfx = f ( x, 1 );

  end

  return
end
