function stiff_midpoint_explicit ( n )

%*****************************************************************************80
%
%% stiff_midpoint_explicit applies the explicit midpoint method to STIFF ODE.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    03 November 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N: the number of steps to take.
%
  if ( nargin < 1 )
    n = 25;
  end

  t0 = 0.0;
  tstop = 1.0;
  tspan = [ t0, tstop ];
  y0 = 0.0;

  [ t1, y1 ] = midpoint_explicit ( tspan, y0, n );

  t2 = linspace ( t0, tstop, 101 );
  y2 = stiff_solution ( t2 );

  clf ( );
  plot ( t1, y1, 'ro-', t2, y2, 'b-', 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<-- t -->' );
  ylabel ( '<-- y(t) -->' );
  s = sprintf ( 'stiff ODE, midpoint explicit, n = %d', n );
  title ( s );
  legend ( 'Computed', 'Exact' );
  filename = 'stiff_midpoint_explicit.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
function [ t, y ] = midpoint_explicit ( tspan, y0, n )

%*****************************************************************************80
%
%% midpoint_explicit applies an explicit midpoint method to the stiff ODE.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    07 February 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N: the number of steps to take.
%
%  Output:
%
%    real t(n+1), y(n+1): the times and solution values.
%
  t0 = tspan(1);
  tstop = tspan(2);

  t = zeros ( n + 1, 1 );
  y = zeros ( n + 1, 1 );

  dt = ( tstop - t0 ) / n;

  t(1) = t0;
  y(1) = y0;

  for i = 1 : n
    t(i+1) = t(i) + dt;
    y(i+1) = ... 
      ( ...
        y(i) + 25.0 * dt * ( ...
                             cos ( t(i) ) - y(i) + cos ( t(i+1) ) ...
                           ) ...
      ) ...
      / ( 1.0 + 25.0 * dt );
  end

  return
end

