% Test convergence of Monte Carlo integration of N-dimensional unit hypersphere
function test2(Ndim)
Ndim  % display number of dimensions for reference
correct = answer(Ndim); % exact volume, for comparison
p = correct/(2^Ndim) % expected fraction of points inside sphere
Ntrials = 100
for m = 1:5
   Npoints(m) = 10^m;
   for trial = 1:Ntrials
      err(trial) = (hypersphere1(Ndim, Npoints(m)) - correct)/correct;
         % fractional error
   end
   err1(m) = sqrt(mean(err.^2));
end
loglog( Npoints, err1, 'k-o', ...
        Npoints, 1./sqrt(Npoints),'b-',...
        Npoints, sqrt((1-p)./(Npoints*p)), 'r-' );
legend( 'MonteCarlo error', '1/sqrt(N)', 'sqrt[(1-p)/(Np)]' )
title( 'Convergence of hypersphere integration', 'fontsize', 20 )
xlabel( 'N, number of M-C samples', 'fontsize', 18 )
ylabel( 'RMS error', 'fontsize', 18 )