TExcelTAD
Home › Calculus in Depth › Triple Integrals in Spherical Coordinates

Triple Integrals in Spherical Coordinates

Spherical coordinates describe a point by its distance from the origin and two angles, like latitude and longitude. Balls, spherical shells, cones with the vertex at the origin, and anything that depends only on the distance ρ\rho become boxes in (ρ,θ,φ)(\rho, \theta, \varphi). The price is the volume element dV=ρ2sin⁡φ dρ dθ dφdV = \rho^2 \sin\varphi\,d\rho\,d\theta\,d\varphi, and that sin⁡φ\sin\varphi is also the reason why choosing "random angles" does not give random directions, a classic bug in graphics code.

The survey course introduces double and triple integrals in Double and Triple Integrals.

Read Triple Integrals in Cylindrical Coordinates first; the general theory is in Change of Variables in Multiple Integrals.

The coordinate system

A point PP has spherical coordinates (ρ,θ,φ)(\rho, \theta, \varphi):

  • ρ=∣OP∣≥0\rho = \lvert OP \rvert \ge 0, the distance from the origin;
  • θ\theta, the same angle as in cylindrical coordinates (longitude), 0≤θ<2π0 \le \theta < 2\pi;
  • φ\varphi, the angle between the positive zz-axis and OPOP (the polar angle, or colatitude), 0≤φ≤π0 \le \varphi \le \pi.
x=ρsin⁡φcos⁡θ,y=ρsin⁡φsin⁡θ,z=ρcos⁡φρ2=x2+y2+z2,r=ρsin⁡φ(the cylindrical r)\begin{aligned} x &= \rho \sin\varphi \cos\theta, & y &= \rho \sin\varphi \sin\theta, & z &= \rho \cos\varphi \\ \rho^2 &= x^2 + y^2 + z^2, & r &= \rho \sin\varphi && \text{(the cylindrical } r\text{)} \end{aligned}
Equation Surface
ρ=c\rho = c sphere of radius cc
θ=c\theta = c half-plane through the zz-axis
φ=c\varphi = c (0<c<π/20 < c < \pi/2) cone opening upward around the zz-axis
φ=π/2\varphi = \pi/2 the xyxy-plane
ρ=2cos⁡φ\rho = 2 \cos\varphi sphere x2+y2+(z−1)2=1x^2 + y^2 + (z - 1)^2 = 1

Note: Conventions differ. Mathematics texts (and this course) use θ\theta for longitude and φ\varphi for the polar angle; physics texts and many graphics papers swap the letters, and latitude in geography is π/2−φ\pi/2 - \varphi. Always check which angle a formula or API means.

The volume element

A spherical wedge with small Δρ\Delta\rho, Δθ\Delta\theta, Δφ\Delta\varphi is almost a box with edges:

radial:Δρalong a meridian:ρ Δφ(arc of a circle of radius ρ)along a parallel:ρsin⁡φ Δθ(arc of a circle of radius r=ρsin⁡φ)ΔV≈ρ2sin⁡φ Δρ Δθ Δφ  ⟹  dV=ρ2sin⁡φ dρ dθ dφ\begin{aligned} &\text{radial:} && \Delta\rho \\ &\text{along a meridian:} && \rho\,\Delta\varphi && \text{(arc of a circle of radius } \rho\text{)} \\ &\text{along a parallel:} && \rho \sin\varphi\,\Delta\theta && \text{(arc of a circle of radius } r = \rho \sin\varphi\text{)} \\ &\Delta V \approx \rho^2 \sin\varphi\,\Delta\rho\,\Delta\theta\,\Delta\varphi \qquad \implies \qquad dV = \rho^2 \sin\varphi\,d\rho\,d\theta\,d\varphi \end{aligned}

The parallels shrink toward the poles (sin⁡φ→0\sin\varphi \to 0), so wedges near the poles are thin. The same factor appears as the absolute value of the Jacobian determinant, which the code below checks.

python · try it yourself
import sympy as sp

rho, th, ph = sp.symbols("rho theta phi", positive=True)
X = sp.Matrix([rho * sp.sin(ph) * sp.cos(th), rho * sp.sin(ph) * sp.sin(th), rho * sp.cos(ph)])
J = X.jacobian([rho, th, ph])
print("det J =", sp.simplify(J.det()))

R = sp.symbols("R", positive=True)
ball = sp.integrate(rho**2 * sp.sin(ph), (rho, 0, R), (th, 0, 2 * sp.pi), (ph, 0, sp.pi))
print("volume of a ball:", ball)
det J = -rho**2*sin(phi)
volume of a ball: 4*pi*R**3/3

The determinant is negative because the ordering (ρ,θ,φ)(\rho, \theta, \varphi) is a left-handed one; only the absolute value ρ2sin⁡φ\rho^2 \sin\varphi matters for volume (in the order (ρ,φ,θ)(\rho, \varphi, \theta) the sign is positive).

∭Ef dV=∫cd∫αβ∫abf(ρsin⁡φcos⁡θ,ρsin⁡φsin⁡θ,ρcos⁡φ) ρ2sin⁡φ dρ dθ dφ\iiint_E f\,dV = \int_c^d \int_\alpha^\beta \int_a^b f(\rho \sin\varphi \cos\theta, \rho \sin\varphi \sin\theta, \rho \cos\varphi)\,\rho^2 \sin\varphi\,d\rho\,d\theta\,d\varphi

for a spherical wedge E={a≤ρ≤b, α≤θ≤β, c≤φ≤d}E = \{a \le \rho \le b,\ \alpha \le \theta \le \beta,\ c \le \varphi \le d\}; ρ\rho may also run between functions of θ\theta and φ\varphi.

Example: the ice-cream cone again

The solid above the cone z=x2+y2z = \sqrt{x^2 + y^2} and inside the sphere x2+y2+z2=8x^2 + y^2 + z^2 = 8 (computed with cylindrical coordinates in the previous lesson) is in spherical coordinates simply

0≤ρ≤22,0≤θ≤2π,0≤φ≤π/4V=∫0π/4∫02π∫022ρ2sin⁡φ dρ dθ dφ=(22)33⋅2π⋅(1−cos⁡π4)=1623⋅2π⋅(1−22)=32π3(2−1)≈13.88\begin{aligned} &0 \le \rho \le 2\sqrt{2}, \quad 0 \le \theta \le 2\pi, \quad 0 \le \varphi \le \pi/4 \\ V &= \int_0^{\pi/4} \int_0^{2\pi} \int_0^{2\sqrt{2}} \rho^2 \sin\varphi\,d\rho\,d\theta\,d\varphi \\ &= \frac{(2\sqrt{2})^3}{3} \cdot 2\pi \cdot \left(1 - \cos\frac{\pi}{4}\right) = \frac{16\sqrt{2}}{3} \cdot 2\pi \cdot \left(1 - \frac{\sqrt{2}}{2}\right) = \frac{32\pi}{3} (\sqrt{2} - 1) \approx 13.88 \end{aligned}

The cone z=rz = r is φ=π/4\varphi = \pi/4 because tan⁡φ=r/z=1\tan\varphi = r/z = 1.

Example: a radial density

A planet of radius RR has density ρm(ρ)=k(2−ρ/R)\rho_m(\rho) = k(2 - \rho/R) (denser at the centre). Its mass:

m=∫0π∫02π∫0Rk(2−ρ/R) ρ2sin⁡φ dρ dθ dφ=4πk(2R33−R34)=53πkR3m = \int_0^{\pi} \int_0^{2\pi} \int_0^R k(2 - \rho/R)\,\rho^2 \sin\varphi\,d\rho\,d\theta\,d\varphi = 4\pi k \left(\frac{2R^3}{3} - \frac{R^3}{4}\right) = \frac{5}{3} \pi k R^3

Whenever the integrand depends only on ρ\rho, the angles contribute the factor 4π4\pi and the integral becomes ∫f(ρ)⋅4πρ2 dρ\int f(\rho) \cdot 4\pi\rho^2\,d\rho: the ball is a stack of shells of area 4πρ24\pi\rho^2 and thickness dρd\rho.

In software: random directions and environment maps

Uniform points on a sphere. A random direction (for scattering, ambient occlusion, particle bursts, random rotations) should be uniform with respect to area on the unit sphere, whose element is dA=sin⁡φ dφ dθdA = \sin\varphi\,d\varphi\,d\theta. Choosing φ\varphi uniformly in [0,π][0, \pi] ignores the sin⁡φ\sin\varphi and crowds points at the poles. The correct recipe follows from the fact that the area between heights z1z_1 and z2z_2 is 2π(z2−z1)2\pi(z_2 - z_1) (Archimedes' hat-box theorem): pick z=cos⁡φz = \cos\varphi uniform in [−1,1][-1, 1] and θ\theta uniform in [0,2π)[0, 2\pi), or normalise a vector of three independent standard normals.

python · try it yourself
import numpy as np

rng = np.random.default_rng(2024)
N = 100_000

def cap_fraction(z):        # share of points within 30° of the north pole (exact: (1 - cos30°)/2)
    return float(np.mean(z > np.cos(np.radians(30))))

phi_naive = rng.uniform(0, np.pi, N)                 # wrong: uniform angle
z_naive = np.cos(phi_naive)
z_good = rng.uniform(-1, 1, N)                       # right: uniform height
g = rng.standard_normal((N, 3))
z_gauss = (g / np.linalg.norm(g, axis=1, keepdims=True))[:, 2]   # right: normalised Gaussian

print(f"exact cap share       : {(1 - np.cos(np.radians(30))) / 2:.4f}")
print(f"phi uniform (naive)   : {cap_fraction(z_naive):.4f}")
print(f"cos(phi) uniform      : {cap_fraction(z_good):.4f}")
print(f"normalised Gaussian   : {cap_fraction(z_gauss):.4f}")
exact cap share       : 0.0670
phi uniform (naive)   : 0.1658
cos(phi) uniform      : 0.0661
normalised Gaussian   : 0.0681

The naive method puts 16\frac{1}{6} of the points (30∘30^\circ out of 180∘180^\circ) in a cap that holds only 6.7% of the area: 2.5 times too many.

Environment maps. An equirectangular (latitude-longitude) environment map stores the sky as a W×HW \times H image indexed by (θ,φ)(\theta, \varphi). Each texel spans Δθ=2π/W\Delta\theta = 2\pi/W and Δφ=π/H\Delta\varphi = \pi/H, but its solid angle is sin⁡φ Δθ Δφ\sin\varphi\,\Delta\theta\,\Delta\varphi, tiny near the poles. Image-based lighting must weight each texel by sin⁡φ\sin\varphi when integrating the light (for an irradiance map or a sky's average colour), or the poles count far too much. The weights add up to (almost exactly, as a midpoint sum) 4π4\pi, the sphere's total solid angle.

python · try it yourself
import numpy as np

W, H = 64, 32
phi = (np.arange(H) + 0.5) * np.pi / H               # texel-centre polar angles
dOmega = np.sin(phi) * (2 * np.pi / W) * (np.pi / H)  # solid angle of one texel in each row
print("total solid angle:", round(float(dOmega.sum() * W), 5), " 4π =", round(4 * np.pi, 5))
print("polar row / equator row texel size:", round(float(dOmega[0] / dOmega[H // 2]), 4))

# average brightness of a sky that is 1 in the upper hemisphere (z > 0) and 0 below
sky = (np.cos(phi) > 0).astype(float)[:, None] * np.ones((H, W))
print("weighted average :", round(float((sky * dOmega[:, None]).sum() / (4 * np.pi)), 4))
print("plain pixel mean :", round(float(sky.mean()), 4))
total solid angle: 12.57142  4π = 12.56637
polar row / equator row texel size: 0.0491
weighted average : 0.5002
plain pixel mean : 0.5

Here the plain mean happens to be right by symmetry (the weighted sum is off only by the small midpoint-rule error); for a sky that is bright only near the zenith (exercise 4), it is badly wrong.

Exercises

Exercise 1: Convert

Convert (x,y,z)=(0,23,−2)(x, y, z) = (0, 2\sqrt{3}, -2) to spherical coordinates.

Answer

(ρ,θ,φ)=(4,π2,2π3)(\rho, \theta, \varphi) = \left(4, \frac{\pi}{2}, \frac{2\pi}{3}\right).

Solution
ρ=0+12+4=4cos⁡φ=z/ρ=−12  ⟹  φ=2π3θ=atan2⁡(23,0)=π2\begin{aligned} &\rho = \sqrt{0 + 12 + 4} = 4 \\ &\cos\varphi = z/\rho = -\tfrac{1}{2} \implies \varphi = \frac{2\pi}{3} \\ &\theta = \operatorname{atan2}(2\sqrt{3}, 0) = \frac{\pi}{2} \end{aligned}

Exercise 2: A spherical shell

Evaluate ∭E1x2+y2+z2 dV\iiint_E \frac{1}{\sqrt{x^2 + y^2 + z^2}}\,dV, where EE is the spherical shell 1≤x2+y2+z2≤41 \le x^2 + y^2 + z^2 \le 4.

Answer

6π≈18.856\pi \approx 18.85.

Solution
∫0π∫02π∫121ρ ρ2sin⁡φ dρ dθ dφ=4π⋅[ρ22]12=4π⋅32=6π\int_0^{\pi} \int_0^{2\pi} \int_1^2 \frac{1}{\rho}\,\rho^2 \sin\varphi\,d\rho\,d\theta\,d\varphi = 4\pi \cdot \left[\frac{\rho^2}{2}\right]_1^2 = 4\pi \cdot \frac{3}{2} = 6\pi

Exercise 3: A cone cap

Find the volume of the part of the ball ρ≤3\rho \le 3 that lies inside the cone φ=π/3\varphi = \pi/3.

Answer

9π≈28.279\pi \approx 28.27.

Solution
V=∫0π/3∫02π∫03ρ2sin⁡φ dρ dθ dφ=9⋅2π⋅(1−cos⁡π3)=18π⋅12=9πV = \int_0^{\pi/3} \int_0^{2\pi} \int_0^3 \rho^2 \sin\varphi\,d\rho\,d\theta\,d\varphi = 9 \cdot 2\pi \cdot \left(1 - \cos\frac{\pi}{3}\right) = 18\pi \cdot \frac{1}{2} = 9\pi

Exercise 4: Zenith sky (code)

A sky has brightness L=cos⁡2φL = \cos^2\varphi in the upper hemisphere and 0 below. Compute its exact average over all directions, 14π∬L dΩ\frac{1}{4\pi} \iint L\,d\Omega, and compare a 64×3264 \times 32 equirectangular map averaged with sin⁡φ\sin\varphi weights and without.

Answer

Exact average 16≈0.1667\frac{1}{6} \approx 0.1667. The weighted map gives ≈0.1668\approx 0.1668; the plain pixel mean gives about 0.25, 50% too bright.

Solution
14π∫02π∫0π/2cos⁡2φsin⁡φ dφ dθ=14π⋅2π⋅13=16\frac{1}{4\pi} \int_0^{2\pi} \int_0^{\pi/2} \cos^2\varphi \sin\varphi\,d\varphi\,d\theta = \frac{1}{4\pi} \cdot 2\pi \cdot \frac{1}{3} = \frac{1}{6}
python · try it yourself
import numpy as np
W, H = 64, 32
phi = (np.arange(H) + 0.5) * np.pi / H
L = np.where(phi < np.pi / 2, np.cos(phi)**2, 0.0)
w = np.sin(phi)
print(round(float((L * w).sum() / w.sum()), 4), round(float(L.mean()), 4))
0.1668 0.25

Test yourself

What is the volume element in spherical coordinates?

  1. ρ dρ dθ dφ\rho\,d\rho\,d\theta\,d\varphi
  2. ρ2sin⁡φ dρ dθ dφ\rho^2 \sin\varphi\,d\rho\,d\theta\,d\varphi
  3. ρ2cos⁡φ dρ dθ dφ\rho^2 \cos\varphi\,d\rho\,d\theta\,d\varphi
  4. sin⁡φ dρ dθ dφ\sin\varphi\,d\rho\,d\theta\,d\varphi

To sample uniformly random directions, you can take θ\theta uniform in [0,2π)[0, 2\pi) and…

  1. φ\varphi uniform in [0,π][0, \pi]
  2. cos⁡φ\cos\varphi uniform in [−1,1][-1, 1]
  3. φ\varphi uniform in [0,π/2][0, \pi/2]
  4. sin⁡φ\sin\varphi uniform in [0,1][0, 1]

In spherical coordinates, φ=π/4\varphi = \pi/4 describes…

  1. a sphere
  2. a plane through the zz-axis
  3. a cone around the zz-axis
  4. a cylinder