function [ xn, xp, it ] = regula_falsi ( f, xn, xp, xtol, ftol, itmax )

%*****************************************************************************80
%
%% regula_falsi carries out the regula falsi method, and computes alpha.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    10 September 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    function handle F: a pointer to a function.  This can be
%    an anonymous function or the name of a MATLAB M-file function.
%
%    real xn, xp: f(xn) < 0, f(xp) > 0.
%
%    real xtol: the interval size tolerance.  
%    The test |xn-xp| <= 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 xn, xp: real xn, xp: a smaller interval, 
%    for which f(xn) < 0, f(xp) > 0.
%
%    integer it: the number of steps.
%
  it = 0;

  while ( true )

    it = it + 1;

    x = ( xn * f(xp) - xp * f(xn) ) / ( f(xp) - f(xn) );

    old = abs ( xp - xn );

    if ( f ( x ) < 0.0 )
      xn = x;
    else
      xp = x;
    end

    new = abs ( xp - xn );
    alpha = new / old;
    fprintf ( 1, 'Step %d: alpha = %g\n', it, alpha );
%
%  Satisfied?
%
    if ( ...
      ( new <= xtol ) && ...
      ( abs ( f ( x ) ) <= ftol ) ...
    )
      return
    end
%
%  Dissatisfied?
%
    if ( itmax <= it )
      return
    end
%
%  Repeat.
%
  end

  return
end
