Double Integrals over Rectangles
A single integral adds up over an interval; a double integral adds up over a region of the plane. Over a rectangle the construction is a direct copy of the Riemann sum: chop the rectangle into small boxes, sample in each, multiply by the box area, add, and refine. This lesson builds the definition, interprets it as volume and as average value, approximates it with the midpoint rule and with Monte Carlo sampling, and lists the properties that make it computable.
The one-variable construction is The Definite Integral; the survey version of this chapter is Multiple Integrals.
The volume problem
Let and let on . The solid lying above and below the surface has some volume . To estimate it:
- Divide into subintervals of width and into subintervals of width . This cuts into sub-rectangles , each of area .
- Pick a sample point in each .
- Approximate the thin column above by a box of height .
Definition
The double integral of over is
if the limit exists and is the same for every choice of sample points. Then is called integrable on . Precisely: for every there is such that every double Riemann sum with , whatever the sample points, lies within of the integral.
Theorem (integrability). If is continuous on the closed rectangle , it is integrable. More generally it suffices that is bounded and its discontinuities lie on finitely many smooth curves: those curves have zero area, so the boxes that touch them contribute nothing in the limit.
When , the integral is the volume of . When changes sign, it is the volume above the -plane minus the volume below it.
The midpoint rule
Choosing the centre of each sub-rectangle gives the midpoint rule:
For a smooth its error shrinks like , just like the one-dimensional midpoint rule, because the linear part of cancels around each centre.
Take on . The exact value (computed in Iterated Integrals) is .
import numpy as np
f = lambda x, y: x**2 + x * y
a, b, c, d = 0.0, 2.0, 0.0, 1.0
exact = 11 / 3
for m in [2, 4, 8, 16, 32]:
n = m
dx, dy = (b - a) / m, (d - c) / n
xm = a + dx * (np.arange(m) + 0.5) # midpoints in x
ym = c + dy * (np.arange(n) + 0.5) # midpoints in y
X, Y = np.meshgrid(xm, ym)
approx = float(f(X, Y).sum() * dx * dy)
print(f"m=n={m:2d} midpoint {approx:.6f} error {exact - approx:.2e}")m=n= 2 midpoint 3.500000 error 1.67e-01
m=n= 4 midpoint 3.625000 error 4.17e-02
m=n= 8 midpoint 3.656250 error 1.04e-02
m=n=16 midpoint 3.664062 error 2.60e-03
m=n=32 midpoint 3.666016 error 6.51e-04
Each doubling of and divides the error by 4: second-order convergence. Here the error is exactly : the midpoint rule integrates the bilinear term exactly, so only contributes, with the one-dimensional midpoint error .
Average value
The average value of over , with , is
So the integral equals : the solid has the same volume as a flat box of height over . For a temperature field on a metal plate, is the mean temperature; for a terrain heightmap it is the mean elevation.
Properties
For integrable , on and a constant :
| Property | Statement |
|---|---|
| linearity | , |
| comparison | on |
| bounds | on |
| additivity | (overlapping only on an edge) |
| constant |
They follow from the same facts about Riemann sums. The bounds give quick sanity checks: for on , , so , and is comfortably inside.
Monte Carlo integration
Another way to estimate the integral is to sample random points uniformly in and use
This is the area times the sample mean of , a direct estimate of . By the central limit theorem its typical error is , where is the standard deviation of over . That rate, , is slow compared with the midpoint rule's for points in 2D, but it does not depend on the dimension, as the next comparison shows.
import numpy as np
rng = np.random.default_rng(7)
f = lambda x, y: x**2 + x * y
A, exact = 2.0, 11 / 3
for N in [100, 10_000, 1_000_000]:
x = rng.uniform(0, 2, N)
y = rng.uniform(0, 1, N)
vals = f(x, y)
est = A * vals.mean()
se = A * vals.std(ddof=1) / np.sqrt(N)
print(f"N={N:>9,} estimate {est:.5f} error {abs(est - exact):.1e} std. error {se:.1e}")N= 100 estimate 3.59576 error 7.1e-02 std. error 3.1e-01
N= 10,000 estimate 3.69236 error 2.6e-02 std. error 3.0e-02
N=1,000,000 estimate 3.66261 error 4.1e-03 std. error 3.0e-03
The standard error is what you can report without knowing the answer; the actual error is a random quantity of about that size.
In software: grids versus random samples
A midpoint grid with points per axis in dimensions needs evaluations and has error . Monte Carlo has error in any dimension. In two dimensions grids win easily; beyond about four dimensions Monte Carlo wins, and in the hundreds of dimensions of a path tracer's light paths or a Bayesian posterior, grids are hopeless.
| evaluations | midpoint grid, 2D | midpoint grid, 10D | Monte Carlo, any |
|---|---|---|---|
| error | 2.5 points per axis, useless | error | |
| 4 points per axis |
- Rendering. The brightness of a pixel is an integral over the pixel area, the lens and incoming light directions. Path tracers estimate it by Monte Carlo; the grainy noise in an under-sampled render is the error made visible.
- Image processing. Box filtering an image is a double integral over a rectangle. A summed-area table (integral image) stores ; then the sum over any rectangle is , four lookups regardless of size. Mipmapping, Viola–Jones face detection and fast blur filters use it.
- Libraries.
scipy.integrate.dblquaduses adaptive one-dimensional rules nested inside each other; see SciPy integration and Numerical Integration.
Exercises
Exercise 1: A Riemann sum by hand
Estimate over with , using the upper-right corner of each square as sample point.
Answer
18 (the exact value is 12).
Solution
The exact value is ; upper-right corners over-estimate an increasing function.
Exercise 2: Midpoint rule by hand
Use the midpoint rule with to estimate on .
Answer
5 (exact value ).
Solution
The sum factors because the integrand is a product and the grid is a product grid.
Exercise 3: Bounds
Without integrating, find lower and upper bounds for on from the range of the integrand. Then compute the exact value.
Answer
; the exact value is 2.
Solution
Exercise 4: Average value (code)
Estimate the average of over with the midpoint rule () and compare with the exact value , where erf is math.erf.
Answer
Midpoint estimate 0.5578, exact value 0.5577.
Solution
The integral is , and dividing by the area 4 gives the average.
import math
import numpy as np
m = 200
c = -1 + (2 / m) * (np.arange(m) + 0.5)
X, Y = np.meshgrid(c, c)
avg = float(np.exp(-(X**2 + Y**2)).mean())
print(round(avg, 4), round((math.sqrt(math.pi) * math.erf(1) / 2)**2, 4))0.5578 0.5577
Averaging the function values on an equal-area grid is the same as the midpoint sum divided by the area.
Test yourself
A double Riemann sum on uses and . What is ?
- 0.5
- 1
- 6
- 0.25
If is continuous on and there, and , the double integral lies in…
Why is Monte Carlo preferred for integrals in many dimensions?
- It is exact
- Its error does not get worse with dimension, while grid rules need exponentially many points
- It needs no random numbers
- It converges like