function zero_chandrupatla_test ( )

%*****************************************************************************80
%
%% zero_chandrupatla_test() tests zero_chandrupatla().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    18 March 2024
%
%  Author:
%
%    John Burkardt
%
  addpath ( '../zero_chandrupatla' )

  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'zero_chandrupatla_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  zero_chandrupatla() seeks a root of a function f(x)\n' );
  fprintf ( 1, '  in an interval [a,b].\n' );

  f = @f_01;
  a = 2.0;
  b = 3.0;
  title = 'f_01(x) = x^3 - 2 x - 5';
  zero_chandrupatla_example ( f, a, b, title );

  f = @f_02;
  a = 0.5;
  b = 1.51;
  title = 'f_02(x) = 1 - 1/x^2';
  zero_chandrupatla_example ( f, a, b, title );

  f = @f_03;
  a = 0.0;
  b = 5.0;
  title = 'f_03(x) = ( x - 3 )^3';
  zero_chandrupatla_example ( f, a, b, title );

  f = @f_04;
  a = 0.0;
  b = 5.0;
  title = 'f_04(x) = 6 * ( x - 2 )^5';
  zero_chandrupatla_example ( f, a, b, title );

  f = @f_05;
  a = -1.0;
  b = 4.0;
  title = 'f_05(x) = x^9';
  zero_chandrupatla_example ( f, a, b, title );

  f = @f_06;
  a = -1.0;
  b = 4.0;
  title = 'f_06(x) = x^19';
  zero_chandrupatla_example ( f, a, b, title );

  f = @f_07;
  a = -1.0;
  b = 4.0;
  title = 'f_07(x) = x e^(-1/x2)';
  zero_chandrupatla_example ( f, a, b, title );

  f = @f_08;
  a = 0.0002;
  b = 2.0;
  title = 'f_08(x) = -(3062(1-xi)e^(-x)/(xi+(1-xi)e^(-x)) - 1013 + 1628/x';
  zero_chandrupatla_example ( f, a, b, title );

  f = @f_09;
  a = 0.0002;
  b = 1.0;
  title = 'f_09(x) = e^x - 2 - 0.01/x^2 + 0.000002/x^3';
  zero_chandrupatla_example ( f, a, b, title );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'zero_chandrupatla_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  rmpath ( '../zero_chandrupatla' )

  return
end
function zero_chandrupatla_example ( f, a, b, title )

%*****************************************************************************80
%
%% zero_chandrupatla_example() tests zero_chandrupatla() on a test function.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    18 March 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    function f ( x ): the user-supplied function.
%
%    real a, b: the endpoints of the change of sign interval.
%
%    string title: a title for the problem.
%
  [ z, fz, calls ] = zero_chandrupatla ( f, a, b );

  fz = f ( z );
  fa = f ( a );
  fb = f ( b );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  %s\n', title );
  fprintf ( 1, '\n' );
  fprintf ( 1, '      A                 Z             B\n' );
  fprintf ( 1, '    F(A)              F(Z)          F(B)\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  %14f  %14f  %14f\n', a,  z,  b );
  fprintf ( 1, '  %14e  %14e  %14e\n', fa, fz, fb );
  fprintf ( 1, '  Number of calls to F = %d\n', calls );

  return
end
function fx = f_01 ( x )

  fx = x^3 - 2.0 * x - 5.0;

  return
end
function fx = f_02 ( x )

  fx = 1.0 - 1.0 / x^2;

  return
end
function fx = f_03 ( x )

  fx = ( x - 3.0 )^3;

  return
end
function fx = f_04 ( x )

  fx = 6.0 * ( x - 2.0 )^5;

  return
end
function fx = f_05 ( x )

  fx = x^9;

  return
end
function fx = f_06 ( x )

  fx = x^19;

  return
end
function fx = f_07 ( x )

  if ( abs ( x ) < 3.8E-04 )
    fx = 0.0;
  else
    fx = x * exp ( - ( 1.0 / x^2 ) );
  end

  return
end
function fx = f_08 ( x )

  xi = 0.61489;
  top = ( 3062.0 * ( 1.0 - xi ) * exp ( - x ) );
  bot = ( xi + ( 1.0 - xi ) * exp ( - x ) );

  fx = - top / bot - 1013.0 + 1628.0 / x;

  return
end
function fx = f_09 ( x )

  fx = exp ( x ) - 2.0 - 0.01 / x^2 + 0.000002 / x^3;

  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

