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 turns a messy integral into a simple one.
Why it works
The chain rule says . Read backwards:
Writing and makes this mechanical:
The recipe
- Pick : the inside of a power, a root, an exponent, a trig function.
- Compute .
- Rewrite the whole integral in . If an is left over, the choice did not work (or needs algebra).
- Integrate in , then put back.
Example 1. . Let , :
Example 2. . Let , , so :
A missing constant factor is fine (you divide by it); a missing is not.
Example 3. . Let , : .
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 values and never go back to :
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 , the limits must be values.
In software
Softplus is the integral of the sigmoid. The logistic sigmoid is everywhere in machine learning. With , :
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.
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 , the density of picks up a factor , the same bookkeeping. Sampling code that turns uniform random numbers into other distributions relies on it.
Exercises
Exercise 1: Power of an inside function
Find .
Answer
Solution
Exercise 2: Tangent
Find .
Answer
(equivalently )
Solution
Exercise 3: Definite, with new limits
Evaluate .
Answer
Solution
Exercise 4: The sigmoid's derivative
Find by substitution, then check with SymPy that your answer differs from only by a constant.
Answer
, which equals .
Solution
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 , which substitution and result are right?
- , result
- , result
- , result
- , result
Substitution is the reverse of which differentiation rule?
- Product rule
- Quotient rule
- Power rule
- Chain rule
In with , what are the new limits?
- from 1 to 2
- from 0 to 3
- from 2 to 5
- from 1 to 4