---
title: Differentiating under the integral sign
heading: When the derivative of an integral is the integral of the derivative
eyebrow: A14 · Lesson 21 · Differentiating under the integral sign
xp: 40
---

::: hook
An engineer wants to know how a filter's output energy changes as its time constant changes, or how a detector's error rate moves with its threshold. Each is an integral that depends on a parameter, and the quick way to its derivative is to differentiate inside the integral. Usually that works. Sometimes it gives nonsense: an integral that's constant in its parameter, whose "derivative inside" doesn't even converge. This lesson states when the swap is allowed, uses it to evaluate integrals that resist every other method, and shows the case where it fails.
:::

::: play
- The curve is $F(t) = \int_0^\infty e^{-tx}\frac{\sin x}{x}\,dx$, computed as $\frac{\pi}{2} - \arctan t$, against $t$. Its derivative, from differentiating under the integral, is $-\int_0^\infty e^{-tx}\sin x\,dx = -1/(1 + t^2)$.
- At $t = 0$ the integral is $\pi/2$, the famous Dirichlet integral, reached by a detour through the parameter.
:::

```sim plot
view: graph
x: t = 0..5
y: F = 0..1.7
slider: t0 = 0..5 step 0.1 = 1
plot: pi/2 - atan(t) | label: F(t)
plot: (pi/2 - atan(t0)) - (t - t0)/(1 + t0^2) | dashed | label: tangent from the derivative under the integral
point: t0, pi/2 - atan(t0)
readout: F(t0) = pi/2 - atan(t0) | digits: 4
readout: slope = -1/(1 + t0^2) | digits: 4
```

::: predict
## $F(t) = \int_0^\infty \frac{\sin(tx)}{x}\,dx$ equals $\pi/2$ for every $t > 0$. What happens if you differentiate under the integral sign?

- [ ] You get 0, as you should
- [x] You get $\int_0^\infty \cos(tx)\,dx$, which doesn't converge
- [ ] You get $\pi/2$
- [ ] You get $-\pi/2t$

::: reveal
You get $\int_0^\infty\cos(tx)\,dx$, which has no value: it oscillates forever. Yet $F$ is constant, so its derivative is plainly 0. Swapping derivative and integral isn't automatic; it needs the derivative inside to be controlled by something integrable, and $|\cos(tx)|$ isn't.
:::
:::

::: nameit
## On a bounded interval

If $f(x, t)$ and $\partial f/\partial t$ are continuous on $[a, b] \times [c, d]$, then for $t$ in $(c, d)$

$$\frac{d}{dt}\int_a^b f(x, t)\,dx = \int_a^b \frac{\partial f}{\partial t}(x, t)\,dx$$

The proof uses lesson 13's uniform continuity: on a compact set, the difference quotient $\frac{f(x, t + h) - f(x, t)}{h}$ approaches $\partial f/\partial t$ uniformly in $x$, and a uniform limit can be integrated term by term.

## Moving limits: Leibniz's rule

$$\frac{d}{dt}\int_{a(t)}^{b(t)} f(x, t)\,dx = f(b(t), t)\,b'(t) - f(a(t), t)\,a'(t) + \int_{a(t)}^{b(t)} \frac{\partial f}{\partial t}\,dx$$

The first two terms come from the fundamental theorem (lesson 18), the last from the rule above.

## Improper integrals: domination

On an infinite range the swap needs more. It's allowed when there's a single integrable function $g(x)$ with $|\partial f/\partial t(x, t)| \le g(x)$ for every $t$ in the range of interest (lesson 47's dominated convergence, applied to difference quotients). For $\int_0^\infty e^{-tx}\frac{\sin x}{x}\,dx$ with $t \ge t_0 > 0$, the derivative inside is bounded by $e^{-t_0x}$, so the swap is fine; for $\int_0^\infty\frac{\sin(tx)}{x}\,dx$ there's no such bound.

## Using it: Feynman's trick

To evaluate $\int h(x)\,dx$, put it inside a family $F(t)$ whose derivative is easy, find $F'$, integrate back in $t$, and fix the constant from a value of $t$ where $F$ is known.
:::

::: example
## Find $\int_0^1 x^n\ln x\,dx$ by differentiating $\int_0^1 x^t\,dx$.

::: solution
1. **The family to differentiate.** For $t > -1$,
   $$\begin{aligned} I(t) &= \int_0^1 x^t\,dx \\ &= \frac{1}{t + 1} \end{aligned}$$
2. **Differentiate under the integral.** $\frac{\partial}{\partial t}x^t = x^t\ln x$ is continuous and bounded on $[0, 1]$ for $t > 0$ (it tends to 0 at $x = 0$), so
   $$\begin{aligned} \int_0^1 x^t\ln x\,dx &= I'(t) \\ &= -\frac{1}{(t + 1)^2} \end{aligned}$$
3. **Put in t = 1.**
   $$\int_0^1 x\ln x\,dx = -\frac14$$
4. **Differentiate again.** A second derivative gives
   $$\int_0^1 x^t\ln^2x\,dx = \frac{2}{(t + 1)^3}$$
   with no integration by parts at all.

**Answer:** $\int_0^1 x^n\ln x\,dx = -\frac{1}{(n + 1)^2}$, which is $-\frac14$ for n = 1.
:::
:::

::: example
## Evaluate the Dirichlet integral $\int_0^\infty\frac{\sin x}{x}\,dx$ with Feynman's trick.

::: solution
1. **Put it in a family.** For $t > 0$ let
   $$F(t) = \int_0^\infty e^{-tx}\frac{\sin x}{x}\,dx$$
2. **Differentiate under the integral.** The derivative inside, $-e^{-tx}\sin x$, is dominated by $e^{-t_0x}$ for $t \ge t_0 > 0$, so
   $$\begin{aligned} F'(t) &= -\int_0^\infty e^{-tx}\sin x\,dx \\ &= -\frac{1}{1 + t^2} \end{aligned}$$
3. **Integrate back.**
   $$F(t) = C - \arctan t$$
   and $F(t) \to 0$ as $t \to \infty$ gives $C = \pi/2$.
4. **Let $t \to 0^+$.** The convergence is uniform enough to pass to the limit, so
   $$\int_0^\infty\frac{\sin x}{x}\,dx = \frac{\pi}{2}$$
5. **A value on the way.** At $t = 1$ the integral is $\pi/4$.

**Answer:** $\int_0^\infty\frac{\sin x}{x}\,dx = \frac\pi2$.
:::
:::

::: example
## A filter's impulse response is $e^{-t/\tau}$, so its output energy for a unit impulse is $E(\tau) = \int_0^\infty e^{-2t/\tau}\,dt$. Find $dE/d\tau$ both ways.

::: solution
1. **Directly.** $E = \tau/2$, so
   $$\frac{dE}{d\tau} = \frac12$$
2. **Differentiate the integrand.**
   $$\frac{\partial}{\partial\tau}e^{-2t/\tau} = \frac{2t}{\tau^2}e^{-2t/\tau}$$
3. **Check domination.** For $\tau$ in any interval $[\tau_1, \tau_2]$ with $\tau_1 > 0$, this is dominated by $\frac{2t}{\tau_1^2}e^{-2t/\tau_2}$, which is integrable.
4. **Differentiate under the integral.**
   $$\begin{aligned} \frac{dE}{d\tau} &= \frac{2}{\tau^2}\int_0^\infty te^{-2t/\tau}\,dt \\ &= \frac{2}{\tau^2}\cdot\frac{\tau^2}{4} \\ &= \frac12 \end{aligned}$$
5. **Why it's useful.** The same method gives sensitivities when the integral has no closed form, which is how circuit optimizers compute gradients of energy, noise or error measures.

**Answer:** both ways give $dE/d\tau = \frac12$.
:::
:::

::: check
given: n = 1 | 2 | 4
answer: -1/(n + 1)^2
tolerance: 0.5%

## Using $\int_0^1 x^t\,dx = 1/(t + 1)$, what is $\int_0^1 x^{\given{n}}\ln x\,dx$?

::: right
Right. Differentiating under the integral gives $-1/(t + 1)^2$ at $t$ = {n}: {answer}.
:::

::: misses
- [1/(n + 1)^2] Check the sign: $\ln x < 0$ on $(0, 1)$, so the integral is negative.
- [-1/(n + 1)] That's $-I(t)$, not its derivative.
- [any] Not yet. Differentiate $1/(t + 1)$ with respect to $t$.
:::
:::

::: check
given: a = 0.5 | 1 | 2 | 5; k = 2 | 3
answer: k!/a^(k + 1)
tolerance: 0.5%

## Starting from $\int_0^\infty e^{-ax}\,dx = 1/a$, differentiate {k} time(s) with respect to $a$ to find $\int_0^\infty x^{\given{k}}e^{-\given{a}x}\,dx$.

::: right
Right. Each derivative brings down a factor $-x$, and $\frac{d^k}{da^k}\frac1a = \frac{(-1)^kk!}{a^{k+1}}$, so the integral is $k!/a^{k+1}$ = {answer}.
:::

::: misses
- [1/a^(k + 1)] Each derivative also brings down the power: the factor is $k!$.
- [any] Not yet. Differentiate $1/a$ {k} time(s) and fix the sign.
:::
:::

::: check
given: t = 0.7 | 1.2 | 1.5
answer: 2*t*sin(t^2)
tolerance: 0.5%

## What is $\frac{d}{dt}\int_0^{t^2}\sin x\,dx$ at $t$ = {t}?

::: right
Right. By Leibniz's rule, with nothing inside depending on $t$, only the moving upper limit counts: $\sin(t^2)\cdot 2t$ = {answer}.
:::

::: misses
- [sin(t^2)] Multiply by the derivative of the upper limit, $2t$.
- [2*t*cos(t^2)] The integrand at the upper limit is $\sin(t^2)$, not its derivative.
- [any] Not yet. Use $f(b(t))\,b'(t)$ with $b(t) = t^2$.
:::
:::

::: check
kind: python
given: t = 0.5 | 1 | 2

## For $I(t) = \int_0^\infty e^{-tx^2}\,dx = \frac12\sqrt{\pi/t}$, compute $I'(t)$ at `t` = {t} three ways: from the formula (`exact`), as a central difference of the formula with $h = 10^{-5}$ (`diff`), and by integrating $-x^2e^{-tx^2}$ numerically from 0 to 20 with Simpson's rule on 20 000 intervals (`inside`).

```python starter
import math
exact = diff = inside = 0
print(exact, diff, inside)
```

```python solution
import math
exact = -0.25 * math.sqrt(math.pi) * t**-1.5
I = lambda s: 0.5 * math.sqrt(math.pi / s)
h = 1e-5
diff = (I(t + h) - I(t - h)) / (2 * h)
n, L = 20000, 20.0
dx = L / n
g = lambda x: -x * x * math.exp(-t * x * x)
inside = (g(0) + g(L) + sum((4 if k % 2 else 2) * g(k * dx) for k in range(1, n))) * dx / 3
print(exact, diff, inside)
```

```python tests
import math
ex = -0.25 * math.sqrt(math.pi) * t**-1.5
assert abs(exact - ex) < 1e-15, "exact is -¼√π t^(-3/2)."
assert abs(diff - ex) < 1e-8 * abs(ex), "The central difference should agree to about 8 digits."
assert abs(inside - ex) < 1e-10 * abs(ex), "Integrating the derivative inside should agree to about 10 digits: the swap is valid here."
```

::: right
Right. All three agree: the integrand's $t$-derivative is dominated by $x^2e^{-t_0x^2}$, so the swap is justified, and integrating the derivative is even more accurate than differencing the result.
:::
:::

::: check
kind: derivation
vars: a = 0.5..3, x = 0..5

## Find $\int_0^\infty x^2e^{-ax}\,dx$ from $\int_0^\infty e^{-ax}\,dx = 1/a$.

::: step
answer: -x*exp(-a*x)

### What is $\frac{\partial}{\partial a}e^{-ax}$?
:::

::: step
answer: x^2*exp(-a*x)

### And $\frac{\partial^2}{\partial a^2}e^{-ax}$?
:::

::: step
answer: 2/a^3

### So $\int_0^\infty x^2e^{-ax}\,dx = \frac{d^2}{da^2}\frac1a = \;?$
:::

::: right
Right. For $a \ge a_0 > 0$ every derivative is dominated by $x^ke^{-a_0x}$, so the swaps are justified, and the moments of an exponential fall out without integrating by parts.
:::
:::

::: check
kind: choice

## $F(t) = \int_0^\infty\frac{\sin(tx)}{x}\,dx$ is $\pi/2$ for all $t > 0$, yet differentiating inside gives a divergent integral. Which condition for the swap fails?

- [x] There's no integrable function bounding $|\partial f/\partial t| = |\cos(tx)|$ for all $x$
- [ ] $f$ isn't continuous
- [ ] The interval of $t$ isn't compact
- [ ] Nothing fails; the swap gives the right answer

::: right
Right. $|\cos(tx)|$ doesn't decay, so no integrable $g(x)$ dominates it, and the swap has no justification. The integral converges only because of cancellation between the oscillations, which the derivative destroys.
:::

::: misses
- [2] $\sin(tx)/x$ is continuous (it tends to $t$ at $x = 0$).
- [3] Even on a compact interval of $t$ the problem remains: it's the infinite range in $x$ that needs a dominating function.
- [4] The swap gives a divergent integral, while the true derivative is 0.
:::
:::

::: explain
## Why does differentiating under an integral over an infinite range need a dominating function, when on a bounded range continuity is enough?

::: model
The derivative of the integral is the limit of integrals of difference quotients, and the swap claims that limit equals the integral of the limit. On a bounded box, continuity gives uniform continuity, so the difference quotients converge uniformly and uniform limits pass through integrals. On an infinite range uniformity can fail: the difference quotients can converge at each $x$ while their integrals over the far tail don't shrink, as with $\sin(tx)/x$, whose derivative $\cos(tx)$ never decays. A single integrable function bounding all of them stops any mass from escaping to infinity, which is exactly what the dominated convergence theorem needs.
:::
:::

::: practice

::: check
given: n = 0..8 int
answer: 2/(n + 1)^3
tolerance: 0.5%

## Differentiating $\int_0^1 x^t\,dx = 1/(t + 1)$ twice, what is $\int_0^1 x^{\given{n}}\ln^2x\,dx$?

::: misses
- [-2/(n + 1)^3] Two derivatives: the signs cancel, and the integrand is positive.
- [any] $\frac{d^2}{dt^2}\frac{1}{t + 1} = \frac{2}{(t + 1)^3}$.
:::
:::

::: check
given: t = 0.5 | 2 | 3
answer: pi/2 - atan(t)
tolerance: 0.5%

## What is $\int_0^\infty e^{-tx}\frac{\sin x}{x}\,dx$ at $t$ = {t}?

::: misses
- [atan(t)] It's $\frac{\pi}{2} - \arctan t$: it falls from $\pi/2$ as $t$ grows.
- [any] $F(t) = \frac{\pi}{2} - \arctan t$.
:::
:::
:::
