function normal_ode_test ( n1 )

%*****************************************************************************80
%
%% normal_ode_test tests normal_ode().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 October 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N1: the number of steps to take.
%
  addpath ( '../normal_ode' );

  if ( nargin < 1 )
    n1 = 100;
  end

  timestamp ( ); 
  fprintf ( 1, '\n' );
  fprintf ( 1, 'normal_ode_test:\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  normal_ode() defines an ODE for the normal PDF.\n' );
  fprintf ( 1, '  Solve this ODE using the Euler method.\n' );
  fprintf ( 1, '  Estimate ODE approximation error by taking twice as many steps.\n' );

  dydt = @ normal_deriv;
  t0 = -5.0;
  tstop = +5.0;
  tspan = [ t0, tstop ];
  y0 = normal_solution ( t0 );

  [ t1, y1 ] = euler ( dydt, tspan, y0, n1 );

  n2 = 2 * n1;
  [ t2, y2 ] = euler ( dydt, tspan, y0, n2 );

  y = normal_solution ( t1 );

  e =  y1(1:n1+1) - y(1:n1+1);
  e2 = y1(1:n1+1) - y2(1:2:n2+1);

  e_norm = rms ( e );
  e2_norm = rms ( e2 );

  figure ( 1 );
  tplot = linspace ( t0, tstop, 101 );
  yplot = normal_solution ( tplot );
  plot ( t1, y1, 'ro', t2, y2, 'b+', tplot, yplot, 'k-', 'linewidth', 3 );
  grid ( 'on' );
  s = sprintf ( 'Compare normal ODE solutions using %d and %d steps', n1, n2 );
  title ( s );
  legend ( 'N steps', '2N steps', 'exact', 'location', 'northwest' );
  filename = sprintf ( 'normal_ode_test.png' );
  print ( '-dpng', filename );

  figure ( 2 );
  plot ( [t0,tstop], [0.0,0.0], 'k--', t1, e2, 'r-', t1, e, 'k-', 'linewidth', 3 );
  grid ( 'on' );
  s = sprintf ( 'normal rk1 estimated error using %d and %d steps', n1, n2 );
  title ( s );
  legend ( 'Zero error', 'Estimated error', 'Exact error', 'location', 'northwest' );
  filename = sprintf ( 'normal_ode_error_test.png' );
  print ( '-dpng', filename );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Solution used %d and %d steps\n', n1, n2 );
  fprintf ( 1, '  Estimated RMS error = %g\n', e2_norm );
  fprintf ( 1, '  Exact     RMS error = %g\n', e_norm );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  At the point t         = %g,\n', tstop );
  fprintf ( 1, '  Y estimate             = %g\n', y1(n1+1) );
  fprintf ( 1, '  Y double step estimate = %g\n', y2(n2+1) );
  fprintf ( 1, '  Y exact value          = %g\n', y(n1+1) );
  fprintf ( 1, '  Y error estimate       = %g\n', y2(n2+1) - y1(n1+1) );
  fprintf ( 1, '  Y exact error          = %g\n', y(n1+1)  - y1(n1+1) );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'normal_ode_test:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  timestamp ( ); 

  rmpath ( '../normal_ode' );

  return
end

