function expsin_adapt2 ( tol )

%*****************************************************************************80
%
%% expsin_adapt2 uses an adaptive stepsize on the EXPSIN ODE.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    01 October 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  f = @ expsin_deriv;
  t0 = 0.0;
  tstop = 10.0;
  tspan = [ t0, tstop ];
  y0 = 1.0;
  dt = 0.1;
  if ( nargin < 1 )
    tol = 0.5;
  end
 
  [ t, y ] = adapt2 ( f, tspan, y0, dt, tol );

  n = length ( t ) - 1;
  fprintf ( 1, '  Number of steps taken: %d\n', n );
  fprintf ( 1, '  Initial stepsize = %g\n', dt );
  fprintf ( 1, '  Minimum stepsize = %g\n', min ( t(2:n+1) - t(1:n) ) );
  fprintf ( 1, '  Maximum stepsize = %g\n', max ( t(2:n+1) - t(1:n) ) );

  ye = expsin_solution ( t );
  e = ye - y;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  User specified error tolerance = %g\n', tol );
  fprintf ( 1, '  RMS norm of error: %g\n', rms ( e ) );

  figure ( 1 );
  te = linspace ( t0, tstop, 101 );
  ye = expsin_solution ( te );
  plot ( te, ye, 'b-', ...
         t, y, 'o', 'linewidth', 2 );
  grid ( 'on' );
  s = sprintf ( 'expsin\\_adapt2, tol = %g', tol );
  title ( s );
  xlabel ( '<--- T --->' );
  ylabel ( '<--- Y(T) --->' );
  legend ( 'Exact', 'Estimate', 'Location', 'North' );
  filename = 'expsin_adapt2.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  figure ( 2 )
  plot ( t(1:n), t(2:n+1)-t(1:n), 'b-', [t(1),t(n)], [0.0, 0.0], 'k-', 'linewidth', 3 )
  grid ( 'on' );
  s = sprintf ( 'expsin\\_adapt2 stepsizes, tol = %g', tol );
  title ( s );
  xlabel ( '<--- T --->' );
  ylabel ( '<--- dT --->' );
  filename = 'expsin_adapt2_stepsize.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end

