function [ x, x1, ierror, k ] = dagger_e12 ( x, x1, abserr, kmax, f )

%*****************************************************************************80
%
%% dagger_e12() implements the dagger E 1,2 algorithm.
%
%  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 234.
%
%  Input:
%
%    real X, X1, distinct points that start the method.
%
%    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, X1: X is an approximation to a root of the equation
%    which satisfies abs ( F(X) ) < ABSERR, and X1 is the
%    previous estimate.
%
%    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;
  fx = f ( x, 0 );
  dfx = f ( x, 1 );

  dfx1 = f ( x1, 1 );
%
%  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

    if ( x - x1 == 0.0 )
      ierror = 3;
      return
    end

    d = ( dfx - dfx1 ) / ( x - x1 );
%
%  Set the increment.
%
    dx = - ( fx / dfx ) - 0.5 * ( fx / dfx ) * ( fx / dfx ) * d / dfx;
%
%  Remember current data for next step.
%
    x1 = x;
    dfx1 = dfx;
%
%  Update the iterate and function values.
%
    x = x + dx;
    fx = f ( x, 0 );
    dfx = f ( x, 1 );

  end

  return
end
