TExcelTAD
Home › Calculus in Depth › Double Integrals over Rectangles

Double Integrals over Rectangles

A single integral adds up f(x) dxf(x)\,dx over an interval; a double integral adds up f(x,y) dAf(x, y)\,dA 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 ff 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 R=[a,b]×[c,d]={(x,y):a≤x≤b, c≤y≤d}R = [a, b] \times [c, d] = \{(x, y) : a \le x \le b,\ c \le y \le d\} and let f(x,y)≥0f(x, y) \ge 0 on RR. The solid SS lying above RR and below the surface z=f(x,y)z = f(x, y) has some volume VV. To estimate it:

  1. Divide [a,b][a, b] into mm subintervals of width Δx=(b−a)/m\Delta x = (b - a)/m and [c,d][c, d] into nn subintervals of width Δy=(d−c)/n\Delta y = (d - c)/n. This cuts RR into mnmn sub-rectangles RijR_{ij}, each of area ΔA=Δx Δy\Delta A = \Delta x\,\Delta y.
  2. Pick a sample point (xij∗,yij∗)(x_{ij}^*, y_{ij}^*) in each RijR_{ij}.
  3. Approximate the thin column above RijR_{ij} by a box of height f(xij∗,yij∗)f(x_{ij}^*, y_{ij}^*).
V≈∑i=1m∑j=1nf(xij∗,yij∗) ΔA(a double Riemann sum)V \approx \sum_{i=1}^{m} \sum_{j=1}^{n} f(x_{ij}^*, y_{ij}^*)\,\Delta A \qquad \text{(a double Riemann sum)}

Definition

The double integral of ff over RR is

∬Rf(x,y) dA=lim⁡m,n→∞∑i∑jf(xij∗,yij∗) ΔA\iint_R f(x, y)\,dA = \lim_{m, n \to \infty} \sum_{i} \sum_{j} f(x_{ij}^*, y_{ij}^*)\,\Delta A

if the limit exists and is the same for every choice of sample points. Then ff is called integrable on RR. Precisely: for every ε>0\varepsilon > 0 there is NN such that every double Riemann sum with m,n>Nm, n > N, whatever the sample points, lies within ε\varepsilon of the integral.

Theorem (integrability). If ff is continuous on the closed rectangle RR, it is integrable. More generally it suffices that ff 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 f≥0f \ge 0, the integral is the volume of SS. When ff changes sign, it is the volume above the xyxy-plane minus the volume below it.

The midpoint rule

Choosing the centre (xˉi,yˉj)(\bar{x}_i, \bar{y}_j) of each sub-rectangle gives the midpoint rule:

∬Rf dA≈∑i∑jf(xˉi,yˉj) ΔA\iint_R f\,dA \approx \sum_{i} \sum_{j} f(\bar{x}_i, \bar{y}_j)\,\Delta A

For a smooth ff its error shrinks like 1/m2+1/n21/m^2 + 1/n^2, just like the one-dimensional midpoint rule, because the linear part of ff cancels around each centre.

Take f(x,y)=x2+xyf(x, y) = x^2 + xy on R=[0,2]×[0,1]R = [0, 2] \times [0, 1]. The exact value (computed in Iterated Integrals) is 8/3+1=11/3≈3.66678/3 + 1 = 11/3 \approx 3.6667.

python · try it yourself
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 mm and nn divides the error by 4: second-order convergence. Here the error is exactly 2/(3m2)2/(3m^2): the midpoint rule integrates the bilinear term xyxy exactly, so only x2x^2 contributes, with the one-dimensional midpoint error (b−a)⋅Δx2⋅f′′/24=2⋅(2/m)2⋅2/24(b - a) \cdot \Delta x^2 \cdot f''/24 = 2 \cdot (2/m)^2 \cdot 2/24.

Average value

The average value of ff over RR, with A(R)=(b−a)(d−c)A(R) = (b - a)(d - c), is

favg=1A(R)∬Rf(x,y) dAf_{\text{avg}} = \frac{1}{A(R)} \iint_R f(x, y)\,dA

So the integral equals favg×areaf_{\text{avg}} \times \text{area}: the solid has the same volume as a flat box of height favgf_{\text{avg}} over RR. For a temperature field T(x,y)T(x, y) on a metal plate, favgf_{\text{avg}} is the mean temperature; for a terrain heightmap it is the mean elevation.

Properties

For integrable ff, gg on RR and a constant cc:

Property Statement
linearity ∬(f+g) dA=∬f dA+∬g dA\iint (f + g)\,dA = \iint f\,dA + \iint g\,dA, ∬cf dA=c∬f dA\iint c f\,dA = c \iint f\,dA
comparison f≤gf \le g on RR ⟹\Longrightarrow ∬f dA≤∬g dA\iint f\,dA \le \iint g\,dA
bounds m≤f≤Mm \le f \le M on RR ⟹\Longrightarrow m⋅A(R)≤∬f dA≤M⋅A(R)m \cdot A(R) \le \iint f\,dA \le M \cdot A(R)
additivity R=R1∪R2R = R_1 \cup R_2 (overlapping only on an edge) ⟹\Longrightarrow ∬R=∬R1+∬R2\iint_R = \iint_{R_1} + \iint_{R_2}
constant ∬R1 dA=A(R)\iint_R 1\,dA = A(R)

They follow from the same facts about Riemann sums. The bounds give quick sanity checks: for f=x2+xyf = x^2 + xy on [0,2]×[0,1][0, 2] \times [0, 1], 0≤f≤60 \le f \le 6, so 0≤∬f dA≤120 \le \iint f\,dA \le 12, and 11/311/3 is comfortably inside.

Monte Carlo integration

Another way to estimate the integral is to sample NN random points uniformly in RR and use

∬Rf dA≈A(R)⋅1N∑kf(xk,yk)\iint_R f\,dA \approx A(R) \cdot \frac{1}{N} \sum_{k} f(x_k, y_k)

This is the area times the sample mean of ff, a direct estimate of favg×A(R)f_{\text{avg}} \times A(R). By the central limit theorem its typical error is A(R)⋅σ/NA(R) \cdot \sigma/\sqrt{N}, where σ\sigma is the standard deviation of ff over RR. That rate, 1/N1/\sqrt{N}, is slow compared with the midpoint rule's 1/N1/N for N=m2N = m^2 points in 2D, but it does not depend on the dimension, as the next comparison shows.

python · try it yourself
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 mm points per axis in dd dimensions needs mdm^d evaluations and has error ∼1/m2=N−2/d\sim 1/m^2 = N^{-2/d}. Monte Carlo has error ∼N−1/2\sim N^{-1/2} 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.

NN evaluations midpoint grid, 2D midpoint grid, 10D Monte Carlo, any dd
10410^4 error ∼10−4\sim 10^{-4} 2.5 points per axis, useless error ∼10−2\sim 10^{-2}
10610^6 ∼10−6\sim 10^{-6} 4 points per axis ∼10−3\sim 10^{-3}
  • 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 1/N1/\sqrt{N} error made visible.
  • Image processing. Box filtering an image is a double integral over a rectangle. A summed-area table (integral image) stores S(i,j)=sum of all pixels above and left of (i,j)S(i, j) = \text{sum of all pixels above and left of } (i, j); then the sum over any rectangle is S(D)−S(B)−S(C)+S(A)S(D) - S(B) - S(C) + S(A), four lookups regardless of size. Mipmapping, Viola–Jones face detection and fast blur filters use it.
  • Libraries. scipy.integrate.dblquad uses adaptive one-dimensional rules nested inside each other; see SciPy integration and Numerical Integration.

Exercises

Exercise 1: A Riemann sum by hand

Estimate ∬R(x+2y) dA\iint_R (x + 2y)\,dA over R=[0,2]×[0,2]R = [0, 2] \times [0, 2] with m=n=2m = n = 2, using the upper-right corner of each square as sample point.

Answer

18 (the exact value is 12).

Solution
ΔA=1⋅1=1,upper-right corners (1,1),(2,1),(1,2),(2,2)f values: 3,4,5,6sum=18,estimate 18⋅ΔA=18\begin{aligned} &\Delta A = 1 \cdot 1 = 1, \quad \text{upper-right corners } (1,1), (2,1), (1,2), (2,2) \\ &f \text{ values: } 3, 4, 5, 6 \qquad \text{sum} = 18, \quad \text{estimate } 18 \cdot \Delta A = 18 \end{aligned}

The exact value is ∬x dA+2∬y dA=4+2⋅4=12\iint x\,dA + 2\iint y\,dA = 4 + 2 \cdot 4 = 12; upper-right corners over-estimate an increasing function.

Exercise 2: Midpoint rule by hand

Use the midpoint rule with m=n=2m = n = 2 to estimate ∬Rxy2 dA\iint_R xy^2\,dA on R=[0,2]×[0,2]R = [0, 2] \times [0, 2].

Answer

5 (exact value 16/3≈5.33316/3 \approx 5.333).

Solution
midpoints: xˉ∈{0.5,1.5}, yˉ∈{0.5,1.5},ΔA=1∑xˉyˉ2=(0.5+1.5)(0.25+2.25)=2⋅2.5=5exact: (∫02x dx)(∫02y2 dy)=2⋅8/3=16/3\begin{aligned} &\text{midpoints: } \bar{x} \in \{0.5, 1.5\},\ \bar{y} \in \{0.5, 1.5\}, \quad \Delta A = 1 \\ &\sum \bar{x}\bar{y}^2 = (0.5 + 1.5)(0.25 + 2.25) = 2 \cdot 2.5 = 5 \\ &\text{exact: } \left(\int_0^2 x\,dx\right)\left(\int_0^2 y^2\,dy\right) = 2 \cdot 8/3 = 16/3 \end{aligned}

The sum factors because the integrand is a product g(x)h(y)g(x)h(y) and the grid is a product grid.

Exercise 3: Bounds

Without integrating, find lower and upper bounds for ∬Rsin⁡(x+y) dA\iint_R \sin(x + y)\,dA on R=[0,π/2]×[0,π/2]R = [0, \pi/2] \times [0, \pi/2] from the range of the integrand. Then compute the exact value.

Answer

0≤∬≤π2/4≈2.4670 \le \iint \le \pi^2/4 \approx 2.467; the exact value is 2.

Solution
0≤x+y≤π⟹0≤sin⁡(x+y)≤1A(R)=(π/2)2=π2/4≈2.467exact: ∫0π/2[−cos⁡(x+y)]0π/2dx=∫0π/2(cos⁡x+sin⁡x) dx=1+1=2\begin{aligned} &0 \le x + y \le \pi \Longrightarrow 0 \le \sin(x + y) \le 1 \\ &A(R) = (\pi/2)^2 = \pi^2/4 \approx 2.467 \\ &\text{exact: } \int_0^{\pi/2} \Big[-\cos(x + y)\Big]_0^{\pi/2} dx = \int_0^{\pi/2} (\cos x + \sin x)\,dx = 1 + 1 = 2 \end{aligned}

Exercise 4: Average value (code)

Estimate the average of f(x,y)=e−(x2+y2)f(x, y) = e^{-(x^2 + y^2)} over R=[−1,1]×[−1,1]R = [-1, 1] \times [-1, 1] with the midpoint rule (m=n=200m = n = 200) and compare with the exact value (πerf⁡(1)/2)2(\sqrt{\pi}\operatorname{erf}(1)/2)^2, where erf is math.erf.

Answer

Midpoint estimate 0.5578, exact value 0.5577.

Solution

The integral is (∫−11e−x2 dx)2=(πerf⁡(1))2\left(\int_{-1}^{1} e^{-x^2}\,dx\right)^2 = (\sqrt{\pi}\operatorname{erf}(1))^2, and dividing by the area 4 gives the average.

python · try it yourself
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 [0,3]×[0,2][0, 3] \times [0, 2] uses m=3m = 3 and n=4n = 4. What is ΔA\Delta A?

  1. 0.5
  2. 1
  3. 6
  4. 0.25

If ff is continuous on RR and −2≤f≤5-2 \le f \le 5 there, and A(R)=3A(R) = 3, the double integral lies in…

  1. [−2,5][-2, 5]
  2. [0,15][0, 15]
  3. [−6,15][-6, 15]
  4. [−6,5][-6, 5]

Why is Monte Carlo preferred for integrals in many dimensions?

  1. It is exact
  2. Its error ∼1/N\sim 1/\sqrt{N} does not get worse with dimension, while grid rules need exponentially many points
  3. It needs no random numbers
  4. It converges like 1/N21/N^2