function golden_section_test ( )

%*****************************************************************************80
%
%% golden_section_test() tests golden_section().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    01 March 2026
%
%  Author:
%
%    John Burkardt
%
  addpath ( '../golden_section' );

  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'golden_section_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  golden_section() seeks a minimizer\n' );
  fprintf ( 1, '  of a function f(x) in the interval [a,b],\n' );
  fprintf ( 1, '  assuming f(x) is unimodal over [a,b],\n' );
%
%  humps(x) = 1 / ((x-0.3)^2 + 0.01) + 1 / ((x-0.9)^2 + 0.04) - 6.0
%
  a = 0.3;
  b = 0.9;
  n = 25;
  x_tol = 0.000001;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  f = humps_fun(x)\n' );
  fprintf ( 1, '  f(%g) = %g\n', a, humps_fun ( a ) );
  fprintf ( 1, '  f(%g) = %g\n', b, humps_fun ( b ) );
  fprintf ( 1, '  iteration limit n = %d\n', n );
  fprintf ( 1, '  x_tol = %g\n', x_tol );
  [ a, b, it, nf ] = golden_section ( @humps_fun, a, b, n, x_tol );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Iterations taken: %d\n', it );
  fprintf ( 1, '  Function evaluations: %d\n', nf );
  fprintf ( 1, '  f(%g) = %g\n', a, humps_fun ( a ) );
  fprintf ( 1, '  f(%g) = %g\n', b, humps_fun ( b ) );
%
%  test_fun(x)=x^4+10*x*sin(x^2)
%
  a = -2.0;
  b = 1.0;
  n = 35;
  x_tol = 0.000001;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  f = test_fun(x)\n' );
  fprintf ( 1, '  f(%g) = %g\n', a, test_fun ( a ) );
  fprintf ( 1, '  f(%g) = %g\n', b, test_fun ( b ) );
  fprintf ( 1, '  iteration limit n = %d\n', n );
  fprintf ( 1, '  x_tol = %g\n', x_tol );
  [ a, b, it, nf ] = golden_section ( @test_fun, a, b, n, x_tol );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Iterations taken: %d\n', it );
  fprintf ( 1, '  Function evaluations: %d\n', nf );
  fprintf ( 1, '  f(%g) = %g\n', a, test_fun ( a ) );
  fprintf ( 1, '  f(%g) = %g\n', b, test_fun ( b ) );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'golden_section_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  timestamp ( );

  rmpath ( '../golden_section' );

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% timestamp() prints the current YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  t = now;
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end
