Volumes in n dimensions

The n-ball of radius $R$ in $n$ dimensions has volume $V_{n}(R)$ given by:

$$ V_{n}(R)={\frac {\pi ^{\frac {n}{2}}}{\Gamma \left({\frac {n}{2}}+1\right)}}R^{n} $$

Let's plot this:

In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from scipy import special
In [3]:
def vol_sphere(n):
    "Return the volume of the unit ball in n dimensions"
    return np.pi**(n/2)/ special.gamma(n/2+1)

def vol_cube(n):
    "Return the volume of the cube surrounding the unit ball in n dimensions"
    return 2**n
In [4]:
n = np.arange(1, 10)
sph = vol_sphere(n)
cub = vol_cube(n)

plt.scatter(n, sph, label="Sphere")
plt.scatter(n, cub, label="Cube")
plt.legend();