Differentiation of Univariate Functions
Chapter 4 asked what is inside a matrix. This chapter asks how a function changes, and it starts where every calculus course starts — one input, one output — because everything later is that idea with more indices.
There are two things on this page. The derivative as the limit of a difference quotient, which you have met; and the Taylor series, which turns a function into a polynomial you can differentiate, integrate and reason about. The second is the one that does the work in the rest of the book: linearisation in §5.8, the Laplace approximation in Chapter 6, and the quadratic models that Chapter 7’s optimisers are built on are all Taylor expansions stopped early.
What you’ll learn
Section titled “What you’ll learn”- Definition 5.1 and 5.2: the difference quotient, and the derivative as its limit.
- Why the limit is a definition and not an algorithm — measured, with the exact step size where a finite difference stops improving.
- Definition 5.3: the Taylor polynomial , and Definition 5.4: the Taylor series .
- Why a Taylor polynomial of degree reproduces a polynomial of degree exactly, with every later coefficient zero.
- The thing most treatments skip: near the error falls with degree; far from it grows.
- §5.1.2’s four differentiation rules, and the one that matters most later — the chain rule.
Intuition: a secant that stops moving
Section titled “Intuition: a secant that stops moving”Pick a point on a curve and a second point a distance away. The straight line through them has a slope you can compute from two function values and a division. Now slide the second point towards the first. The secant turns, and — if the function is smooth — it settles on a particular line. Its slope is the derivative.
That is the whole idea, and it is worth noticing what it does not say. It does not say “compute the slope for a small ”. It says the slopes converge, and the derivative is the thing they converge to. Those are different claims, and on a computer the difference is the subject of half this page.
flowchart TD DQ["difference quotient
(f(x+h) − f(x)) / h
Def 5.1 — the slope of a secant"] DQ -->|"h → 0"| D["derivative f'(x)
Def 5.2 — the slope of the tangent"] D --> RULES["§5.1.2 rules
product, quotient, sum, chain"] D --> HIGH["f'', f''', … f⁽ᵏ⁾"] HIGH --> TP["Taylor polynomial
Tₙ(x) = Σ f⁽ᵏ⁾(x₀)/k! (x−x₀)ᵏ
Def 5.3, Eq 5.7"] TP -->|"n → ∞"| TS["Taylor series T∞
Def 5.4, Eq 5.8"] TS -->|"x₀ = 0"| MAC["Maclaurin series"] TS -->|"f = T∞"| AN["f is analytic"] TP --> USE["stop at n = 1: linearisation §5.8
stop at n = 2: the Laplace approximation
and every quadratic model in Ch 7"] RULES --> AD["§5.6 automatic differentiation
is the chain rule, applied mechanically"]
The math
Section titled “The math”The difference quotient and the derivative
Section titled “The difference quotient and the derivative”The book works Example 5.2 — the derivative of — straight from Definition 5.2 rather than quoting the power rule, and the point of that exercise is the binomial expansion: every term with for vanishes in the limit, leaving .
Taylor polynomials
Section titled “Taylor polynomials”The book’s Remark after Definition 5.4 is the one to hold on to: in general, a Taylor polynomial of degree is an approximation of a function, which does not need to be a polynomial. The Taylor polynomial is similar to in a neighbourhood around . However, a Taylor polynomial of degree is an exact representation of a polynomial of degree .
Two words there earn their place. “Neighbourhood” — the guarantee is local, and the measurements below show exactly how local. “Exact” — for a polynomial, is not an approximation at all.
The rules
Section titled “The rules”§5.1.2 lists four, and the chain rule is the one the rest of the chapter is built from:
| rule | statement |
|---|---|
| product | |
| quotient | |
| sum | |
| chain |
Worked example by hand
Section titled “Worked example by hand”Example 5.3 — x to the fourth, expanded at 1
Section titled “Example 5.3 — x to the fourth, expanded at 1”, so , , , , , and identically. Dividing by :
| coefficient | |||
|---|---|---|---|
| 0 | |||
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 |
Those coefficients are — the fourth row of Pascal’s triangle, which is exactly what the binomial theorem says should give. So , and every coefficient past the fourth is zero.
Measured over : the largest is — machine noise, and it does not change at or .
Example 5.4 — sin plus cos, expanded at 0
Section titled “Example 5.4 — sin plus cos, expanded at 0”The derivatives cycle with period four. Since , at :
so
This is Exercise 5.4, and it is done.
The error, both ways
Section titled “The error, both ways”Here is the part that is usually left out. Measure twice: once on a tight interval around the expansion point, and once on .
| max error, | max error, | |
|---|---|---|
| 0 | ||
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 |
The left column falls by roughly a factor of ten per degree. The right column rises, more than quadrupling from to , before turning around. A Taylor polynomial buys accuracy near a point by giving it up away from it, and any statement that “more terms is better” is missing the qualifier.
See it move
Section titled “See it move”The coefficients cycle +1, +1, −1, −1. Each frame reports the local order of the error, derived from the first nonzero coefficient not yet used rather than assumed to be n+1.
Watch degree 4. The error drops to machine noise and the last two frames change nothing, because every remaining coefficient is exactly zero.
From scratch
Section titled “From scratch”The obvious way to get numerically is to difference times. It does not work, and the reason is worth seeing.
import math
import numpy as np
def coeffs_by_differencing(f, x0, K, h=1e-3):
"""Repeated central differences. Loses about 8 digits per order."""
out = []
for k in range(K + 1):
# The k-th central difference, from the binomial stencil.
acc = 0.0
for j in range(k + 1):
acc += (-1) ** j * math.comb(k, j) * f(x0 + (k / 2 - j) * h)
out.append(acc / h ** k / math.factorial(k))
return out
def coeffs_by_jet(x0, K):
"""Truncated-Taylor arithmetic for sin(x) + cos(x): exact at every order."""
s = [0.0] * (K + 1)
c = [0.0] * (K + 1)
s[0], c[0] = math.sin(x0), math.cos(x0)
u = [0.0] * (K + 1)
u[0], u[1] = x0, 1.0
for k in range(1, K + 1):
ss = sum(j * u[j] * c[k - j] for j in range(1, k + 1))
cc = sum(j * u[j] * s[k - j] for j in range(1, k + 1))
s[k], c[k] = ss / k, -cc / k
return [s[k] + c[k] for k in range(K + 1)]
K = 8
exact = [(math.sin(k * math.pi / 2) + math.cos(k * math.pi / 2)) / math.factorial(k)
for k in range(K + 1)]
diff = coeffs_by_differencing(lambda x: math.sin(x) + math.cos(x), 0.0, K)
jet = coeffs_by_jet(0.0, K)
print(f"{'k':>2} {'exact':>14} {'by differencing':>16} {'error':>10} {'by jet':>14} {'error':>10}")
for k in range(K + 1):
print(f"{k:>2} {exact[k]:>14.10f} {diff[k]:>16.10f} {abs(diff[k]-exact[k]):>10.1e}"
f" {jet[k]:>14.10f} {abs(jet[k]-exact[k]):>10.1e}") k exact by differencing error by jet error
0 1.0000000000 1.0000000000 0.0e+00 1.0000000000 0.0e+00
1 1.0000000000 0.9999999583 4.2e-08 1.0000000000 0.0e+00
2 -0.5000000000 -0.4999999583 4.2e-08 -0.5000000000 5.6e-17
3 -0.1666666667 -0.1666666805 1.4e-08 -0.1666666667 5.6e-17
4 0.0416666667 0.0416426153 2.4e-05 0.0416666667 6.9e-18
5 0.0083333333 0.0138777878 5.5e-03 0.0083333333 1.7e-18
6 -0.0013888889 5.5511151231 5.6e+00 -0.0013888889 4.3e-19
7 -0.0001984127 -1299.6658423191 1.3e+03 -0.0001984127 8.1e-20
8 0.0000248016 -225789.4048096796 2.3e+05 0.0000248016 1.0e-20Read the two error columns, and then read the differencing column itself, because the failure is worse than “inaccurate”.
Differencing holds up to about . Then it does not merely lose precision, it loses the answer:
| true coefficient | by differencing | |
|---|---|---|
| 5 | ||
| 6 | ||
| 7 | ||
| 8 |
At the sign is wrong and the magnitude is out by a factor of four thousand. At it is out by ten orders of magnitude. This is not an approximation that has degraded; it is noise multiplied by .
The jet column is exact at every order, and the reason is that it never subtracts two nearly equal numbers. Each recurrence is an algebraic identity between coefficients, so the chain rule is applied symbolically and evaluated numerically. That is also, precisely, forward-mode automatic differentiation — which is the connection §5.6 makes explicit.
On real data
Section titled “On real data”Reading the plot
Section titled “Reading the plot”From the secant figure. The convergence is visible but the rate is the thing to extract. The errors are , , and for — each roughly half the previous. Halving halves the error, so the forward difference is first order, and the leading error term is . That is also why the error is smaller for the same where the curve is straighter.
From the round-off figure. This is the figure that changes how you write code.
The forward difference has error — truncation falling like , round-off rising like . The sum has a minimum, and it is not at the smallest representable :
| best | best error | |
|---|---|---|
| forward difference | ||
| central difference |
Two consequences. First, is the classic forward-difference rule of thumb, and the measurement lands on it. Second, the central difference is not merely better — it is better by three orders of magnitude, for one extra function evaluation. If you are ever going to difference numerically, difference centrally.
And the failure mode is severe: at the central estimate is wrong by , which is times worse than its own best. A gradient check that “uses a really small h to be safe” is not being safe.
From the Taylor figure. The two curves on the right go in opposite directions, and the reason is that a Taylor polynomial is a local object. matches to order at and is under no obligation anywhere else — and a degree- polynomial has to do something as grows, which for (bounded) means running away.
The left column of the table is the guarantee: a factor of about ten per degree on . The right column is the small print.
Pitfalls
Section titled “Pitfalls”Compare
Section titled “Compare”| way to get a derivative | exactness | cost | needs |
|---|---|---|---|
| by hand, symbolically | exact | your time | a closed form, and care |
| forward difference | , floor | 1 extra evaluation | nothing |
| central difference | , floor | 2 extra evaluations | nothing |
| jet / forward-mode AD | exact to machine precision | the function | the code, not the formula |
| reverse-mode AD (§5.6) | exact to machine precision | the function, all inputs at once | a computation graph |
| complex-step | with no cancellation | 1 complex evaluation | an analytic function |
The middle two rows are the ones this page is about. The bottom three are §5.6, and the reason the chapter goes there.
-
Definition 5.2 says the derivative is the limit of the difference quotient. Why can that not be executed directly in floating point?
At h = 1e-15 the central difference on this page is wrong by 8e-02, which is 2.8e+11 times worse than its own best at h = 2.7e-06. Making h smaller past the optimum makes things worse, not better.
pch.quizShowAnswer
B — Because the truncation error falls like h while the round-off error from subtracting two nearly equal numbers rises like 1/h, so the total has a minimum at a specific h — measured here at 1.5e-08 for a forward difference — At h = 1e-15 the central difference on this page is wrong by 8e-02, which is 2.8e+11 times worse than its own best at h = 2.7e-06. Making h smaller past the optimum makes things worse, not better.
-
A central difference costs one extra function evaluation over a forward difference. What does that buy?
The h-squared error term comes from the odd-order terms cancelling between f(x+h) and f(x-h). It is close to free, so if you are differencing numerically at all, difference centrally.
pch.quizShowAnswer
B — Second-order rather than first-order convergence, and a floor three orders of magnitude lower: 2.9e-13 against 2.6e-10 on this page — The h-squared error term comes from the odd-order terms cancelling between f(x+h) and f(x-h). It is close to free, so if you are differencing numerically at all, difference centrally.
-
Why is T_4 of x^4 at x0 = 1 not an approximation?
Measured max error over the window: 7.11e-15, unchanged at degrees 5 and 6. The book states this in the Remark after Definition 5.4.
pch.quizShowAnswer
B — Because a Taylor polynomial of degree n reproduces a polynomial of degree at most n exactly — the coefficients 1, 4, 6, 4, 1 are the binomial expansion of ((x-1)+1)^4, and every coefficient past the fourth is exactly zero — Measured max error over the window: 7.11e-15, unchanged at degrees 5 and 6. The book states this in the Remark after Definition 5.4.
-
Adding Taylor terms drove the sigmoid's maximum error on [-6, 6] from 0.5 up to 46. Is something wrong?
Near x0 the sigmoid's error falls 1.2e-1, 2.5e-3, 2.5e-3, 6.3e-5, ... every added term helps locally. Quoting a Taylor error without naming the interval is the mistake, not the polynomial.
pch.quizShowAnswer
B — No. Each polynomial is strictly better near x0 — the error there falls monotonically — and a Taylor polynomial promises nothing away from the expansion point. A degree-7 polynomial has to grow without bound, and a bounded function does not — Near x0 the sigmoid's error falls 1.2e-1, 2.5e-3, 2.5e-3, 6.3e-5, ... every added term helps locally. Quoting a Taylor error without naming the interval is the mistake, not the polynomial.
-
The from-scratch table gets the 6th Taylor coefficient as +5.551 instead of -0.001389 using repeated differences, but exactly using jets. What is the difference?
The k-th difference divides by h to the k, so at h = 1e-3 and k = 8 it multiplies surviving round-off by 1e24 — the 8th coefficient came out as -225789 instead of +0.0000248. Jet arithmetic truncated at order 1 is exactly forward-mode automatic differentiation, which is where §5.6 goes.
pch.quizShowAnswer
B — The jet uses no step at all. Its recurrences are algebraic identities between coefficients, so the chain rule is applied symbolically and only evaluated numerically — nothing nearly-equal is ever subtracted — The k-th difference divides by h to the k, so at h = 1e-3 and k = 8 it multiplies surviving round-off by 1e24 — the 8th coefficient came out as -225789 instead of +0.0000248. Jet arithmetic truncated at order 1 is exactly forward-mode automatic differentiation, which is where §5.6 goes.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – The secant converging, and then not
Section titled “Exercise 1 – The secant converging, and then not”Exercise 2 – Exercises 5.1, 5.2 and 5.3
Section titled “Exercise 2 – Exercises 5.1, 5.2 and 5.3”Exercise 3 – Both Taylor errors
Section titled “Exercise 3 – Both Taylor errors”Exercise 4 – Why repeated differencing fails
Section titled “Exercise 4 – Why repeated differencing fails”Exercise 5 – Jets: exact coefficients at every order
Section titled “Exercise 5 – Jets: exact coefficients at every order”Recall card
Section titled “Recall card”- The difference quotient is the slope of a secant; the derivative is the limit of those slopes, and the limit is a definition rather than an algorithm.
- A forward difference is first order and a central difference second order. Measured: best forward error 2.6e-10 at h = 1.5e-08, best central 2.9e-13 at h = 2.7e-06 — three orders better for one extra function evaluation.
- Making h smaller past the optimum makes things worse. At h = 1e-15 the central estimate is 2.8e+11 times worse than its own best, because subtracting two nearly equal numbers destroys digits.
- The Taylor polynomial of degree n is the sum of f-to-the-k at x0 over k factorial times (x − x0) to the k, and the Taylor series is its limit; at x0 = 0 it is the Maclaurin series, and f = T-infinity means f is analytic.
- For a polynomial of degree at most n, T_n is not an approximation — it is f, with every later coefficient exactly zero. Verified: T_4 of x^4 at x0 = 1 has max error 7.11e-15.
- Near x0 the error falls with degree; on a wide interval it need not. For sin + cos the near error falls 5.9e-1 to 2.1e-5 over six degrees while the far error rises 2.41 to 12.26 and then comes back.
- A Taylor series can fail to converge where the function is perfectly smooth. log(1+x) at 0 converges only for |x| < 1, because the radius is set by the nearest complex singularity.
- Symmetry changes the local order. For an odd function every even coefficient vanishes, so T_1 is already accurate to h-cubed; predicting h-to-the-(n+1) from the degree alone is wrong for most functions.
- Never estimate a high derivative by repeated differencing — the k-th difference divides by h to the k, so the 6th coefficient came out as +5.551 instead of -0.001389 and the 8th as -225789 instead of +0.0000248. Jet arithmetic is exact at every order and is forward-mode autodiff.
- The chain rule is the rule that matters. Everything in §5.2 to §5.6 is the chain rule with more indices.
Next: Partial Differentiation and Gradients — the same definition, one variable at a time.
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading