1
$\begingroup$

I am new to Monte Carlo integration and have been tasked with using MC integration in order to calculate the volume of a torus with inner radius 5cm and outer radius 10cm. Below is the code I have used to plot a 3d torus, however what I need to do now is plot an array of random 3d points within the figure window and determine how many lie within the volume of the torus in order to calculate its approximate volume.

Below is the code I have written so far:

def plot_torus(N, c, a):
   U = np.linspace(0, 2*np.pi, N)
   V = np.linspace(0, 2*np.pi, N)
   U, V = np.meshgrid(U, V)
   X = (c+a*np.cos(V))*np.cos(U)
   Y = (c+a*np.cos(V))*np.sin(U)
   Z = a*np.sin(V)
   return X, Y, Z

x, y, z = plot_torus(100, 10, 2.5)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(x, y, z, antialiased=True, color='pink')
ax.set_xlabel('$x$', fontsize=14)
ax.set_ylabel('$y$', fontsize=14)
ax.set_zlabel('$z$', fontsize=14)

ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10) 
ax.set_zlim(-10, 10)
plt.show()

I have been able to do this for a simpler 2d plot with only 2 variables to worry about, but now I am not so sure as to how to plot these 3d points and how to count them. Can anyone point me in the right direction as to how to plot these random points? Any help would be highly appreciated!

$\endgroup$
1
  • 2
    $\begingroup$ You should learn at some point that with approximately random points, you get an error of $O(1/\sqrt{N})$ for $N$ the number of points sampled. With Sobol' sequences or other quasi-random low-discrepancy sequences you can "improve" this error to $O(1/N)$. $\endgroup$ Oct 22, 2023 at 9:27

1 Answer 1

2
$\begingroup$

You don't need to plot a torus to calculate its volume. Moreover you can analytically compute the volume integral and it's even on wikipedia. With Monte Carlo you can use rejection sampling to figure out the volume of the torus. For that purpose you can define a bounding box for the torus and generate $N$ points at random inside. Let $M$ of those points be within the torus, then $M/N\approx V_T/V_B$ approximates the ratio of the torus volume to the box volume, and thus $(M/N) \cdot V_B \approx V_T$ approximates the volume of the torus. To figure out which points are inside or outside of the torus you can use the implicit function or sdf $f:\mathbb{R}^3\to\mathbb{R}$ for its surface and if $f(p) <0$ then $p$ is a point inside the torus.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.