I have a question. I must build a program to calculate a volume of a cube with Monte Carlo method. Cube has a beginning in (0,0,0) on XYZ axis. There can also be some spheres. The spheres can be in cube then the volume of cube is := cube vol. - sphere vol. This sphere can also be outside the cube. I know how this method works but i have problem to define interval of random points ( i only know to calculate an integral with this method). The parameters are: a - lenght of cube side, point - (X,Y,Z) point which is center of sphere, r -radius. For example a = 1, point1 = (0,0,0), r1 = 0.5, point2 = (1.25,1.25,1.25) r2 = 0.25. How to determine interval of random points in this situation?
-
So if I understand you correctly, your problem is not about programming, but about the algorithm itself. – Hulk Mar 14 '15 at 22:41
-
1(1) A volume *is* an integral (unity over space). (2) There is no reason to ever pick a point outside the cube. It doesn't matter whether it is inside a sphere or not, it will never contribute to the volume. So you can restrict your sampling to the cube itself. – 5gon12eder Mar 14 '15 at 22:51
1 Answers
The interval of your points must be at least the interval of the cube itself. It can be more than the interval of the cube and you can still make the algorithm work, but it will be less computationally efficient. It cannot be less than the interval of the cube itself or you will get the wrong answer.
In more detail:
You always want to come up with a classification of points: Points inside the cube, vs points inside the cube but not inside a sphere. The ratio of the first class to the second class is the ratio of the volume of the cube to the volume of the cube that is not occupied by a sphere. (Technically, the one ratio approaches the other ratio as the number of points increases.)
So consider your three cases:
If your sampling interval is exactly the interval of the cube, your classification is simple. All the points are in the first class. You determine the second class just by checking each point against all the spheres.
If your sampling interval is larger than the interval of the cube, you are introducing a new class of points into the equation: points that are not even in the cube. Not only do you now need to test each point against the cube, the points that are not in the cube are useless. They do not get used at all because they are not in any category that helps you. You are doing some fraction of work for no purpose at all.
If your sampling interval is smaller than the interval of the cube, then this doesn't work at all for obvious reasons-- you're not sampling over the whole volume of the cube and so there are regions you are not (statistically) investigating, and you will approach the wrong ratio no matter what you do.
So one of these cases won't work; another case does exactly what you need it to do and nothing more; and the third case can be made to work but does a certain fraction of work that is literally useless and wasteful.

- 4,687
- 2
- 26
- 64