function [ t, y ] = adapt1 ( dydt, tspan, y0, dt, tol )

%*****************************************************************************80
%
%% adapt1 reduces the stepsize if the error is too big.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    13 October 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    function handle dydt: defines the derivative function of the form
%      value = dydt ( t, y )
%
%    real tspan(2): contains the initial and final times.
%
%    real y0(m): contains the initial condition.
%
%    real dt: the size of the first step.
%
%    real tol: an error tolerance.
%
%  Output:
%
%    real t(n+1): the times.
%
%    real y(n+,m): the estimated solution values.
%
  t0 = tspan(1);
  tstop = tspan(2);
%
%  Initial value.
%
  i = 1;
  t(i) = t0;
  y(i) = y0;
%
%  Until you reach or pass TSTOP, take another step.
%
  while ( t(i) < tstop )
%
%  Full step of size DT.
%   
    y1 = y(i) + dt * dydt ( t(i), y(i) );
%
%  Two half steps of size DT/2.
%
    th = t(i) + dt / 2.0;
    yh = y(i) + dt / 2.0 * dydt ( t(i), y(i) );
    y2 = yh   + dt / 2.0 * dydt ( th, yh );
%
%  Compute result using step doubling and extrapolation.
%
    t(i+1) = t(i) + dt;
    y(i+1) = 2.0 * y2 - y1;
    i = i + 1;
%
%  Set the error estimate.
%
    e = y2 - y1;
%
%  Decrease or increase stepsize if error is large or small.
%
    if ( 1.5 * tol * dt / ( tstop - t0 ) < abs ( e ) )
      dt = dt / 2.0;
    end

  end

  return
end

