function fd1d_poisson_test ( )

%*****************************************************************************80
%
%% fd1d_poisson_test() tests fd1d_poisson();
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    05 December 2019
%
%  Author:
%
%    John Burkardt
%
  addpath ( '../fd1d_poisson' );

  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'fd1d_poisson_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  fd1d_poisson() is a Poisson equation solver\n' );
  fprintf ( 1, '  for an interval.\n' );

  xmin = 0.0;
  xmax = 5.0;
  fprintf ( 1, '\n' );
  fprintf ( 1, '  The equations are:\n' );
  fprintf ( 1, '    -y" = -(2x+5)*exp(x).\n' );
  fprintf ( 1, '    y(%g) = %g\n', xmin, g(xmin) );
  fprintf ( 1, '    y(%g) = %g\n', xmax, g(xmax) );
  fprintf ( 1, '  over the interval [%g,%g]\n', xmin, xmax );

  n = 21;
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Using grid of n = %d points.\n', n );

  [ u, x ] = fd1d_poisson ( n, xmin, xmax,  @f, @g );
%
%  Compute the RMS error.
%
  err = 0.0;
  for i = 1 : n
    err = err + ( u(i) - exact(x(i)) )^2;
  end
  err = sqrt ( err / n );

  fprintf ( 1, '  The RMS error was %g\n', err );
%
%  Plot the computed and exact solutions.
%
  clf ( );
  hold ( 'on' );
  plot ( x, u, 'ro-', 'linewidth', 3 );
  xplot = linspace ( xmin, xmax, 101 );
  u2 = g ( xplot );
  plot ( xplot, u2, 'b-', 'linewidth', 3 );
  hold ( 'off' );
  grid ( 'on' );
  xlabel ( '<-- X -->' );
  ylabel ( '<-- Y -->' );
  title ( 'Computed (red) and exact (blue) solutions' );
  filename = 'fd1d_poisson_test.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'fd1d_poisson_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  timestamp ( );

  rmpath ( '../fd1d_poisson' );

  return
end
function value = f ( x )

%*****************************************************************************80
%
%% f evaluates the right hand side of the Poisson equation.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    05 December
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real X, the argument.
%
%  Output:
%
%    real VALUE, the value of F(X).
%
  value = - ( 2.0 * x + 5.0 ) .* exp ( x );

  return
end
function value = g ( x )

%*****************************************************************************80
%
%% g evaluates the boundary conditions.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    05 December 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real X, the argument.
%
%  Output:
%
%    real VALUE, the value of G(X).
%
  value = exact ( x );

  return
end
function value = exact ( x )

%*****************************************************************************80
%
%% exact evaluates the exact solution.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    13 December 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real X, the argument.
%
%  Output:
%
%    real VALUE, the exact solution at x.
%
  value = ( 2.0 * x + 1.0 ) .* exp ( x );

  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

