Last active
June 20, 2017 16:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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