function optisection_test ( )

%*****************************************************************************80
%
%% optisection_test() tests optisection().
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    01 May 2026
%
%  Author:
%
%    John Burkardt
%
  addpath ( '../optisection' );
  addpath ( '../test_unimodal' );

  timestamp ( );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'optisection_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Test optisection(), to minimize unimodal functions.\n' );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of test functions is %d\n', p00_test_num ( ) );

  x_tol = 1.0E-05;
  p00_optisection_test ( x_tol );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'optisection_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  
  timestamp ( );

  rmpath ( '../optisection' );
  rmpath ( '../test_unimodal' );

  return
end
function p00_optisection_test ( x_tol )

%*****************************************************************************80
%
%% p00_optisection_test() tests optisection() on many test functions.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    01 May 2026
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real x_tol: execution stops when the interval is smaller than x_tol.
%
  n = 100;
  c = 0.2;

  fprintf ( 1, '\n' );
  fprintf ( 1, 'p00_optisection_test():\n' );
  fprintf ( 1, '  Minimize f(x) in [a,b] using the optisection method of Kodnyanko.\n' );
  fprintf ( 1, '  The function f(x) is assumed to be unimodal.\n' );
  fprintf ( 1, '  Success is determined if the interval is no more than %g\n', x_tol );
  fprintf ( 1, '  Failure is determined if the number of iterations reaches %d\n', n );
  fprintf ( 1, '  The proportionality coefficient C is %g\n', c );
%
%  Get the number of tests.
%
  test_num = p00_test_num ( );

  for test = 1 : test_num

    title = p00_title ( test );

    fprintf ( 1, '\n' );
    fprintf ( 1, '  test %d\n', test );
    fprintf ( 1, '  "%s"\n', title );

    [ a, b ] = p00_interval ( test );
    fa = p00_f ( test, a );
    fb = p00_f ( test, b );
    fprintf ( 1, '  [ A,    B]:  %16.12e                      %16.12e\n', a,   b );
    fprintf ( 1, '  [FA,   FB]:  %16.12e                      %16.12e\n', fa,   fb );

    [ a, b, mx, my, nf ] = optisection ( a, b, c, @(x)p00_f ( test, x ), x_tol, n );
%
%  Print results.
%
    fa = p00_f ( test, a );
    fb = p00_f ( test, b );

    fprintf ( 1, '  Function evaluations: %d\n', nf );
    fprintf ( 1, '  [ A, M, B]:  %16.12e  %16.12e  %16.12e\n', a,  mx, b );
    fprintf ( 1, '  [FA,FM,FB]:  %16.12e  %16.12e  %16.12e\n', fa, my, fb );

  end

  return
end

function timestamp ( )

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

  return
end

