function fd2d_poisson_test ( )

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

  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'fd2d_poisson_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  fd2d_poisson() is a simple poisson equation solver\n' );
  fprintf ( 1, '  for a rectangular region.\n' );

  nx = 21;
  ny = 41;

  xmin = 0.0;
  xmax = 1.0;
  ymin = 1.0;
  ymax = 3.0;

  [ U, X, Y ] = fd2d_poisson ( nx, ny, xmin, xmax, ymin, ymax, @f, @g );
%
%  Compute the RMS error.
%
  err = 0.0;
  for j = 1 : ny
    for i = 1 : nx
      err = err + ( U(i,j) - exact ( X(i,j), Y(i,j) ) )^2;
    end
  end
  err = sqrt ( err / nx / ny );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Using a %d by %d grid\n', nx, ny );
  fprintf ( 1, '  The RMS error was %g\n', err );
%
%  Plot the computed and exact solutions.
%
  figure ( 1 )
  surf ( X, Y, U );
  title ( 'Computed solution' );
  print ( '-dpng', 'poisson_computed_surface.png' );

  figure ( 2 )
  U2 = exact ( X, Y );
  surf ( X, Y, U2 );
  title ( 'Exact solution' );
  print ( '-dpng', 'poisson_exact_surface.png' );

  figure ( 3 )
  contourf ( X, Y, U );
  title ( 'Computed solution' );
  print ( '-dpng', 'poisson_computed_contours.png' );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'fd2d_poisson_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  rmpath ( '../fd2d_poisson' );

  return
end
function value = f ( x, y )

%*****************************************************************************80
%
%% f evaluates the right hand side of the Poisson equation.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    17 September 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real X, Y, the arguments.
%
%  Output:
%
%    real VALUE, the value of F(X,Y).
%
  value = pi * pi * ( x .* x + y .* y ) .* sin ( pi * x .* y );

  return
end
function value = g ( x, y )

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

  return
end
function value = exact ( x, y )

%*****************************************************************************80
%
%% exact evaluates the exact solution.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    06 December 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real X, Y, the arguments.
%
%  Output:
%
%    real VALUE, the value of the exact solution at (X,Y).
%
  value = sin ( pi * x .* y );

  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

