function reaction_twoway_midpoint ( n )

%*****************************************************************************80
%
%% reaction_twoway_midpoint() solves the two way reaction ODE using midpoint().
%
%  Discussion:
%
%    The reference suggests an experiment in which the Forward midpoint method
%    is used, with a timestep of 1/50.  The problem is to be solved three
%    times, with the second reaction coefficient, k2, set to 10 (the default), 
%    100, and 1000.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 February 2021
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Willem Hundsdorfer, Jan Verwer,
%    Numerical solution of time-dependent advection-diffusion-reaction equations,
%    Springer, 2003
%    ISBN: 978-3-662-09017-6
%
%  Input:
%
%    integer N, the number of time steps, including the initial value.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'reaction_twoway_midpoint:\n' );
  fprintf ( 1, '  Solve the two way reaction ODE using midpoint()\n' );
%
%  Get parameter values.
%
  [ k1, k2, t0, y0, tstop ] = reaction_twoway_parameters ( );
%
%  Solve the equation.
%
  f = @ reaction_twoway_deriv;
  tspan = [ t0, tstop ];

  [ t, y ] = midpoint ( f, tspan, y0, n );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of steps will be %d\n', n );
  fprintf ( 1, '  Stepsize will be %g\n', ( tstop - t0 ) / ( n - 1 ) );
%
%  Evaluate exact solution.
%
  ye = reaction_twoway_exact ( t );
%
%  Plot.
%
  figure ( 1 );
  clf ( );
  hold ( 'on' );
    plot ( t, y(:,1),  'ro', 'linewidth', 3 );
    plot ( t, y(:,2),  'go', 'linewidth', 3 );
    plot ( t, ye(:,1), 'c-', 'linewidth', 3 ); 
    plot ( t, ye(:,2), 'm-', 'linewidth', 3 );
    plot ( tspan, [0.0,0.0], 'k--', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<---  t  --->' );
  ylabel ( '<---  Concentrations  --->' );
  title_string = sprintf ( 'Two-way reaction: solution midpoint, k2 = %g', k2 );
  title ( title_string );
  legend ( 'W1', 'W2', 'W1exact', 'W2exact', 'x axis', 'Location', 'North' );
  filename = 'reaction_twoway_midpoint_solution.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  h = reaction_twoway_conserved ( y );

  figure ( 2 );
  clf ( );
  hold ( 'on' );
    plot ( t,      h,          'r-',  'linewidth', 3 );
    plot ( tspan, [h(1),h(1)], 'b--', 'linewidth', 3 );
    plot ( tspan, [0.0,0.0],   'k--', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<---  t  --->' );
  ylabel ( '<---  conserved quantity  --->' );
  title_string = sprintf ( 'conserved: two-way reaction midpoint, k2 = %g', k2 );
  title ( title_string );
  legend ( 'h', 'h initial', 'x axis' );
  filename = 'reaction_twoway_midpoint_conservation.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'reaction_twoway_midpoint():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end
