Basically 3-D Darts throwing program

  • Thread starter KevinNukkE
  • Start date
  • Tags
    Program
In summary, the problem involves using Monte Carlo to compute the volume fraction bounded by two spheres in a box bounded by planes. The code used involves generating random points within the box and checking if they are inside the spheres. The volume fraction is then calculated by dividing the number of points inside the spheres by the total number of points, and then multiplied by 2 to account for negative coordinates.
  • #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);
 
Physics news on Phys.org
  • #2

disp(x);
disp(y);
disp(z);
Dartstime = n/toc;
volume = 4 * Nc;
volume = volume * 2;
disp(volume);

To answer your questions:

A. No, you do not need to repeat each plane variable 4 times. The spheres are 3-dimensional objects, so you only need to check if the point is inside the sphere at any given coordinate (x,y,z).

B. Yes, you should multiply your random numbers for x, y, and z by 2 to have them "bound" inside the box. This ensures that the points are within the bounds of the box (x=±2, y=±2, z=±2).

Your code looks correct, but there are a few things you can improve upon. Instead of using a for loop to generate the random numbers, you can use the "rand" function to generate all the random numbers at once. Also, instead of using multiple if statements, you can use a single if statement with logical operators to check if the point is inside the sphere. Here is an example of how your code can be improved:

% Define the radius of the spheres and the bounds of the box
r = 1; % radius of the spheres
xmin = -2; % minimum x coordinate of the box
xmax = 2; % maximum x coordinate of the box
ymin = -2; % minimum y coordinate of the box
ymax = 2; % maximum y coordinate of the box
zmin = -2; % minimum z coordinate of the box
zmax = 2; % maximum z coordinate of the box

% Generate n random points within the box
n = 100000000; % number of random points
x = (xmax-xmin).*rand(n,1) + xmin; % generate n random x coordinates
y = (ymax-ymin).*rand(n,1) + ymin; % generate n random y coordinates
z = (zmax-zmin).*rand(n,1) + zmin; % generate n random z coordinates

% Check if the points are inside the spheres
Nc = sum((x.^2 + y.^2 + z.^2) <= r^2); % count the number of points inside the spheres

% Calculate the volume fraction
volume_fraction = Nc/n; % divide the tally by the number of trials

% Multiply by 2 to account
 

Related to Basically 3-D Darts throwing program

1. How does the 3-D Darts throwing program work?

The 3-D Darts throwing program uses computer algorithms and mathematical calculations to simulate the physics of a dart throw. It takes into account factors such as force, angle, and spin to accurately predict the trajectory of the dart.

2. Can the 3-D Darts throwing program improve my dart throwing skills?

While the 3-D Darts throwing program can provide helpful insights and tips for improving your technique, it is ultimately up to the individual to practice and refine their skills. The program can serve as a useful training tool, but it is not a guaranteed method for improving your skills.

3. Is the 3-D Darts throwing program accurate?

The 3-D Darts throwing program has been extensively tested and verified for accuracy. However, there are many variables in a real-life dart throwing scenario that may affect the accuracy of the program's predictions. It is meant to be a simulation and should not be relied upon as a perfect representation of real-life dart throwing.

4. Can the 3-D Darts throwing program be customized for different dart games?

Yes, the program can be customized for different dart games such as 501, Cricket, and more. Users can input their own game rules and the program will adjust its calculations accordingly.

5. Is the 3-D Darts throwing program only for professional dart players?

The 3-D Darts throwing program can be used by anyone, regardless of their skill level. It can be a useful tool for beginners to learn the basics of dart throwing and for more experienced players to fine-tune their skills. The program is designed to be user-friendly and accessible to all levels of players.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
905
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Precalculus Mathematics Homework Help
Replies
17
Views
1K
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
148
  • Calculus and Beyond Homework Help
Replies
14
Views
697
  • Precalculus Mathematics Homework Help
Replies
7
Views
905
  • Calculus and Beyond Homework Help
Replies
3
Views
592
Back
Top