function fd2d_convergence_test ( )

%*****************************************************************************80
%
%% fd2d_convergence_test() does a convergence study;
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    06 December 2019
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Charles Hall, Thomas Porsching,
%    Numerical Analysis of Partial Differential Equations, page 258,
%    Prentice-Hall, 1990,
%    ISBN: 013626557X,
%    LC: QA374.H29.
%
  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'fd2d_convergence_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Use fd2d_poisson() to solve the same problem on a\n' );
  fprintf ( 1, '  sequence of meshes of mesh size h.  Compute RMS error E.\n' );
  fprintf ( 1, '  Plot log(h) versus log(E).\n' );

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

  fprintf ( 1, '\n' );
  fprintf ( 1, '  i           h(i)     e(i)                rate\n' );
  fprintf ( 1, '\n' );

  for k = 1 : 5

    nx = 2^(k+1) + 1;
    ny = nx;
    [ U, X, Y ] = fd2d_poisson ( nx, ny, xmin, xmax, ymin, ymax, @f, @g );

    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 );

    h(k) = 1.0 / ( nx - 1 );
    e(k) = err;

    if ( k == 1 )
      fprintf ( 1, '%2d  %14.6g  %14.6g\n', k, h(k), e(k) );
    else
      r = ( log(e(k-1)) -log(e(k)) ) / ( log(h(k-1)) -log(h(k)) );
      fprintf ( 1, '%2d  %14.6g  %14.6g  %14.6g\n', k, h(k), e(k), r );
    end

  end
%
%  Plot the computed and exact solutions.
%
  x = log ( h );
  y = log ( e );
  plot ( x, y, 'r-o', 'linewidth', 3 );
  axis ( 'equal' )
  grid ( 'on' );
  xlabel ( '<-- log(h) -->' );
  ylabel ( '<-- log(RMS error) -->' );
  title ( 'Convergence rate' );
  filename = 'fd2d_convergence_test.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'fd2d_convergence_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  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:
%
%    06 December 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real X, Y, the arguments.
%
%  Output:
%
%    real VALUE, the value of F(X,Y).
%
  value = - 2.0 - ( x.^2 + y.^2 ) .* exp ( 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 = ( x - 2.0 ).^2 + exp ( 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

