function exercise5 ( )

%*****************************************************************************80
%
%% exercise5 analyzes the error in Poisson solutions as a function of n.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    16 July 2020
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'exercise5:\n' );
  fprintf ( 1, '  Solve the Poisson equation with an increasing number of points.\n' );
  fprintf ( 1, '  Look at the corresponding behavior of the error.\n' );

  nplot = [];
  eplot = [];
  for n = [ 11, 21, 41, 81, 161, 321, 641]
    err = exercise4 ( n );
    nplot = [ nplot, n-1 ];
    eplot = [ eplot, err ];
  end

  plot ( log ( nplot ), log ( eplot ), 'ro-' );
  grid ( 'on' );
  xlabel ( '<-- log ( n - 1 ) -->' );
  ylabel ( '<-- log ( RMS error ) -->' );
  title ( 'Exercise 5: N versus RMS error' );
  print ( '-dpng', 'exercise5.png' );

  return
end
function err = exercise4 ( n )

%*****************************************************************************80
%
  a = 0.0;
  b = 5.0;

  [ x, u ] = poisson_solve ( n, a, b,  @f, @g );
  err = rms ( u' - g(x) );
  
  return
end

function value = f ( x )

%*****************************************************************************80
%
  value = - ( 2.0 * x + 5.0 ) .* exp ( x );
  return
end

function value = g ( x )

%*****************************************************************************80
%
  value = ( 2.0 * x + 1.0 ) .* exp ( x );
  return
end

