Volume of n-sphere? Please help me with my version:
Show older comments
Hello Experts,
I have to write a function for 10-dimensional sphere using Monte - Carlo method.
What I did is this:
N = 1000000;
g = 1;
S = 0;
RandVars = -1 + 2*rand(1,10,N);
for i = 1:N
if (sum(RandVars(1,10,:).^2) < 1)
S = S+1;
end
end
Please revise my code and assist me with computing the volume using the Monte Carlo.
Many thanks, Steve
Accepted Answer
More Answers (1)
Walter Roberson
on 10 Nov 2013
2 votes
To calculate volume, you need to divide the successes, S, by the number of attempts, to get the fill fraction. Then multiply the fill fraction by the area of a hypercube each side of which is the same as the "2" you used in "2 * rand(1,100,N)"
Note, the leading 1 in your rand() is not serving any purpose.
In each iteration of your loop, you are summing the same thing. Where are you indexing by "i" ?
5 Comments
Steve
on 10 Nov 2013
Walter Roberson
on 10 Nov 2013
Please look again:
In each iteration of your loop, you are summing the same thing. Where are you indexing by "i" ?
That's a hint.
Walter Roberson
on 10 Nov 2013
Another hint:
r = reshape(randperm(21), 3, 7); %some data
disp(r)
disp(sum(r.^2)) %think about this
Steve
on 11 Nov 2013
Steve
on 11 Nov 2013
Categories
Find more on Particle & Nuclear Physics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!