TExcelTAD
Home › Calculus Essentials › Integration by Substitution

Integration by Substitution

Substitution is the chain rule run backwards. When an integrand contains a function together with (a multiple of) its derivative, renaming the inner function uu turns a messy integral into a simple one.

Why it works

The chain rule says ddxF(g(x))=F′(g(x))⋅g′(x)\frac{d}{dx} F(g(x)) = F'(g(x)) \cdot g'(x). Read backwards:

∫f(g(x))⋅g′(x) dx=F(g(x))+Cwhere F′=f\int f(g(x)) \cdot g'(x)\,dx = F(g(x)) + C \qquad \text{where } F' = f

Writing u=g(x)u = g(x) and du=g′(x) dxdu = g'(x)\,dx makes this mechanical:

∫f(g(x))⋅g′(x) dx=∫f(u) du=F(u)+C=F(g(x))+C\int f(g(x)) \cdot g'(x)\,dx = \int f(u)\,du = F(u) + C = F(g(x)) + C

The recipe

  1. Pick u=the “inside” of somethingu = \text{the ``inside'' of something}: the inside of a power, a root, an exponent, a trig function.
  2. Compute du=u′(x) dxdu = u'(x)\,dx.
  3. Rewrite the whole integral in uu. If an xx is left over, the choice did not work (or needs algebra).
  4. Integrate in uu, then put xx back.

Example 1. ∫2xcos⁡(x2) dx\int 2x\cos(x^2)\,dx. Let u=x2u = x^2, du=2x dxdu = 2x\,dx:

∫cos⁡(u) du=sin⁡u+C=sin⁡(x2)+C\int \cos(u)\,du = \sin u + C = \sin(x^2) + C

Example 2. ∫x/(x2+1) dx\int x / (x^2 + 1)\,dx. Let u=x2+1u = x^2 + 1, du=2x dxdu = 2x\,dx, so x dx=du/2x\,dx = du/2:

∫1u⋅du2=12ln⁡∣u∣+C=12ln⁡(x2+1)+C\int \frac{1}{u} \cdot \frac{du}{2} = \tfrac{1}{2} \ln\lvert u \rvert + C = \tfrac{1}{2} \ln(x^2 + 1) + C

A missing constant factor is fine (you divide by it); a missing xx is not.

Example 3. ∫sin⁡3x⋅cos⁡x dx\int \sin^3 x \cdot \cos x\,dx. Let u=sin⁡xu = \sin x, du=cos⁡x dxdu = \cos x\,dx: ∫u3 du=sin⁡4x/4+C\int u^3\,du = \sin^4 x / 4 + C.

python · try it yourself
import sympy as sp

x = sp.symbols("x")
examples = [2 * x * sp.cos(x**2), x / (x**2 + 1), sp.sin(x)**3 * sp.cos(x)]
for f in examples:
    F = sp.integrate(f, x)
    print(f"∫ {str(f):18} dx = {F}")
∫ 2*x*cos(x**2)      dx = sin(x**2)
∫ x/(x**2 + 1)       dx = log(x**2 + 1)/2
∫ sin(x)**3*cos(x)   dx = sin(x)**4/4

Definite integrals: change the limits too

With a definite integral you can convert the limits to uu values and never go back to xx:

∫01xex2 dx,u=x2,du=2x dx,x=0→u=0,x=1→u=1=12∫01eu du=12(e−1)≈0.8591\begin{aligned} &\int_0^1 x e^{x^2}\,dx, \quad u = x^2, \quad du = 2x\,dx, \quad x = 0 \to u = 0, \quad x = 1 \to u = 1 \\ &= \tfrac{1}{2} \int_0^1 e^u\,du = \tfrac{1}{2} (e - 1) \approx 0.8591 \end{aligned}
python · try it yourself
import sympy as sp

x, u = sp.symbols("x u")
original = sp.integrate(x * sp.exp(x**2), (x, 0, 1))
in_u = sp.Rational(1, 2) * sp.integrate(sp.exp(u), (u, 0, 1))
print("in x:", original, " in u:", in_u, " ≈", round(float(in_u), 4))
in x: -1/2 + E/2  in u: -1/2 + E/2  ≈ 0.8591

Tip: Forgetting to change the limits (or to substitute back) is the most common mistake. If you stay in uu, the limits must be uu values.

In software

Softplus is the integral of the sigmoid. The logistic sigmoid σ(x)=ex/(1+ex)\sigma(x) = e^x / (1 + e^x) is everywhere in machine learning. With u=1+exu = 1 + e^x, du=ex dxdu = e^x\,dx:

∫σ(x) dx=∫ex1+ex dx=∫duu=ln⁡(1+ex)+C\int \sigma(x)\,dx = \int \frac{e^x}{1 + e^x}\,dx = \int \frac{du}{u} = \ln(1 + e^x) + C

ln⁡(1+ex)\ln(1 + e^x) is the softplus activation, a smooth version of ReLU. Its derivative is the sigmoid, which is exactly why softplus is used where a smooth ReLU is wanted.

python · try it yourself
import math

softplus = lambda x: math.log1p(math.exp(x))
sigmoid = lambda x: 1 / (1 + math.exp(-x))

h = 1e-6
for x in [-2.0, 0.0, 1.5]:
    slope = (softplus(x + h) - softplus(x - h)) / (2 * h)
    print(f"x={x:4}: softplus'(x) ≈ {slope:.6f}   sigmoid(x) = {sigmoid(x):.6f}")
x=-2.0: softplus'(x) ≈ 0.119203   sigmoid(x) = 0.119203
x= 0.0: softplus'(x) ≈ 0.500000   sigmoid(x) = 0.500000
x= 1.5: softplus'(x) ≈ 0.817574   sigmoid(x) = 0.817574

Substitution also shows up as change of variables in probability: if Y=g(X)Y = g(X), the density of YY picks up a factor ∣dx/dy∣\lvert dx/dy \rvert, the same du=g′ dxdu = g'\,dx bookkeeping. Sampling code that turns uniform random numbers into other distributions relies on it.

Exercises

Exercise 1: Power of an inside function

Find ∫3x2(x3+1)5 dx\int 3x^2(x^3 + 1)^5\,dx.

Answer

(x3+1)6/6+C(x^3 + 1)^6 / 6 + C

Solution
u=x3+1,du=3x2 dx∫u5 du=u6/6+C=(x3+1)6/6+C\begin{aligned} &u = x^3 + 1, \quad du = 3x^2\,dx \\ &\int u^5\,du = u^6/6 + C = (x^3 + 1)^6/6 + C \end{aligned}

Exercise 2: Tangent

Find ∫tan⁡x dx\int \tan x\,dx.

Answer

−ln⁡∣cos⁡x∣+C-\ln\lvert \cos x \rvert + C (equivalently ln⁡∣sec⁡x∣+C\ln\lvert \sec x \rvert + C)

Solution
tan⁡x=sin⁡x/cos⁡x;u=cos⁡x,du=−sin⁡x dx∫sin⁡x/cos⁡x dx=−∫du/u=−ln⁡∣u∣+C=−ln⁡∣cos⁡x∣+C\begin{aligned} &\tan x = \sin x / \cos x; \quad u = \cos x, \quad du = -\sin x\,dx \\ &\int \sin x / \cos x\,dx = -\int du/u = -\ln\lvert u \rvert + C = -\ln\lvert \cos x \rvert + C \end{aligned}

Exercise 3: Definite, with new limits

Evaluate ∫0π/2sin⁡x⋅cos⁡2x dx\int_0^{\pi/2} \sin x \cdot \cos^2 x\,dx.

Answer

1/31/3

Solution
u=cos⁡x,du=−sin⁡x dx;x=0→u=1,x=π/2→u=0∫10u2 (−du)=∫01u2 du=1/3\begin{aligned} &u = \cos x, \quad du = -\sin x\,dx; \quad x = 0 \to u = 1, \quad x = \pi/2 \to u = 0 \\ &\int_1^0 u^2\,(-du) = \int_0^1 u^2\,du = 1/3 \end{aligned}

Exercise 4: The sigmoid's derivative

Find ∫ex/(1+ex)2 dx\int e^x / (1 + e^x)^2\,dx by substitution, then check with SymPy that your answer differs from σ(x)=1/(1+e−x)\sigma(x) = 1/(1 + e^{-x}) only by a constant.

Answer

−1/(1+ex)+C-1/(1 + e^x) + C, which equals σ(x)−1+C\sigma(x) - 1 + C.

Solution
u=1+ex,du=ex dx∫du/u2=−1/u+C=−1/(1+ex)+C\begin{aligned} &u = 1 + e^x, \quad du = e^x\,dx \\ &\int du/u^2 = -1/u + C = -1/(1 + e^x) + C \end{aligned}
python · try it yourself
import sympy as sp

x = sp.symbols("x")
mine = -1 / (1 + sp.exp(x))
sigma = 1 / (1 + sp.exp(-x))
print("derivative matches:", sp.simplify(sp.diff(mine, x) - sp.exp(x) / (1 + sp.exp(x))**2) == 0)
print("mine - sigma =", sp.simplify(mine - sigma))
derivative matches: True
mine - sigma = -1

Test yourself

For ∫cos⁡(5x) dx\int \cos(5x)\,dx, which substitution and result are right?

  1. u=cos⁡xu = \cos x, result sin⁡(5x)\sin(5x)
  2. u=5xu = 5x, result sin⁡(5x)/5+C\sin(5x)/5 + C
  3. u=5xu = 5x, result 5sin⁡(5x)+C5\sin(5x) + C
  4. u=xu = x, result sin⁡(5x)+C\sin(5x) + C

Substitution is the reverse of which differentiation rule?

  1. Product rule
  2. Quotient rule
  3. Power rule
  4. Chain rule

In ∫122x(x2+1)3 dx\int_1^2 2x(x^2 + 1)^3\,dx with u=x2+1u = x^2 + 1, what are the new limits?

  1. uu from 1 to 2
  2. uu from 0 to 3
  3. uu from 2 to 5
  4. uu from 1 to 4