function [ t, y, e ] = rk12 ( yprime, n, tspan, y0 )

%*****************************************************************************80
%
%% rk12 uses Runge-Kutta schemes of order 1 and 2.
%
%  Discussion:
%
%    This function approximates the solution of a differential
%    equation of the form:
%      y' = yprime(t,y)
%      y(tspan(1)) = y0
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    06 November 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    function handle yprime: defines the derivative function of the form
%      value = yprime ( t, y )
%
%    integer n: the number of steps to take.
%
%    real tspan(2): contains the initial and final times.
%
%    real y0: contains the initial condition.
%
%  Output:
%
%    real t(n+1): the times.
%
%    real y(n+1): the estimated solution values.
%
%    real e(n+1): the estimated truncation errors.
%
  dt = ( tspan(2) - tspan(1) ) / n;

  t(1) = tspan(1);
  y(1) = y0;
  e(1) = 0.0;
 
  for i = 1 : n

    k1 = dt * yprime ( t(i),      y(i) );
    y1 = y(i) + k1;
    k2 = dt * yprime ( t(i) + dt, y(i) + k1 );
    y2 = y(i) + 0.5 * k1 + 0.5 * k2;

    t(i+1) = t(i) + dt;
    y(i+1) = y2;
    e(i+1) = y2 - y1;

  end

  return
end

