function rectangle_distance_test ( )

%*****************************************************************************80
%
%% rectangle_distance_test() tests rectangle_distance().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 May 2023
%
%  Author:
%
%    John Burkardt
%
  addpath ( '../rectangle_distance' );

  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'rectangle_distance_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Test rectangle_distance().\n' );
%
%  Because of the way we compute the PDF, we sometimes compute 
%  (and ignore) square roots of negative numbers.
%  We need to tell MATLAB not to worry!
%
  warning ( 'off', 'MATLAB:plot:IgnoreImaginaryXYPart' )

  a = 1.0;
  b = 3.0;

  n = 10000;
  [ mu, var ] = rectangle_distance_stats ( n, a, b );
  a = min ( a, b );
  b = max ( a, b );
  d = sqrt ( a^2 + b^2 );
  mu_exact = ( 1.0 / 15.0 ) * ...
    ( ...
        a^3 / b^3 + b^3 / a^3 + d * ( 3.0 - a^2 / b^3 - b^2 / a^3 ) ...
      + 2.5 * ...
      ( ...
          b^2 / a * log ( ( a + d ) / b ) ...
        + a^2 / b * log ( ( b + d ) / a ) ...
      ) ...
    );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Using N = %d sample points,\n', n );
  fprintf ( 1, '  Short rectangle side a =  %g\n', a );
  fprintf ( 1, '  Long rectangle side b =   %g\n', b );
  fprintf ( 1, '  Estimated mean distance = %g\n', mu );
  fprintf ( 1, '  Exact mean distance     = %g\n', mu_exact );
  fprintf ( 1, '  Estimated variance      = %g\n', var );
 
  n = 10000;
  rectangle_distance_histogram ( n, a, b );
  filename = 'rectangle_distance_histogram.png';
  print ( '-dpng', filename );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );

  rectangle_distance_pdf ( a, b );
  filename = 'rectangle_distance_pdf.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );

  clf ( );
  hold ( 'on' );
  n = 10000;
  rectangle_distance_histogram ( n, a, b );
  rectangle_distance_pdf ( a, b );
  hold ( 'off' );
  filename = 'rectangle_distance_compare.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved in "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'rectangle_distance_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  rmpath ( '../rectangle_distance' );

  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

