function mc_2d_test ( )

%*****************************************************************************80
%
%% MC_2D_TEST uses an increasing sequence of Monte Carlo "quadrature rules".
%
%  Discussion:
%
%    The Monte Carlo procedure can be regarded as a quadrature rule with
%    a uniform weight...and slow convergence.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    16 February 2013
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'MC_2D_TEST:\n' );
  fprintf ( 1, '  Monte Carlo "quadrature rules" of increasing order.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '             N        Q(N)        Err(N)\n' );
  fprintf ( 1, '\n' );
  t = 0.25 * pi * ( erf ( 0.4)+erf(0.6))*(erf(0.3)+erf(0.7));
%
%  First, just try N = 1 to 21.
%
  for n = 1 : 21

    x = rand ( 2, n );
%
%  Evaluate function, compute quadrature formula and error.
%
    fx = exp ( - ( x(1,:) - 0.3 ).^2 - ( x(2,:) - 0.4 ).^2 );
    q = sum ( fx ) / n;
    e = abs ( q - t );
    fprintf ( 1, '  %12d  %12f  %12g\n', n, q, e );

  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '             N        Q(N)        Err(N)\n' );
  fprintf ( 1, '\n' );
%
%  Now try powers of 2 to get an accurate result.
%
  n = 1;
  for n_log = 0 : 20

    x = rand ( 2,  n );
%
%  Evaluate function, compute quadrature formula and error.
%
    fx = exp ( - ( x(1,:) - 0.3 ).^2 - ( x(2,:) - 0.4 ).^2 );
    q = sum ( fx ) / n;
    e = abs ( q - t );
    fprintf ( 1, '  %12d  %12f  %12g\n', n, q, e );
    n = n * 2;

  end
%
%  Make a log log plot of n versus e.
%
  n_plot = zeros ( 21, 1 );
  e_plot = zeros ( 21, 1 );

  n = 1;
  for n_log = 0 : 20
    x = rand ( 2, n );
    fx = exp ( - ( x(1,:) - 0.3 ).^2 - ( x(2,:) - 0.4 ).^2 );
    q = sum ( fx ) / n;
    e = abs ( q - t );
    n_plot(n_log+1) = n;
    e_plot(n_log+1) = e;
    n = n * 2;
  end
%
%  For some reason, clunky MATLAB graphics will NOT do a LOGLOG
%  plot if I use hold on/hold off.
%
  loglog ( n_plot, e_plot, 'r-*', ...
          [n_plot(1), n_plot(21)], [0.1, 0.1/1024], 'k-', 'Linewidth', 3 );
  grid on;
  xlabel ( '<---Log(N)--->', 'Fontsize', 24 );
  ylabel ( '<---Log(Error)--->', 'Fontsize', 24 );
  title ( 'LogLog plot of Monte Carlo Quadrature Error', 'Fontsize', 24 );

  print ( '-dpng', 'mc_2d_test.png' );

  return
end


