Double and Triple Integrals
A single integral adds up a quantity along an interval. A double integral adds it up over a region of the plane, and a triple integral over a solid in space. They give volumes, masses, averages and probabilities, and they are computed as one ordinary integral inside another.
The double integral as a volume
For on a region , the double integral is the volume between the surface and the region in the xy-plane. It is defined like a Riemann sum: chop into small rectangles of area , multiply each by the height at a sample point, add, and let the rectangles shrink:
With the integral is simply the area of . Dividing by the area gives the average value of over .
Iterated integrals (Fubini)
On a rectangle , , the double integral can be computed as two single integrals, in either order:
Do the inner integral treating the outer variable as a constant (like a partial derivative in reverse), then integrate the result.
Example. over , :
Non-rectangular regions
If lies between two curves, for , the inner limits become functions:
Sketch the region first. Sometimes swapping the order of integration turns an impossible inner integral into an easy one.
import sympy as sp
x, y = sp.symbols('x y')
# rectangle example from above
print("rectangle:", sp.integrate(x + 2*y, (y, 0, 1), (x, 0, 2)))
# region between y = x² and y = x (0 <= x <= 1), integrand f = x*y
D1 = sp.integrate(x*y, (y, x**2, x), (x, 0, 1))
D2 = sp.integrate(x*y, (x, y, sp.sqrt(y)), (y, 0, 1)) # same region, other order
print("between curves, dy dx:", D1, " dx dy:", D2)
print("area of that region :", sp.integrate(1, (y, x**2, x), (x, 0, 1)))rectangle: 4
between curves, dy dx: 1/24 dx dy: 1/24
area of that region : 1/6
SymPy's integrate(f, (y, …), (x, …)) integrates in the order listed: the first tuple is the inner integral.
Polar coordinates
For discs, rings and sectors, use , . A small polar "rectangle" has area , not , because the arc length of a wedge grows with . That factor is the Jacobian determinant of the change of variables:
A famous case is the Gaussian over the whole plane, . Taking the square root gives over the real line, the constant inside the normal distribution.
Triple integrals
A triple integral adds over a solid . With it is the volume; with it is the mass. It is computed as three nested integrals. Cylindrical () and spherical () coordinates handle round solids.
import sympy as sp
x, y, z, r, th, rho, phi = sp.symbols('x y z r theta rho phi', nonnegative=True)
gauss = sp.integrate(sp.exp(-r**2) * r, (r, 0, sp.oo), (th, 0, 2*sp.pi))
print("Gaussian over the plane:", gauss)
# volume of the solid under z = 4 - x^2 - y^2 above the xy-plane (polar)
print("paraboloid volume:", sp.integrate((4 - r**2) * r, (r, 0, 2), (th, 0, 2*sp.pi)))
# volume of a ball of radius R (spherical)
R = sp.symbols('R', positive=True)
print("ball volume:", sp.integrate(rho**2 * sp.sin(phi), (rho, 0, R), (phi, 0, sp.pi), (th, 0, 2*sp.pi)))
# mass of the unit cube with density x + y + z
print("cube mass:", sp.integrate(x + y + z, (x, 0, 1), (y, 0, 1), (z, 0, 1)))Gaussian over the plane: pi
paraboloid volume: 8*pi
ball volume: 4*pi*R**3/3
cube mass: 3/2
Monte Carlo integration
In many dimensions, or over awkward regions, nested formulas become impractical. Monte Carlo integration samples random points, averages at them, and multiplies by the volume of the sampling box. Its error shrinks like regardless of the number of dimensions, which is why it rules in high dimensions. See also Numerical Integration.
import numpy as np
rng = np.random.default_rng(42)
N = 200_000
pts = rng.uniform(-1, 1, size=(N, 3)) # box [-1, 1]^3, volume 8
inside = np.sum(pts**2, axis=1) <= 1
print("ball volume ≈", round(8 * inside.mean(), 4), " exact", round(4 / 3 * np.pi, 4))
# mass of the ball with density 1 + z^2
f = np.where(inside, 1 + pts[:, 2]**2, 0.0)
print("ball mass ≈", round(8 * f.mean(), 4), " exact", round(4 / 3 * np.pi + 4 * np.pi / 15, 4))ball volume ≈ 4.2017 exact 4.1888
ball mass ≈ 5.0462 exact 5.0265
In software
- Rendering. The light reaching a pixel is an integral over the pixel's area, the lens, the directions of incoming light and time. Path tracers estimate it with Monte Carlo, which is why their images start noisy and clean up as samples accumulate.
- Probability and statistics. For two random variables with joint density , probabilities are double integrals: . Expected values are integrals of .
- Physics and games. Mass, centre of mass and moment of inertia of a body are volume integrals; engines compute them once per shape to set up rigid-body dynamics (Rotation Dynamics).
Exercises
Exercise 1: Rectangle
Compute over , .
Answer
Solution
Exercise 2: Triangle
Find where is the triangle with corners (0, 0), (2, 0), (0, 2).
Answer
4/3.
Solution
The triangle is , .
Exercise 3: Polar area
Use polar coordinates to find the area of the ring .
Answer
.
Solution
Check: .
Exercise 4: Monte Carlo check (code)
Estimate over the square by Monte Carlo with 100 000 points (seed 1), and compare with (the tails outside the square are tiny).
Answer
The estimate is about 3.155, within about 0.014 of (Monte Carlo error with is of that order).
Solution
The box has area 36, so the estimate is .
import numpy as np
rng = np.random.default_rng(1)
p = rng.uniform(-3, 3, size=(100_000, 2))
est = 36 * np.mean(np.exp(-(p**2).sum(axis=1)))
print(round(float(est), 4), round(float(np.pi), 4))3.1551 3.1416
Test yourself
In polar coordinates, equals…
What does compute?
- The perimeter of
- The area of
- The volume above
- Zero
Why is Monte Carlo integration popular in high dimensions?
- It is exact
- Its error shrinks like regardless of dimension
- It needs no random numbers
- It only works in 3D