- #1
KevinNukkE
- 3
- 0
Homework Statement
Two spheres with radius 1 cm and centers (-.25,0,0) and (.25,0,0) are contained in a box bounded by planes x=±2, y=±2, z=±2. Use Monte Carlo to compute the volume fraction bounded by the spheres in the box. Sample a point (x,y,z) randomly within the box. If the point is inside either of the spheres, tally 1 for that point. The volume fraction is just the tally divided by the number of trials
Homework Equations
I am using Matlab and its horrible, terrible random number generator.
The problem seems to me like a 3-D version of the darts game. My questions are:
A. Do I need to repeat each plane variable 4 times to get the full circle?
B. Should I multiply my random numbers for x, y, and z by 2 (for both positive and negative) to have them "bound" inside the box?
The Attempt at a Solution
I did this for the 2-D Darts game and came up with
function darts
Nc=0;
n=100000000;
tic;
for i = 1:n
x = rand(1,n);
y = rand(1,n);
if x.^2+y.^2<1
Nc = Nc+1;
end
end
dartstime = n/toc;
pies = 4 * Nc;
Using the same principle method I have
%%Throwing darts positive
x = rand;
y = rand;
z = rand;
%x and y plane
if x^2+y^2<0.5
Nc = Nc+1;
end
%Repeat 4 times for each of the three? then for the negative circle?
%x and z plane
if x^2+z^2<0.5
Nc = Nc+1;
end
%y and z plane
if y^2+z^2<0.5
Nc = Nc+1;
end
%%Throwing Darts negative
x1 = -rand;
y1 = -rand;
z1 = -rand;
if x1^2+y1^2<0.5
Nc = Nc+1;
end
if x1^2+z1^2<0.5
Nc = Nc+1;
end
if y1^2+z1^2<0.5
Nc = Nc+1;
end
disp(Nc);