function exercise2 ( initial_condition )

%*****************************************************************************80
%
%% exercise2 solves the Burgers equation using the upwind method.
%
%  Discussion:
%
%    We consider BNI, the Burgers nonconservative inviscid equation.
%
%    We also track the conservation of velocity.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    16 July 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    function value = initial_condition ( x ): sets the initial condition.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'exercise2:\n' );
  fprintf ( 1, '  Solve the Burgers equation with upwind differencing.\n' );
  fprintf ( 1, '  Use the nonconservative inviscid equation.\n' );

  xmin = -1.0;
  xmax = +1.0;
  nx = 81;
  x = linspace ( xmin, xmax, nx );
  dx = ( xmax - xmin ) / ( nx - 1 );

  tmin = 0.0;
  tmax = 2.0;
  nt = 201;
  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 ];

  c = zeros ( nt, 1 );

  for k = 1 : nt

    if ( k == 1 )
      un = initial_condition ( x );
%
%  Use the Upwind scheme.
%  Because we are assuming the velocity is positive, we always
%  take the backward spatial derivative.
%
    else
      uo = un;
      un(I) = uo(I) - dt / dx * uo(I) .* ( uo(I) - uo(IM1) );
      un(nx) = un(1);
    end
%
%  Plot the computed solution.
%
    clf ( );
    plot ( x, un, 'b-', 'linewidth', 3 );
    grid ( 'on' );
    xlabel ( '<--- x --->' );
    ylabel ( '<--- u(x,t) -->' );
    label = sprintf ( 'BNI Upwind: k = %3d, t = %4.1f', k, t(k) );
    title ( label );
    axis ( [ xmin, xmax, 0.0, 1.1 ] );
    pause ( 0.1 );

    c(k) = sum ( un(I) * dx );

  end

  filename = 'exercise2.png';
  print ( '-dpng', filename );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Last velocity plot saved as "%s"\n', filename );
%
%  Plot the integral of velocity, which should be conserved.
%
  clf ( );
  plot ( t, c, 'b-', [tmin,tmax], [c(1), c(1) ], 'r--', 'linewidth', 3 );
  grid ( 'on' );
  xlabel ( '<--- t --->' );
  ylabel ( '<--- \int(u dx) -->' );
  title ( 'BNI Upwind: conservation of \int(u dx)' );
  emax = 1.1 * max ( e );
  axis ( [ tmin, tmax, 0.0, emax ] );
  filename = 'exercise2_conservation.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Conservation plot saved as "%s"\n', filename );

  return
end

