function [ a, b, c ] = bracket ( f, x1, x2 )

%*****************************************************************************80
%
%% bracket seeks to bracket a minimizer of a function.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    25 September 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    function handle f: the function for which a minimizer bracket
%    is sought.
%
%    real x1, x2: two distinct points which are the starting point for
%    the search, and which define the spacing to be used.  If
%    f(x1) == f(x2), the points are not suitable.
%
%  Output:
%
%    real a, b, c: three points such that f(a) > f(b) < f(c).
%

%
%  Get initial triplet.
%
  if ( f(x1) == f(x2) )
    fprintf ( 1, 'bracket - f(x1) = f(x2), points are not usable.\n' );
    a = NaN;
    b = NaN;
    c = NaN;
    return
  elseif ( f(x1) > f(x2) )
    a = x1;
    b = x2;
  else
    a = x2;
    b = x1;
  end
  c = b + ( b - a );
%
%  Keep going in the downhill direction, waiting for the first uphill value.
%
  itmax = 100;
  it = 0;
  while ( f(c) < f(b) && it < itmax )
    a = b;
    b = c;
    c = b + ( b - a );
    it = it + 1;
    if ( itmax <= it )
      fprintf ( 1, 'bracket failed, exceeded iteration limit %d\n', itmax );
      return
    end
  end

  return
end

