function [ estV,estFrac ] = sphereVolMC( dim,nn ) %sphereVolMC : MC for hypersphere volume % Monte Carlo estimate of volume of hypersphere in d dimensions % outputs the volume and the fraction of the corresponding hypercube estV = 0.0; for i=1:nn x = rand(dim,1); % column vector with 'dim' random numbers xNorm2 = x' * x; % squared norm of vector % (remember x' is transpose of x) if ( xNorm2 < 1 ) estV = estV + 1; end end estV = estV/nn; % this is the integral (volume of one "quadrant") estFrac = estV; % mult by 2^dim to get the vol of the full hypersphere estV = estV * 2^dim; end