Skip to content

Instantly share code, notes, and snippets.

@localmin
Last active June 20, 2017 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save localmin/b68adf63d066ad270eda8f3a249059f6 to your computer and use it in GitHub Desktop.
Save localmin/b68adf63d066ad270eda8f3a249059f6 to your computer and use it in GitHub Desktop.
This script gets the volumes of D-mentional unit sphere by Monte Carlo simulation.
clear
% Number of random data points,case of 100, 1000 & 10000
N = [100,1000,10000];
[~,A] = size(N);
% Dimension numbers
D = 20;
for d = 1:D
for a=1:A
cnt=0;
volume = zeros(N(a),1);
for n = 1:N(a)
vec = rand(d,1);
cond = vec'*vec;
if(cond<=1)
cnt = cnt + 1;
end
% Get volume by Monte Carlo using n data points.
volume(n,1) = (cnt / n)*2^d;
end
% Plot the results
subplot(A,1,a);plot([1:N(a)], volume);
xlabel('number of random data point');ylabel('Volume');
lgnd_vlm = sprintf( 'Volume: %f', volume(N(a)) );
legend( lgnd_vlm );
end
saveas(gcf,['result' num2str(d) '.fig']);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment