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 on , hold fixed and integrate in :
This is "partial integration", the reverse of partial differentiation: is treated as a constant. The result is a function of alone, which we then integrate:
The inner integral is done first, and the inner differential () matches the inner limits ( to ). The other order is .
Example: on .
Both orders give , the value the midpoint rule approached in the previous lesson.
Fubini's theorem
Theorem. If is continuous on , then
More generally it holds if is bounded on , is discontinuous only on finitely many smooth curves, and the iterated integrals exist.
Why it is true (slicing). For , is the area of the cross-section of the solid at position . The volume of a solid with known cross-sectional areas is , the slicing method for volumes. Slicing perpendicular to the -axis instead gives the other order.
Why it is true (sums). Take a midpoint Riemann sum and group its terms by column: . The inner bracket is a Riemann sum for , and the outer sum is a Riemann sum for . Continuity on the compact rectangle makes 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 , the inner integral pulls the constant factor out:
Example: .
This is the multivariable version of "independent pieces multiply", and it is exactly why the joint density of independent random variables factorises.
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 :
- first: needs integration by parts and leaves a messy function of .
- first: , and .
Rule of thumb: integrate first in the variable that makes the inner integral an obvious antiderivative.
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
which is unbounded near the origin. Since , the inner -integral is , and
The double integral of is infinite, so is not integrable and Fubini does not apply. (The companion result, Tonelli's theorem, says the order never matters for , even if the value is .)
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.dblquadandnquadare 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 isf(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: instead of . 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 , then in the other order.
Answer
2 in both orders.
Solution
Exercise 2: Separable
Evaluate over .
Answer
.
Solution
Exercise 3: Pick the easy order
Evaluate over ( from 0 to 1, from 0 to ).
Answer
Solution
Integrating in first needs integration by parts; first is immediate because is the derivative of with respect to .
Exercise 4: Check with SymPy (code)
Evaluate over by hand, then confirm both orders with SymPy.
Answer
.
Solution
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 , which variable runs from 1 to 3?
- both
- neither
equals…
- 8
- 4
- 16
- 6
For on , the iterated integrals are and . Why doesn't this contradict Fubini's theorem?
- Fubini only holds for polynomials
- is not integrable on the square ( is infinite)
- SymPy made a mistake
- The square is not closed