function fisher_pde_ftcs ( )

%*****************************************************************************80
%
%% fisher_pde_ftcs() solves the KPP Fisher equation using FTCS().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 September 2024
%
%  Author:
%
%    John Burkardt
%
  timestamp ( );
%
%  Set the X values.
%
  nx = 51;
  xmin = 0.0;
  xmax = 1.0;
  x = linspace ( xmin, xmax, nx );
  dx = ( xmax - xmin ) / ( nx - 1 );
%
%  Set the T values.
%
  nt = 801;
  tmin = 0.0;
  tmax = 0.05;
  t = linspace ( tmin, tmax, nt );
  dt = ( tmax - tmin ) / ( nt - 1 );
%
%  Indexes to approximate uxx:
%
  Im1 = [ 2,  1:nx-2, nx-1 ];
  I   = [ 1,  2:nx-1, nx   ];
  Ip1 = [ 2,  3:nx,   nx-1 ];

  fprintf ( 1, '\n' );
  fprintf ( 1, 'fisher_pde_ftcs():\n' );
  fprintf ( 1, '  Solve the KPP Fisher PDE using ftcs()\n' );
  fprintf ( 1, '    du/dt = uxx + u * ( 1 - u )\n' );
  fprintf ( 1, '  over the spatial interval:\n' );
  fprintf ( 1, '    %g <= x <= %g\n', xmin, xmax );
  fprintf ( 1, '  and time interval\n' );
  fprintf ( 1, '     %g <= t <= %g\n', tmin, tmax );
  fprintf ( 1, '  with boundary conditions:\n' );
  fprintf ( 1, '    u(%g) = ( cos(100*t(j)) + 1.5 ) / 2.5 (Dirichlet)\n', xmin );
  fprintf ( 1, '    u''(%g) = 0.0 (Neumann)\n', xmax );
  fprintf ( 1, '  and initial condition\n' );
  fprintf ( 1, '    u(0,x) = cos(10*x).\n' );
  fprintf ( 1, '  Create a sequence of graphic figures.\n' );
  fprintf ( 1, '  Save each one to a png file.\n' );
  fprintf ( 1, '  Use "convert()" to create a gif animation.\n' );

  fprintf ( 1, '  Space steps = %d\n', nx );
  fprintf ( 1, '  Space interval is [%g,%g]\n', xmin, xmax );
  fprintf ( 1, '  Space increment is %g\n', dx );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Time steps = %d\n', nt );
  fprintf ( 1, '  Time interval is [%g,%g]\n', tmin, tmax );
  fprintf ( 1, '  Time increment is %g\n', dt );
%
%  Set the initial condition.
%
  frame = 0;

  for j = 1 : nt

    if ( j == 1 )
      u(1:nx) = ( cos ( 15.0 * x ) + 1.5 ) / 2.5;
    else
      dudxx = ( u(Ip1) - 2.0 * u(I) + u(Im1) ) / dx^2;
      dudt = dudxx + u .* ( 1.0 - u );
      u(1:nx) = u(1:nx) + dt * dudt;
    end
%
%  Enforce the Dirichlet condition on the left.
%
    u(1) = ( cos ( 100.0 * t(j) ) + 1.5 ) / 2.5;
%
%  Enforce the Neumann condition on the right.
%
    u(nx) = u(nx-1);
%
%  Display the current solution.
%
    f = figure ( "visible", "off" );
    clf ( );
    hold ( 'on' );
      plot ( x, u, 'b-', 'linewidth', 2 );
      plot ( [x(1),x(nx)], [0.0,0.0], 'k-', 'linewidth', 2 ); 
    hold ( 'off' );
    axis ( [ xmin, xmax, 0.0, 1.0 ] );
    grid ( 'on' );
    label = sprintf ( 'u(t,x), fisher pde ftcs, step = %d, t=%.3f', j, t(j) );
    title ( label );
    xlabel ( '<--- X --->' );
    ylabel ( '<--- U(T,X) --->' );

    if ( mod ( j, 25 ) == 1 )
      filename = sprintf ( '%05d_frame.png', frame );
      print ( '-dpng', filename );
      frame = frame + 1;
    end

    if ( j == 1 )
      filename = 'fisher_pde_ftcs_initial.png';
      print ( '-dpng', filename );
      fprintf ( 1, '  Graphics saved as "%s"\n', filename );
    elseif ( j == nt )
      filename = 'fisher_pde_ftcs_final.png';
      print ( '-dpng', filename );
      fprintf ( 1, '  Graphics saved as "%s"\n', filename );
    end

  end
%
%  Use Imagemagick "convert()" to merge png images into a gif.
%
  system ( "convert -delay 10 -loop 0 *.png fisher_pde_ftcs.gif" );
  system ( "rm *_frame.png" );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Animation created as "fisher_pde_ftcs.gif"\n' );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'fisher_pde_ftcs():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  timestamp ( );

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% timestamp() prints the current YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  t = now;
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end

