function reaction_twoway_euler ( n )

%*****************************************************************************80
%
%% reaction_twoway_euler() solves reaction_twoway_ode() using euler().
%
%  Discussion:
%
%    The reference suggests an experiment in which the Forward Euler 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_euler():\n' );
  fprintf ( 1, '  Solve reaction_twoway_ode() using euler()\n' );
%
%  Get parameter values.
%
  [ k1, k2, t0, y0, tstop ] = reaction_twoway_parameters ( );
%
%  Solve the equation.
%
  f = @ reaction_twoway_deriv;
  tspan = [ t0, tstop ];

  [ t, y ] = euler ( 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 ( );
  plot ( t, y(:,1), 'ro', t, y(:,2), 'go', ...
         t, ye(:,1), 'c-', t, ye(:,2), 'm-', ...
         tspan, [0.0,0.0], 'k--', ...
         'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<---  t  --->' );
  ylabel ( '<---  Concentrations  --->' );
  title_string = sprintf ( 'reaction_twoway_ode(): euler, k2 = %g', k2 );
  title ( title_string );
  legend ( 'W1', 'W2', 'W1exact', 'W2exact', 'x axis', 'Location', 'North' );
  filename = 'reaction_twoway_euler_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-', ...
           tspan, [h(1),h(1)], 'b--', ...
           tspan, [0.0,0.0], 'k--', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<---  t  --->' );
  ylabel ( '<---  conserved quantity  --->' );
  title_string = sprintf ( 'reaction_twoway_ode(): euler, conserved, k2 = %g', k2 );
  title ( title_string );
  legend ( 'h', 'h initial', 'x axis' );
  filename = 'reaction_twoway_euler_conservation.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
