function expsin_rk1_extrapolate ( n1 )

%*****************************************************************************80
%
%% expsin_rk1_extrapolate estimates error by extrapolation.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    28 September 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N1: the number of steps to take.
%    
  fprintf ( 1, '\n' );
  fprintf ( 1, 'expsin_rk1_extrapolate:\n' );
  fprintf ( 1, '  Estimate ODE approximation error by taking twice as many steps.\n' );
  fprintf ( 1, '  Then use extrapolation to improve the estimate.\n' );

  if ( nargin < 1 )
    n1 = 20;
  end

  dydt = @ expsin_deriv;
  t0 = 0.0;
  tstop = 10.0;
  tspan = [ t0, tstop ];
  y0 = 1.0;

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

  n2 = 2 * n1;
  [ t2, y2 ] = rk1 ( dydt, tspan, y0, n2 );
%
%  Use extrapolation to compute a more accurate estimate y3.
%
  y3 = 2.0 * y2(1:2:n2+1) - y1(1:n1+1);

  y = expsin_solution ( t1 );
%
%  Evaluate the errors at the t1 values.
%
  e =   y(1:n1+1)   - y1(1:n1+1);
  e2 = y2(1:2:n2+1) - y1(1:n1+1);
  e3 = y3(1:n1+1)   - y1(1:n1+1);

  e_norm  = rms ( e );
  e2_norm = rms ( e2 );
  e3_norm = rms ( e3 );
%
%  Display the solution estimates.
%
  tplot = linspace ( t0, tstop, 101 );
  yplot = expsin_solution ( tplot );
  plot ( t1, y1, 'ro', t2, y2, 'b+', t1, y3, 'g*', tplot, yplot, 'k-', 'linewidth', 3 );
  grid ( 'on' );
  s = sprintf ( 'Compare expsin rk1 solutions using %d and %d steps', n1, n2 );
  legend ( 'rk1 solution', 'rk2 solution', 'extrapolation', 'exact', 'location', 'north' );
  title ( s );
  filename = sprintf ( 'expsin_rk1_extrapolate_%d.png', n1 );
  print ( '-dpng', filename );

  plot ( [t0,tstop], [0.0,0.0], 'k--', t1, e2, 'r-', t1, e3, 'g-', t1, e, 'k-', 'linewidth', 3 );
  grid ( 'on' );
  s = sprintf ( 'Extrapolated expsin rk1 estimated error using %d and %d steps', n1, n2 );
  title ( s );
  legend ( 'Zero error', 'Estimated error', 'Extrapolated error', 'Exact error', ...
    'location', 'north' );
  filename = sprintf ( 'expsin_rk1_extrapolate_error_%d.png', n1 );
  print ( '-dpng', filename );
%
%  Report error estimates for whole solution, and at final point.
%
  fprintf ( 1, '\n' );

  fprintf ( 1, '  Exact        RMS error = %g\n', e_norm );
  fprintf ( 1, '  Estimated    RMS error = %g\n', e2_norm );
  fprintf ( 1, '  Extrapolated RMS error = %g\n', e3_norm );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  At the point t          = %g,\n', tstop );
  fprintf ( 1, '  Y exact value           = %g\n', y(n1+1) );
  fprintf ( 1, '  Y estimate              = %g\n', y1(n1+1) );
  fprintf ( 1, '  Y double step estimate  = %g\n', y2(n2+1) );
  fprintf ( 1, '  Y extrapolated estimate = %g\n', y3(n1+1) );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Y exact error           = %g\n', y(n1+1)  - y1(n1+1) );
  fprintf ( 1, '  Y error estimate        = %g\n', y2(n2+1) - y1(n1+1) );
  fprintf ( 1, '  Y extrapolated error    = %g\n', y(n1+1) -  y3(n1+1) );

  return
end

