function ccff_sparse_test ( )

%*****************************************************************************80
%
%% ccff_sparse_test() uses ccff() to build a sparse grid.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    19 May 2014
%
%  Author:
%
%    John Burkardt
%
%  Local:
%
%    integer D, the spatial dimension.
%
%    integer MAXK, the maximum level to check.
%
  d = 10;
  maxk = 5;
  func = 'prod( exp(-(x/2).^2/2)/2/sqrt(2*pi), 2)';
  trueval = fu_integral ( d );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'ccff_sparse_test():\n' );
  fprintf ( 1, '  ccff() builds a sparse grid:\n' );
  fprintf ( 1, '  Sparse nested, unweighted quadrature over [0,1].\n' );
  fprintf ( 1, '  Exact integral should be %g\n', trueval );
  fprintf ( 1, '\n' );
  fprintf ( 1, '   D  Level   Nodes    SG estimate MC estimate\n' );
  fprintf ( 1, '                       SG error    MC error\n' );
  fprintf ( 1, '\n' );

  for k = 1 : maxk
%
%  Compute sparse grid estimate.
%
    [ x, w ] = nwspgr ( 'ccff', d, k );
    fx = eval ( func );
    sgappr = w' * fx;
    sgerror = sqrt ( ( sgappr - trueval ).^2 ) / trueval;
%
%  Average 1000 Monte Carlo estimates.
%
    numnodes = length ( w );
    sim = zeros ( 1000, 1 );
    for r = 1 : 1000
      x = rand ( numnodes, d );
      fx = eval ( func );
      sim(r) = mean ( fx );
    end
    simerror = sqrt ( mean ( ( sim - trueval ).^2 ) ) / trueval;

    fprintf ( '  %2d     %2d  %6d  %10.5g  %10.5g\n', ...
      d, k, numnodes, sgappr, mean ( sim ) )
    fprintf ( '                     %10.5g  %10.5g\n', ...
      sgerror, simerror )

  end

  return
end
