TExcelTAD
Home › Calculus in Depth › The Chain Rule

The Chain Rule

The chain rule differentiates compositions: a function fed into another function. It is the most used rule in calculus, the one that makes sin⁡(x2)\sin(x^2), e−x2/2e^{-x^2/2} and 1+x4\sqrt{1 + x^4} routine, and the mathematical core of backpropagation in neural networks. This lesson states it, proves it carefully, and shows how to apply it to long chains.

For a gentler first look, see The Chain Rule and Backpropagation Is the Chain Rule.

Statement

If gg is differentiable at xx and ff is differentiable at g(x)g(x), then F=f∘gF = f \circ g, F(x)=f(g(x))F(x) = f(g(x)), is differentiable at xx and

F′(x)=f′(g(x))⋅g′(x)F'(x) = f'(g(x)) \cdot g'(x)

In Leibniz notation, with u=g(x)u = g(x) and y=f(u)y = f(u):

dydx=dydu⋅dudx\frac{dy}{dx} = \frac{dy}{du} \cdot \frac{du}{dx}

Read it as "derivative of the outside, evaluated at the inside, times the derivative of the inside". Rates multiply: if uu changes 3 times as fast as xx, and yy changes 2 times as fast as uu, then yy changes 6 times as fast as xx.

Why the naive proof fails, and a correct one

The tempting argument writes Δy/Δx=(Δy/Δu)⋅(Δu/Δx)\Delta y/\Delta x = (\Delta y/\Delta u) \cdot (\Delta u/\Delta x) and lets Δx→0\Delta x \to 0. The flaw: Δu=g(x+h)−g(x)\Delta u = g(x + h) - g(x) may be zero for small h≠0h \ne 0 (for example when gg is constant near xx, or oscillates like x2sin⁡(1/x)x^2 \sin(1/x)), and then Δy/Δu\Delta y/\Delta u is 0/00/0.

Proof. Fix the problem by defining the slope of ff at b=g(x)b = g(x) with a helper that is safe when the step is zero:

ε(k)={f(b+k)−f(b)k−f′(b)if k≠00if k=0\varepsilon(k) = \begin{cases} \dfrac{f(b + k) - f(b)}{k} - f'(b) & \text{if } k \ne 0 \\[1ex] 0 & \text{if } k = 0 \end{cases}

Because ff is differentiable at bb, ε(k)→0\varepsilon(k) \to 0 as k→0k \to 0, and for every kk (including 0)

f(b+k)−f(b)=(f′(b)+ε(k))⋅kf(b + k) - f(b) = (f'(b) + \varepsilon(k)) \cdot k

Now put k=Δu=g(x+h)−g(x)k = \Delta u = g(x + h) - g(x), so b+k=g(x+h)b + k = g(x + h), and divide by hh:

F(x+h)−F(x)h=(f′(b)+ε(Δu))⋅g(x+h)−g(x)h\frac{F(x+h) - F(x)}{h} = (f'(b) + \varepsilon(\Delta u)) \cdot \frac{g(x+h) - g(x)}{h}

As h→0h \to 0: gg is continuous at xx, so Δu→0\Delta u \to 0 and ε(Δu)→0\varepsilon(\Delta u) \to 0; and the last fraction tends to g′(x)g'(x). The limit is f′(g(x))⋅g′(x)f'(g(x)) \cdot g'(x). ∎

Worked examples

Function Outside f(u)f(u) Inside u=g(x)u = g(x) Derivative
(3x2+1)5(3x^2 + 1)^5 u5u^5 3x2+13x^2 + 1 5(3x2+1)4⋅6x5(3x^2 + 1)^4 \cdot 6x
sin⁡(x2)\sin(x^2) sin⁡u\sin u x2x^2 cos⁡(x2)⋅2x\cos(x^2) \cdot 2x
e−x2/2e^{-x^2/2} eue^u −x2/2-x^2/2 −x⋅e−x2/2-x \cdot e^{-x^2/2}
1+x4\sqrt{1 + x^4} u\sqrt{u} 1+x41 + x^4 2x3/1+x42x^3/\sqrt{1 + x^4}
bx=exln⁡bb^x = e^{x \ln b} eue^u xln⁡bx \ln b bx⋅ln⁡bb^x \cdot \ln b

The last row proves the general exponential rule promised in Derivatives of Polynomials and Exponential Functions. Combined with the chain rule, the power rule for functions reads ddx[g(x)]n=n[g(x)]n−1⋅g′(x)\frac{d}{dx} [g(x)]^n = n[g(x)]^{n-1} \cdot g'(x).

Longer chains

For three layers, y=f(g(k(x)))y = f(g(k(x))), apply the rule twice:

y′=f′(g(k(x)))⋅g′(k(x))⋅k′(x)y' = f'(g(k(x))) \cdot g'(k(x)) \cdot k'(x)

Example: y=sin⁡3(e2x)=(sin⁡(e2x))3y = \sin^3(e^{2x}) = (\sin(e^{2x}))^3 has layers cube, sine, exp, linear:

y′=3sin⁡2(e2x)⋅cos⁡(e2x)⋅e2x⋅2y' = 3\sin^2(e^{2x}) \cdot \cos(e^{2x}) \cdot e^{2x} \cdot 2

SymPy agrees, and a numeric check confirms the formula at one point:

python · try it yourself
import math
import sympy as sp

x = sp.symbols('x')
y = sp.sin(sp.exp(2*x))**3
dy = sp.diff(y, x)
print("SymPy:", dy)

hand = 3*sp.sin(sp.exp(2*x))**2 * sp.cos(sp.exp(2*x)) * sp.exp(2*x) * 2
print("matches hand formula:", sp.simplify(dy - hand) == 0)

F = sp.lambdify(x, y, "math")
h, a = 1e-6, 0.3
print("numeric  y'(0.3) ≈", round((F(a + h) - F(a - h)) / (2*h), 5))
print("formula  y'(0.3) =", round(float(dy.subs(x, a)), 5))
SymPy: 6*exp(2*x)*sin(exp(2*x))**2*cos(exp(2*x))
matches hand formula: True
numeric  y'(0.3) ≈ -2.55066
formula  y'(0.3) = -2.55066

Forward and reverse: how a computer applies the rule

A chain x→u1→u2→…→yx \to u_1 \to u_2 \to \ldots \to y produces a product of local derivatives. You can multiply them in two orders:

  • Forward mode: start at xx, carry the pair (value, derivative) through each layer.
  • Reverse mode: run forward to get all values, then multiply local derivatives from yy back to xx. This is backpropagation; with many inputs and one output (a loss), it gets every partial derivative in one backward pass.
python · try it yourself
import math

# y = sin(u2), u2 = u1**2, u1 = 3x + 1   at x = 0.5
x = 0.5
u1 = 3*x + 1
u2 = u1**2
y = math.sin(u2)

# local derivatives
d_u1_dx, d_u2_du1, d_y_du2 = 3, 2*u1, math.cos(u2)

# reverse sweep: start with dy/dy = 1 and walk back
grad = 1.0
for name, local in [("u2", d_y_du2), ("u1", d_u2_du1), ("x", d_u1_dx)]:
    grad *= local
    print(f"dy/d{name:2} = {grad: .6f}")
print("closed form 6(3x+1)·cos((3x+1)²):", round(6*u1*math.cos(u1**2), 6))
dy/du2 =  0.999449
dy/du1 =  4.997247
dy/dx  =  14.991741
closed form 6(3x+1)·cos((3x+1)²): 14.991741

Each step multiplies one more local derivative; the running product is the derivative of yy with respect to that node. A real framework does the same over a graph with millions of nodes (Automatic Differentiation).

Note: The vanishing gradient problem is the chain rule in action: a product of many factors smaller than 1 (such as sigmoid slopes, at most 14\frac{1}{4}) shrinks exponentially with depth. ReLU activations and residual connections were introduced largely to keep those products near 1.

In software

  • Neural networks and LLMs: every gradient used in training is a chain-rule product through the layers (Machine Learning, Generative AI & LLMs).
  • Easing and time remapping: if an animation plays p(s)p(s) along a remapped clock s=e(t)s = e(t), its speed is p′(e(t))⋅e′(t)p'(e(t)) \cdot e'(t).
  • Physics with changing units or coordinates: converting a rate in one variable to a rate in another is always a chain-rule multiplication, which is the basis of Related Rates.

Exercises

Exercise 1: Power of a polynomial

Differentiate y=(x3−2x)4y = (x^3 - 2x)^4.

Answer

y′=4(x3−2x)3(3x2−2)y' = 4(x^3 - 2x)^3(3x^2 - 2)

Solution
outside u4→4u3;inside u=x3−2x→3x2−2y′=4(x3−2x)3⋅(3x2−2)\begin{aligned} &\text{outside } u^4 \to 4u^3; \quad \text{inside } u = x^3 - 2x \to 3x^2 - 2 \\ &y' = 4(x^3 - 2x)^3 \cdot (3x^2 - 2) \end{aligned}

Exercise 2: Exponential with trig inside

Differentiate y=ecos⁡3xy = e^{\cos 3x}.

Answer

y′=−3sin⁡(3x)⋅ecos⁡3xy' = -3\sin(3x) \cdot e^{\cos 3x}

Solution
layers: eu, u=cos⁡v, v=3xy′=ecos⁡3x⋅(−sin⁡3x)⋅3\begin{aligned} &\text{layers: } e^u,\ u = \cos v,\ v = 3x \\ &y' = e^{\cos 3x} \cdot (-\sin 3x) \cdot 3 \end{aligned}

Exercise 3: Composite from a table

g(2)=5g(2) = 5, g′(2)=−3g'(2) = -3, f′(5)=4f'(5) = 4. Find (f∘g)′(2)(f \circ g)'(2).

Answer

−12-12

Solution
(f∘g)′(2)=f′(g(2))⋅g′(2)=f′(5)⋅(−3)=4⋅(−3)=−12(f \circ g)'(2) = f'(g(2)) \cdot g'(2) = f'(5) \cdot (-3) = 4 \cdot (-3) = -12

Exercise 4: Quotient or chain?

Differentiate y=1x2+9y = \frac{1}{\sqrt{x^2 + 9}} using the chain rule only, and evaluate at x=4x = 4.

Answer

y′=−x(x2+9)3/2y' = -\dfrac{x}{(x^2 + 9)^{3/2}}, and y′(4)=−4/125=−0.032y'(4) = -4/125 = -0.032

Solution
y=(x2+9)−1/2y′=−12(x2+9)−3/2⋅2x=−x(x2+9)3/2at x=4: x2+9=25, 253/2=125, so y′=−4/125\begin{aligned} &y = (x^2 + 9)^{-1/2} \\ &y' = -\tfrac{1}{2}(x^2 + 9)^{-3/2} \cdot 2x = -\frac{x}{(x^2 + 9)^{3/2}} \\ &\text{at } x = 4{:}\ x^2 + 9 = 25,\ 25^{3/2} = 125, \text{ so } y' = -4/125 \end{aligned}
python · try it yourself
import sympy as sp

x = sp.symbols('x')
d = sp.diff(1/sp.sqrt(x**2 + 9), x)
print(d, "|", d.subs(x, 4))
-x/(x**2 + 9)**(3/2) | -4/125

Exercise 5: Reverse mode by hand

For y=(2x+1)3y = (2x + 1)^3 write the forward values and the backward products at x=1x = 1, as in the reverse-sweep example. Check against the closed form.

Answer

u=3u = 3, y=27y = 27; dydu=27\frac{dy}{du} = 27, dydx=54\frac{dy}{dx} = 54.

Solution
forward: u=2⋅1+1=3,y=u3=27local: dydu=3u2=27,dudx=2backward: dydu=27,dydx=27⋅2=54closed form: 6(2x+1)2 at x=1→6⋅9=54\begin{aligned} &\text{forward: } && u = 2 \cdot 1 + 1 = 3, \quad y = u^3 = 27 \\ &\text{local: } && \frac{dy}{du} = 3u^2 = 27, \quad \frac{du}{dx} = 2 \\ &\text{backward: } && \frac{dy}{du} = 27, \quad \frac{dy}{dx} = 27 \cdot 2 = 54 \\ &\text{closed form: } && 6(2x + 1)^2 \text{ at } x = 1 \to 6 \cdot 9 = 54 \end{aligned}

Test yourself

What is ddxcos⁡(x3)\frac{d}{dx} \cos(x^3)?

  1. −sin⁡(3x2)-\sin(3x^2)
  2. −sin⁡(x3)⋅3x2-\sin(x^3) \cdot 3x^2
  3. sin⁡(x3)⋅3x2\sin(x^3) \cdot 3x^2
  4. −3x2cos⁡(x3)-3x^2 \cos(x^3)

Why does the proof need the helper ε(k)\varepsilon(k) instead of writing Δy/Δu⋅Δu/Δx\Delta y/\Delta u \cdot \Delta u/\Delta x?

  1. Δu\Delta u can be 0 for small h≠0h \ne 0, making Δy/Δu\Delta y/\Delta u undefined
  2. Δx\Delta x can be negative
  3. ff might not be continuous
  4. Leibniz notation is not rigorous at all

Backpropagation computes gradients by…

  1. finite differences for each weight
  2. symbolic simplification of the whole network
  3. multiplying local derivatives from the output back towards the inputs
  4. the quotient rule