TExcelTAD
Home › Calculus in Depth › Iterated Integrals

Iterated Integrals

Computing a double integral straight from its Riemann-sum definition is hopeless by hand. Fubini's theorem rescues us: over a rectangle, a double integral equals an iterated integral, one ordinary integral nested inside another, and the order can be swapped. This lesson states the theorem with a proof sketch, shows how products split into two single integrals, and explains when swapping the order fails.

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

It continues Double Integrals over Rectangles.

Partial integration

For f(x,y)f(x, y) on R=[a,b]×[c,d]R = [a, b] \times [c, d], hold xx fixed and integrate in yy:

A(x)=∫cdf(x,y) dyA(x) = \int_c^d f(x, y)\,dy

This is "partial integration", the reverse of partial differentiation: xx is treated as a constant. The result is a function of xx alone, which we then integrate:

∫ab∫cdf(x,y) dy dx=∫ab[∫cdf(x,y) dy]dx\int_a^b \int_c^d f(x, y)\,dy\,dx = \int_a^b \left[ \int_c^d f(x, y)\,dy \right] dx

The inner integral is done first, and the inner differential (dydy) matches the inner limits (cc to dd). The other order is ∫cd∫abf(x,y) dx dy\int_c^d \int_a^b f(x, y)\,dx\,dy.

Example: f(x,y)=x2+xyf(x, y) = x^2 + xy on [0,2]×[0,1][0, 2] \times [0, 1].

y first:∫01(x2+xy) dy=x2+x2,∫02(x2+x2)dx=83+1=113x first:∫02(x2+xy) dx=83+2y,∫01(83+2y)dy=83+1=113\begin{aligned} &y \text{ first:} && \int_0^1 (x^2 + xy)\,dy = x^2 + \frac{x}{2}, && \int_0^2 \left(x^2 + \frac{x}{2}\right) dx = \frac{8}{3} + 1 = \frac{11}{3} \\ &x \text{ first:} && \int_0^2 (x^2 + xy)\,dx = \frac{8}{3} + 2y, && \int_0^1 \left(\frac{8}{3} + 2y\right) dy = \frac{8}{3} + 1 = \frac{11}{3} \end{aligned}

Both orders give 113\frac{11}{3}, the value the midpoint rule approached in the previous lesson.

Fubini's theorem

Theorem. If ff is continuous on R=[a,b]×[c,d]R = [a, b] \times [c, d], then

∬Rf(x,y) dA=∫ab∫cdf(x,y) dy dx=∫cd∫abf(x,y) dx dy\iint_R f(x, y)\,dA = \int_a^b \int_c^d f(x, y)\,dy\,dx = \int_c^d \int_a^b f(x, y)\,dx\,dy

More generally it holds if ff is bounded on RR, is discontinuous only on finitely many smooth curves, and the iterated integrals exist.

Why it is true (slicing). For f≥0f \ge 0, A(x)=∫cdf(x,y) dyA(x) = \int_c^d f(x, y)\,dy is the area of the cross-section of the solid at position xx. The volume of a solid with known cross-sectional areas is ∫abA(x) dx\int_a^b A(x)\,dx, the slicing method for volumes. Slicing perpendicular to the yy-axis instead gives the other order.

Why it is true (sums). Take a midpoint Riemann sum and group its terms by column: ∑i[∑jf(xˉi,yˉj) Δy]Δx\sum_i \left[\sum_j f(\bar{x}_i, \bar{y}_j)\,\Delta y\right] \Delta x. The inner bracket is a Riemann sum for A(xˉi)A(\bar{x}_i), and the outer sum is a Riemann sum for ∫A(x) dx\int A(x)\,dx. Continuity on the compact rectangle makes ff uniformly continuous, which lets both limits be taken together. Grouping by rows gives the other order. A finite double sum can always be added in either order; Fubini says the same is true in the limit.

Separable integrands

If f(x,y)=g(x) h(y)f(x, y) = g(x)\,h(y), the inner integral pulls the constant factor out:

∬Rg(x) h(y) dA=(∫abg(x) dx)⋅(∫cdh(y) dy)\iint_R g(x)\,h(y)\,dA = \left( \int_a^b g(x)\,dx \right) \cdot \left( \int_c^d h(y)\,dy \right)

Example: ∬ over [0,π/2]×[0,1] of yey2cos⁡x dA=(∫0π/2cos⁡x dx)(∫01yey2 dy)=1⋅e−12≈0.859\iint \text{ over } [0, \pi/2] \times [0, 1] \text{ of } y e^{y^2} \cos x\,dA = \left(\int_0^{\pi/2} \cos x\,dx\right)\left(\int_0^1 y e^{y^2}\,dy\right) = 1 \cdot \frac{e - 1}{2} \approx 0.859.

This is the multivariable version of "independent pieces multiply", and it is exactly why the joint density of independent random variables factorises.

python · try it yourself
import sympy as sp

x, y = sp.symbols("x y")

f = x**2 + x * y
print("dy dx:", sp.integrate(sp.integrate(f, (y, 0, 1)), (x, 0, 2)))
print("dx dy:", sp.integrate(sp.integrate(f, (x, 0, 2)), (y, 0, 1)))

g = y * sp.exp(y**2) * sp.cos(x)
both = sp.integrate(g, (y, 0, 1), (x, 0, sp.pi / 2))
split = sp.integrate(sp.cos(x), (x, 0, sp.pi / 2)) * sp.integrate(y * sp.exp(y**2), (y, 0, 1))
print("separable:", both, "=", split, "≈", round(float(both), 4))
dy dx: 11/3
dx dy: 11/3
separable: -1/2 + E/2 = -1/2 + E/2 ≈ 0.8591

SymPy writes Euler's number as E. In sp.integrate(g, (y, …), (x, …)) the first tuple is the inner integral.

Choosing the order

Fubini says the orders agree, but they need not be equally easy. For ∬ over [0,1]×[0,1] of xexy dA\iint \text{ over } [0, 1] \times [0, 1] \text{ of } x e^{xy}\,dA:

  • xx first: ∫xexy dx\int x e^{xy}\,dx needs integration by parts and leaves a messy function of yy.
  • yy first: ∫01xexy dy=[exy]01=ex−1\int_0^1 x e^{xy}\,dy = \left[e^{xy}\right]_0^1 = e^x - 1, and ∫01(ex−1) dx=e−2\int_0^1 (e^x - 1)\,dx = e - 2.

Rule of thumb: integrate first in the variable that makes the inner integral an obvious antiderivative.

python · try it yourself
from scipy import integrate
import math

f = lambda y, x: x * math.exp(x * y)          # dblquad wants f(inner, outer)
val, err = integrate.dblquad(f, 0, 1, 0, 1)   # x from 0 to 1, y from 0 to 1
print(f"dblquad: {val:.12f}  (error estimate {err:.1e})")
print(f"e - 2  : {math.e - 2:.12f}")
dblquad: 0.718281828459  (error estimate 1.9e-14)
e - 2  : 0.718281828459

When the order matters

Fubini needs integrability. Without it, the two iterated integrals can exist and disagree. The standard example is

f(x,y)=x2−y2(x2+y2)2on (0,1]×(0,1]f(x, y) = \frac{x^2 - y^2}{(x^2 + y^2)^2} \qquad \text{on } (0, 1] \times (0, 1]

which is unbounded near the origin. Since ∂∂y[yx2+y2]=f\frac{\partial}{\partial y}\left[\frac{y}{x^2 + y^2}\right] = f, the inner yy-integral is 11+x2\frac{1}{1 + x^2}, and

∫01∫01f dy dx=∫0111+x2 dx=π4∫01∫01f dx dy=−π4(by the antisymmetry f(y,x)=−f(x,y))\begin{aligned} \int_0^1 \int_0^1 f\,dy\,dx &= \int_0^1 \frac{1}{1 + x^2}\,dx = \frac{\pi}{4} \\ \int_0^1 \int_0^1 f\,dx\,dy &= -\frac{\pi}{4} && \text{(by the antisymmetry } f(y, x) = -f(x, y)\text{)} \end{aligned}

The double integral of ∣f∣\lvert f \rvert is infinite, so ff is not integrable and Fubini does not apply. (The companion result, Tonelli's theorem, says the order never matters for f≥0f \ge 0, even if the value is +∞+\infty.)

python · try it yourself
import sympy as sp

x, y = sp.symbols("x y", positive=True)
f = (x**2 - y**2) / (x**2 + y**2)**2
inner_y = sp.simplify(sp.integrate(f, (y, 0, 1)))
inner_x = sp.simplify(sp.integrate(f, (x, 0, 1)))
print("inner dy:", inner_y, "  outer ->", sp.integrate(inner_y, (x, 0, 1)))
print("inner dx:", inner_x, "  outer ->", sp.integrate(inner_x, (y, 0, 1)))
inner dy: 1/(x**2 + 1)   outer -> pi/4
inner dx: -1/(y**2 + 1)   outer -> -pi/4

In software

  • Nested quadrature. scipy.integrate.dblquad and nquad are Fubini in code: an adaptive one-dimensional rule for the outer variable, calling another adaptive rule for the inner one. The cost multiplies with every level of nesting, which is why they are fine in 2D or 3D and impractical beyond. Note the argument order: dblquad's integrand is f(y, x), inner variable first, a classic source of bugs. See SciPy integration.
  • Vectorised separable sums. When an integrand factorises, two 1D sums replace one 2D sum: O(m+n)O(m + n) instead of O(mn)O(mn). Separable image filters (a 2D Gaussian blur as a horizontal pass followed by a vertical pass) use exactly this.
  • Swapping sums in code. Changing the loop order of a nested sum is the discrete Fubini; it is always valid for finite sums, but for long floating-point sums the order changes the rounding error slightly.

Exercises

Exercise 1: Compute both orders

Evaluate ∫12∫03(2x−y2) dx dy\int_1^2 \int_0^3 (2x - y^2)\,dx\,dy, then in the other order.

Answer

2 in both orders.

Solution
inner dx:∫03(2x−y2) dx=9−3y2outer dy:∫12(9−3y2) dy=9−(8−1)=2other order:∫12(2x−y2) dy=2x−73,∫03(2x−73)dx=9−7=2\begin{aligned} &\text{inner } dx\text{:} && \int_0^3 (2x - y^2)\,dx = 9 - 3y^2 \\ &\text{outer } dy\text{:} && \int_1^2 (9 - 3y^2)\,dy = 9 - (8 - 1) = 2 \\ &\text{other order:} && \int_1^2 (2x - y^2)\,dy = 2x - \frac{7}{3}, \qquad \int_0^3 \left(2x - \frac{7}{3}\right) dx = 9 - 7 = 2 \end{aligned}

Exercise 2: Separable

Evaluate ∬Rxe−y dA\iint_R x e^{-y}\,dA over R=[0,4]×[0,ln⁡3]R = [0, 4] \times [0, \ln 3].

Answer

163≈5.333\frac{16}{3} \approx 5.333.

Solution
(∫04x dx)(∫0ln⁡3e−y dy)=8⋅(1−13)=163\left(\int_0^4 x\,dx\right)\left(\int_0^{\ln 3} e^{-y}\,dy\right) = 8 \cdot \left(1 - \frac{1}{3}\right) = \frac{16}{3}

Exercise 3: Pick the easy order

Evaluate ∬Rxsin⁡(xy) dA\iint_R x \sin(xy)\,dA over R=[0,1]×[0,π]R = [0, 1] \times [0, \pi] (xx from 0 to 1, yy from 0 to π\pi).

Answer
Solution

Integrating in xx first needs integration by parts; yy first is immediate because xx is the derivative of xyxy with respect to yy.

∫0πxsin⁡(xy) dy=[−cos⁡(xy)]y=0y=π=1−cos⁡(πx)∫01(1−cos⁡πx) dx=1−[sin⁡(πx)π]01=1−0=1\begin{aligned} \int_0^{\pi} x \sin(xy)\,dy &= \Big[-\cos(xy)\Big]_{y=0}^{y=\pi} = 1 - \cos(\pi x) \\ \int_0^1 (1 - \cos \pi x)\,dx &= 1 - \left[\frac{\sin(\pi x)}{\pi}\right]_0^1 = 1 - 0 = 1 \end{aligned}

Exercise 4: Check with SymPy (code)

Evaluate ∬R1(1+x+y)2 dA\iint_R \frac{1}{(1 + x + y)^2}\,dA over R=[0,1]×[0,2]R = [0, 1] \times [0, 2] by hand, then confirm both orders with SymPy.

Answer

ln⁡(3/2)≈0.4055\ln(3/2) \approx 0.4055.

Solution
∫02(1+x+y)−2 dy=11+x−13+x∫01[11+x−13+x]dx=ln⁡2−(ln⁡4−ln⁡3)=ln⁡(3/2)\begin{aligned} \int_0^2 (1 + x + y)^{-2}\,dy &= \frac{1}{1 + x} - \frac{1}{3 + x} \\ \int_0^1 \left[\frac{1}{1 + x} - \frac{1}{3 + x}\right] dx &= \ln 2 - (\ln 4 - \ln 3) = \ln(3/2) \end{aligned}
python · try it yourself
import sympy as sp
x, y = sp.symbols("x y", nonnegative=True)
f = 1 / (1 + x + y)**2
a = sp.integrate(f, (y, 0, 2), (x, 0, 1))
b = sp.integrate(f, (x, 0, 1), (y, 0, 2))
print(sp.simplify(a - sp.log(sp.Rational(3, 2))), sp.simplify(b - sp.log(sp.Rational(3, 2))), round(float(a), 4))
0 0 0.4055

Printing the difference from the expected value avoids depending on how SymPy chooses to arrange the logarithms.

Test yourself

In ∫02∫13f(x,y) dx dy\int_0^2 \int_1^3 f(x, y)\,dx\,dy, which variable runs from 1 to 3?

  1. yy
  2. xx
  3. both
  4. neither

∬ over [0,1]×[0,2] of 6xy2 dA\iint \text{ over } [0, 1] \times [0, 2] \text{ of } 6xy^2\,dA equals…

  1. 8
  2. 4
  3. 16
  4. 6

For f=x2−y2(x2+y2)2f = \frac{x^2 - y^2}{(x^2 + y^2)^2} on (0,1]2(0, 1]^2, the iterated integrals are π4\frac{\pi}{4} and −π4-\frac{\pi}{4}. Why doesn't this contradict Fubini's theorem?

  1. Fubini only holds for polynomials
  2. ff is not integrable on the square (∬∣f∣ dA\iint \lvert f \rvert\,dA is infinite)
  3. SymPy made a mistake
  4. The square is not closed