function exercise2 ( exact )

%*****************************************************************************80
%
%% exercise2 solves the advection equation using the FTCS method.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    16 July 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    value = exact ( x, t, c ): the name of a function, preceded by an @ sign,
%    which returns the exact solution of the advection equation at location
%    x, time t, and velocity c.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'exercise2:\n' );
  fprintf ( 1, '  Solve the advection equation using the FTCS method.\n' );

  c = 0.8;

  xmin = 0.0;
  xmax = 100.0;
  nx = 101;
  x = linspace ( xmin, xmax, nx );
  dx = ( xmax - xmin ) / ( nx - 1 );

  tmin = 0.0;
  tmax = 126.0;
  nt = floor ( c * ( tmax - tmin ) / ( 0.4 * dx ) );
  t = linspace ( tmin, tmax, nt );
  dt = ( tmax - tmin ) / ( nt - 1 );

  IM1 = [ nx-1, 1:nx-2 ];
  I   = [ 1:nx-1 ];
  IP1 = [ 2:nx-1, 1 ];

  err = 0.0;

  for k = 1 : nt

    if ( k == 1 )
      un = exact ( x, t(k), c );
%
%  Use the FTCS scheme.
%
    else
      uo = un;
      un(I) = uo(I) - c * dt / dx * ( uo(IP1) - uo(IM1) ) / 2.0;
      un(nx) = un(1);
    end
%
%  Plot the exact and computed solutions.
%
    vn = exact ( x, t(k), c );

    err = err + rms ( un - vn );
    
    plot ( x, un, 'b-', x, vn, 'r--', 'linewidth', 3 );
    grid ( 'on' );
    xlabel ( '<--- x --->' );
    ylabel ( '<--- u(x,t) -->' );
    label = sprintf ( 'FTCS: k = %3d, t = %4.1f', k, t(k) );
    title ( label );
    axis ( [ xmin, xmax, 0.0, 1.1 ] );
    pause ( 0.1 );

  end

  err = err / nt;
  fprintf ( 1, '  Averaged RMS error = %g\n', err );

  filename = 'ftcs.png';
  print ( '-dpng', filename );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end

