function string_pde ( m, n, dx, dt, alpha )

%*****************************************************************************80
%
%% string_pde solves a version of the string problem.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    04 August 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M, the number of time steps.
%
%    integer N, the number of spatial intervals.
%
%    real DX, the spatial step.
%
%    real DT, the time step.
%
%    real ALPHA, the CFL stability parameter.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'string_pde\n' );
  fprintf ( 1, '  Solve a particular version of the string ODE.\n' );

  u = zeros ( m + 1, n + 1 );

  for j = 1 : m + 1

    if ( j == 1 )

      u(1,1) = 0.0;
      for i = 2 : n
        x = ( i - 1 ) * dx;
        u(1,i) = string_initial_value ( x );
      end
      u(1,n+1) = 0.0;

    elseif ( j == 2 )

      u(2,1) = 0.0;
      for i = 2 : n
        x = ( i - 1 ) * dx;
        u(2,i) = ...
            ( alpha / 2.0 ) * u(1,i-1) ...
          + ( 1.0 - alpha ) * u(1,i)   ...
          + ( alpha / 2.0 ) * u(1,i+1) ...
          + dt * string_initial_derivative ( x );
      end
      u(2,n+1) = 0.0;

    else

      u(j,1) = 0.0;
      for i = 2 : n
        u(j,i) = ...
                          alpha   * u(j-1,i-1) ...
          + 2.0 * ( 1.0 - alpha ) * u(j-1,i)   ...
          +               alpha   * u(j-1,i+1) ...
          -                         u(j-2,i);
      end
      u(j,n+1) = 0.0;
    end

  end
%
%  Write data file.
%
  data_filename = 'string_data.txt';
  data_unit = fopen ( data_filename, 'wt' );

  for i = 1 : m + 1;
    t = ( i - 1 ) * dt;
    for j = 1 : n + 1
      x = ( j - 1 ) * dx;
      fprintf ( data_unit, '%10.4f  %10.4f  %10.4f\n', x, t, u(i,j) );
    end
    fprintf ( data_unit, '\n' );
  end
  fclose ( data_unit );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Plot data written to the file "%s".\n', data_filename );
%
%  Write gnuplot command file.
%
  command_filename = 'string_commands.txt';
  command_unit = fopen ( command_filename, 'wt' );

  fprintf ( command_unit, 'set term png\n' );
  fprintf ( command_unit, 'set output "string.png"\n' );
  fprintf ( command_unit, 'set grid\n' );
  fprintf ( command_unit, 'set style data lines\n' );
  fprintf ( command_unit, 'unset key\n' );
  fprintf ( command_unit, 'set xlabel "<---X--->"\n' );
  fprintf ( command_unit, 'set ylabel "<---Time--->"\n' );
  fprintf ( command_unit, 'splot "%s" using 1:2:3 with lines\n', data_filename );
  fprintf ( command_unit, 'quit\n' );

  fclose ( command_unit );

  fprintf ( 1, '  Gnuplot command data written to the file "%s".\n', command_filename );

  return
end
function value = string_initial_value ( x )

%*****************************************************************************80
%
%% string_initial_value supplies the initial condition.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    03 December 2012
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real X, the location.
%
%    Output, real VALUE, the value of the solution at time 0 and location X.
%
  if ( 0.25 <= x && x <= 0.50 )
    value = ( x - 0.25 ) * ( 0.50 - x );
  else
    value = 0.0;
  end

  return
end
function value = string_initial_derivative ( x )

%*****************************************************************************80
%
%% string_initial_derivative supplies the initial time derivative.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    01 December 2012
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, real X, the location.
%
%    Output, real VALUE, the value of the time derivative of the solution 
%    at time 0 and location X.
%
  value = 0.0;

  return
end

