function exercise2 ( )

%*****************************************************************************80
%
%% exercise2 solves the time-dependent heat equation.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    13 May 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'exercise2\n' );
  fprintf ( 1, '  Time dependent heat equation.\n' );
%
%  Set the X values.
%
  nx = 21;
  a = 0.0;
  b = 3.0;
  x = linspace ( a, b, nx );
  dx = ( b - a ) / ( nx - 1 );
%
%  Set the T values.
%
  nt = 101;
  c = 0.0;
  d = 1.0;
  t = linspace ( c, d, nt );
  dt = ( d - c ) / ( nt - 1 );

  fprintf ( 1, '  Use nx = %d and nt = %d.\n', nx, nt );
%
%  Set the first solution to the initial condition.
%
  U = zeros ( nx, nt );

  U(1:nx,1) = h ( x );
%
%  Compute values at time j+1 from values at time j.
%
  for j = 1 : nt - 1
    U(1,j+1) = h ( x(1) );
    U(2:nx-1,j+1) = U(2:nx-1,j) ...
      + ( dt / dx^2 / 2.0 ) * ( U(1:nx-2,j) - 2.0 * U(2:nx-1,j) + U(3:nx,j) );
    U(nx,j+1) = h ( x(nx) );
  end
%
%  To plot U(X,T), turn the x and t vectors into tables.
%
  [ X, T ] = ndgrid ( x, t );

  surf ( X, T, U, 'edgecolor', 'none' );
  xlabel ( '<-- X -->' )
  ylabel ( '<-- T -->' );
  zlabel ( '<-- U(X,T) -->');
  title ( 'exercise2' );

  filename = 'exercise2.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'exercise2\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end
function value = h ( x )

%*****************************************************************************80
%
%% H evaluates the initial condition.
%
  value =   x .* ( 3 - x ) .* ( x.^2 - 2*x + 1 );

  return
end

