SafeZone AI Learn
Learn/ Mathematical Foundations/ Optimization · lesson 1 of 7

Calculus and Taylor approximation

The derivative as the best local lie, Taylor series as lying to higher order — watched converging and diverging live — and the audit of every 'expand to first order' this curriculum will ever perform.

Where this came from. Fermat noticed (1630s) that curves are momentarily flat at their peaks — so “find the best” became “find where the rate of change vanishes”, a computation instead of a search. Taylor (1715) added that smooth functions are locally polynomial; Cauchy (1847), buried in orbital arithmetic, turned both into “just step downhill”. The whole story is in How the mathematics was forged; this lesson sharpens the tools themselves.

Every module ahead performs the same sleight of hand, usually in a half-sentence: “expanding to first order…”, “to second order, the loss is locally quadratic…”. This lesson is that half-sentence given a full hearing — because gradient descent, Newton’s method, the √d in attention, the Hessian’s bowls and saddles, and every convergence proof in the track are corollaries of one idea: smooth functions are boring up close, and boring is computable.

The derivative: the best local lie

Forget “slope of the tangent” for a moment; take the definition this curriculum actually uses. The derivative at aa is the number f(a)f'(a) that makes the linear approximation

f(a+h)    f(a)+f(a)hf(a + h) \;\approx\; f(a) + f'(a)\, h

as good as a linear function can be: the error vanishes faster than hh (o(h)o(h); with a bounded second derivative — the usual case in this track — the lie shrinks quadratically while the step shrinks linearly). That “best local lie” framing is why the derivative rules the track: an optimizer never sees the true loss surface, only this lie at the current point — and steps as if the lie were true. Small steps, good lie, progress; big steps, stale lie, divergence — you have already felt this trade if you have touched the Gradient Descent lesson’s η slider, and now you know its name: the approximation error of a first-order Taylor expansion.

Three mechanical rules generate every derivative in this curriculum — product, quotient, and above all the chain rule, (fg)=f(g(x))g(x)(f \circ g)' = f'(g(x)) \cdot g'(x): the lie of a composition is the product of the lies. Backpropagation (Deep Learning module) is this sentence, industrialized across a billion parameters; the Matrix Calculus lesson upgrades it to vectors. What deserves emphasis here is the meaning: derivatives compose multiplicatively, which is why deep products of them vanish or explode (the RNN lesson’s theorem) and why “the Jacobian is the local linear map” (Linear Algebra module) makes every linear-algebra fact a statement about nonlinear systems too.

Taylor: lying to higher order

If one linear term makes a good local lie, more polynomial terms make a better one:

f(a+h)  =  f(a)+f(a)h+f(a)2!h2++f(n)(a)n!hn+Rn,Rn=f(n+1)(ξ)(n+1)!hn+1f(a+h) \;=\; f(a) + f'(a)h + \frac{f''(a)}{2!}h^2 + \cdots + \frac{f^{(n)}(a)}{n!}h^n + R_n, \qquad R_n = \frac{f^{(n+1)}(\xi)}{(n+1)!} h^{n+1}

— each term corrects the previous lie with the next derivative’s information, and the remainder RnR_n (Lagrange’s form, with ξ\xi somewhere in the gap) prices the residual dishonesty. Two truncations carry the entire track: first order (f(a)+f(a)hf(a) + f'(a)h) is what gradient descent believes; second order (+12f(a)h2+\tfrac12 f''(a)h^2) is what Newton’s method and every Hessian argument believes — the loss as a local bowl, curvature and all. Watch the whole machinery, including its famous failure mode:

Start with sin at order 1: the tangent line, honest for a whisker around a, wrong everywhere else — this line is all gradient descent ever sees. Raise the order and watch the polynomial hug the sine wave outward, wiggle by wiggle: by n = 7 it is faithful over a full period, and the error readout at a + 0.5 collapses by orders of magnitude per term. That is Taylor behaving. Now switch to ln(1+x) and meet the fine print: the red lines mark the RADIUS OF CONVERGENCE, and outside them raising n makes the fit catastrophically WORSE — the polynomial’s tail thrashes harder with every term you add. More effort, more wrong: a series is a local promise, never a global one. Slide the expansion point a and watch the trusted window travel with it — which is precisely why iterative optimizers RE-expand at every step instead of trusting one expansion forever, and why “the loss is locally quadratic” always comes with the word locally doing heavy, load-bearing work.

The track’s expansions, audited in advance

The honest ledger of every Taylor moment you are about to meet, so none of them slips by as hand-waving:

  • Gradient descent’s step rule (next lessons): expand to second order, note the first-order term shrinks the loss while the second-order term punishes big steps by curvature — the step-size bound η<2/L\eta < 2/L falls straight out of R1R_1.
  • Newton’s method: trust the quadratic lie completely and jump to its minimum — brilliant where the lie is good, catastrophic where it isn’t (the same radius-of-convergence morality as the widget).
  • The Hessian’s geometry (Matrix Calculus): the second-order term 12hHh\tfrac12 h^\top H h IS the bowls-saddles-ravines picture; the quadratic-form widget you may already have met is a Taylor term with the truncation taken seriously.
  • Momentum, Adam, curvature arguments (Optimization module): every “the loss is locally an ill-conditioned quadratic” premise is a second-order expansion plus the hope that third-order terms stay small over a step.
  • Softmax saturation, sigmoid gradients, log(1+x) tricks across the DL and LLM tracks: one-term expansions used exactly within their trusted windows — now you can check the window yourself.

One closing distinction worth carrying: this lesson’s guarantees are local (Taylor’s theorem says nothing beyond the radius) while the next lesson’s subject — convexity — is the rare global promise. The track’s arc is exactly that tension: local lies you can always compute, global structure you occasionally get for free, and algorithms that stitch thousands of local lies into a path that works anyway.

import numpy as np
from math import factorial

# Taylor for sin around 0, error vs order at x = 0.5 — the widget's readout
x, a = 0.5, 0.0
for n in [1, 3, 5, 7, 9]:
    P = sum(np.sin(a + k * np.pi / 2) / factorial(k) * (x - a) ** k
            for k in range(n + 1))
    print(n, abs(P - np.sin(x)))       # collapses ~2 orders of magnitude per step

# and the failure: ln(1+x) at x = 2.5, outside radius 1 from a = 0
x = 2.5
for n in [2, 6, 12, 20]:
    P = sum((-1) ** (k - 1) / k * x ** k for k in range(1, n + 1))
    print(n, abs(P - np.log(1 + x)))   # GROWS with n — more terms, more wrong

Exercises

Work these before the next lesson

  1. Prove the “best lie” claim: show that among all linear functions L(h) = c₀ + c₁h, the choice c₀ = f(a), c₁ = f′(a) is the unique one whose error is o(h). What goes wrong for a function with a kink (|x| at 0), and which activation function makes this practically relevant?
    Solution

    Worked solutions are part of Premiumunlock all of them for £5/month →

  2. 5 more exercises — each with a worked solution — are part of Premium. Unlock everything for £5/month →

References

  • M. Spivak, Calculus — the rigorous backbone, ch. 19–20 for Taylor’s theorem with remainders.
  • B. Taylor, Methodus Incrementorum, 1715 — the original, for the historically brave.
  • A.-L. Cauchy, “Méthode générale pour la résolution des systèmes d’équations simultanées”, 1847 — gradient descent, in four pages.
  • 3Blue1Brown, “Essence of calculus” series — the geometric intuition this lesson’s algebra formalizes.