Skip to content

Single-Variable Calculus Refresher

Chapter 5 does not teach calculus. It generalises it: the derivative becomes a gradient, the chain rule becomes backpropagation, the second derivative becomes a Hessian, and the Taylor series becomes a multivariate Taylor series. Each of those is a one-variable idea with more indices.

So this page is the one-variable version, done properly — rules derived from the limit definition, not listed, because a rule you have derived once is a rule you can rebuild when you forget it.

  • Every derivative rule, obtained from the difference quotient.
  • Why the chain rule is the one that matters most, and how to spot the “outer” function.
  • What higher derivatives measure, and what the second derivative tells you about a minimum.
  • Integration in its two guises — area, and undoing differentiation — and why they are the same thing.
  • The Taylor series, which is the single most reused idea in the whole module.

Intuition: the derivative is a rate, and it is local

Section titled “Intuition: the derivative is a rate, and it is local”

The derivative of ff at aa is the slope of the line that best matches ff near aa. “Near” is doing real work in that sentence: the derivative knows nothing about ff far away.

That locality is the whole reason gradient descent needs many small steps rather than one big one. The gradient tells you which way is downhill here; walk too far and you leave the region where that was true. Everything frustrating about learning rates traces back to this one property.

Your car’s odometer records position s(t)s(t). The speedometer shows s(t)s'(t) — the rate at which position changes. Over the whole trip, average speed is total distance over total time: a secant. The speedometer reading right now is a tangent. Calculus is the machinery for turning the first into the second, by shrinking the interval to nothing.

And the reverse trip is integration: given the speedometer trace for the whole journey, the distance covered is the area under it. Two operations, one relationship, and that relationship is the Fundamental Theorem.

diagram Diagram mermaid

The definition, and the two rules that fall straight out of it

Section titled “The definition, and the two rules that fall straight out of it”
f(x)  =  limh0f(x+h)f(x)hf'(x) \;=\; \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}

Linearity. Substituting f+gf + g and cfcf into the quotient and splitting the fraction gives

(f+g)=f+g,(cf)=cf.(f + g)' = f' + g', \qquad (cf)' = c f' .

Both are immediate, because a limit of a sum is the sum of the limits. This is the same linearity that Σ\Sigma has, and it is why a loss summed over a dataset can be differentiated term by term — the fact §5.2 relies on to make minibatch gradients legitimate.

For f(x)=xnf(x) = x^n with nn a positive integer, the binomial theorem gives

(x+h)n=xn+nxn1h+(n2)xn2h2+(x+h)^n = x^n + n x^{n-1} h + \binom{n}{2} x^{n-2} h^2 + \cdots

so

(x+h)nxnh=nxn1+(n2)xn2h+\frac{(x+h)^n - x^n}{h} = n x^{n-1} + \binom{n}{2} x^{n-2} h + \cdots

Every surviving term after the first still carries at least one factor of hh, so as h0h \to 0 they all vanish and

  ddxxn=nxn1  \boxed{\;\frac{\mathrm{d}}{\mathrm{d}x} x^n = n x^{n-1}\;}

That is the whole derivation, and it is worth having done once: the rule is not a convention, it is what the binomial expansion leaves behind.

The trick is to add and subtract the same thing:

f(x+h)g(x+h)f(x)g(x)h=f(x+h)g(x+h)f(x+h)g(x)+f(x+h)g(x)f(x)g(x)h=f(x+h)g(x+h)g(x)h  +  g(x)f(x+h)f(x)h\begin{aligned} \frac{f(x+h)g(x+h) - f(x)g(x)}{h} &= \frac{f(x+h)g(x+h) - f(x+h)g(x) + f(x+h)g(x) - f(x)g(x)}{h} \\[4pt] &= f(x+h)\,\frac{g(x+h) - g(x)}{h} \;+\; g(x)\,\frac{f(x+h) - f(x)}{h} \end{aligned}

Let h0h \to 0. The first factor f(x+h)f(x)f(x+h) \to f(x) by continuity, and the two quotients become derivatives:

  (fg)=fg+fg  \boxed{\;(fg)' = f'g + fg'\;}

The interpolation step — adding and subtracting f(x+h)g(x)f(x+h)g(x) — is the entire idea. It appears again in the proof of the multivariate chain rule in §5.2, so it is worth recognising.

The chain rule, and why it is the important one

Section titled “The chain rule, and why it is the important one”
  (fg)(x)=f(g(x))g(x)  \boxed{\;(f \circ g)'(x) = f'(g(x))\cdot g'(x)\;}

Read it as: the rate of change of the whole is the rate of the outer, evaluated where the inner puts you, times the rate of the inner. Rates multiply.

Sketch of why: write u=g(x)u = g(x) and

f(g(x+h))f(g(x))h=f(g(x+h))f(g(x))g(x+h)g(x)g(x+h)g(x)h\frac{f(g(x+h)) - f(g(x))}{h} = \frac{f(g(x+h)) - f(g(x))}{g(x+h) - g(x)} \cdot \frac{g(x+h) - g(x)}{h}

As h0h \to 0 the second factor becomes g(x)g'(x) and the first becomes f(u)f'(u). (The step is invalid when the denominator is zero, which a careful proof handles separately, but the mechanism is exactly this.)

Derivable from the product and chain rules by writing f/g=fg1f/g = f\cdot g^{-1}:

(fg)=fgfgg2\left(\frac{f}{g}\right)' = \frac{f'g - fg'}{g^2}

Worth remembering, but if it will not come: rewrite as a product and use the two rules you do remember.

f(x)f(x)f(x)f'(x)note
cc00a constant has no rate
xnx^nnxn1nx^{n-1}holds for all real nn, not just integers
exe^xexe^xthe function that is its own derivative
lnx\ln x1/x1/xfor x>0x > 0
sinx\sin xcosx\cos x
cosx\cos xsinx-\sin xnote the sign
σ(x)=11+ex\sigma(x) = \dfrac{1}{1+e^{-x}}σ(x)(1σ(x))\sigma(x)\bigl(1-\sigma(x)\bigr)the logistic derivative

The last row earns its place. The logistic sigmoid’s derivative is expressible in terms of the sigmoid itself, so a network that has already computed σ(x)\sigma(x) in its forward pass gets the derivative for one multiply. That is not an aesthetic nicety — it is why the formula is worth deriving:

σ(x)=ex(1+ex)2=11+exex1+ex=σ(x)(1σ(x))\sigma'(x) = \frac{e^{-x}}{(1+e^{-x})^2} = \frac{1}{1+e^{-x}}\cdot\frac{e^{-x}}{1+e^{-x}} = \sigma(x)\bigl(1 - \sigma(x)\bigr)

It also shows the problem: the maximum of σ(1σ)\sigma(1-\sigma) is 1/41/4 at x=0x = 0, and it decays towards zero in both tails. Every sigmoid layer multiplies the backward signal by at most 0.250.25, so ten stacked sigmoids attenuate a gradient by a factor of at least 4101064^{10} \approx 10^6. That is the vanishing-gradient problem, quantified from one derivative.

ff'' is the derivative of ff': the rate at which the rate is changing, which is curvature.

f(a)f''(a)shape at a stationary point f(a)=0f'(a) = 0
>0> 0curving up — a local minimum
<0< 0curving down — a local maximum
=0= 0inconclusive; look further

This is the second-derivative test, and §5.7 generalises it: in many variables ff'' becomes the Hessian matrix, and “positive” becomes “positive definite”. A stationary point where the Hessian has both positive and negative eigenvalues is a saddle — the multivariate case that has no one-variable analogue, and the one that dominates high-dimensional optimisation.

As area. abf(x)dx\int_a^b f(x)\,\mathrm{d}x is the signed area between the graph and the axis, defined as the limit of rectangle sums:

abf(x)dx  =  limni=1nf(xi)Δx,Δx=ban\int_a^b f(x)\,\mathrm{d}x \;=\; \lim_{n\to\infty} \sum_{i=1}^{n} f(x_i)\,\Delta x, \qquad \Delta x = \frac{b-a}{n}

Note what that is: a sum, with the limit taken. Everything from the sums page applies — linearity, splitting, factoring constants out — which is why (f+g)=f+g\int (f+g) = \int f + \int g needs no separate proof.

As antiderivative. FF is an antiderivative of ff when F=fF' = f.

The Fundamental Theorem of Calculus says these two are the same thing:

abf(x)dx  =  F(b)F(a)\int_a^b f(x)\,\mathrm{d}x \;=\; F(b) - F(a)

Compare that with the telescoping identity from the sums page: i(bibi1)=bnb0\sum_i (b_i - b_{i-1}) = b_n - b_0. Same statement. The interior cancels and the endpoints survive; the Fundamental Theorem is telescoping with the step size taken to zero.

Chapter 6 integrates constantly — a density integrates to one, an expectation is an integral, a marginal is an integral over the variable being removed — and every one of those is this operation.

The most reused idea in the module:

f(x)  =  k=0f(k)(a)k!(xa)k=f(a)+f(a)(xa)+f(a)2(xa)2+f(x) \;=\; \sum_{k=0}^{\infty} \frac{f^{(k)}(a)}{k!}\,(x-a)^k = f(a) + f'(a)(x-a) + \frac{f''(a)}{2}(x-a)^2 + \cdots

Truncate after the linear term and you have the tangent line: the linearisation of ff at aa. Truncate after the quadratic term and you have the best local parabola — which is exactly the model Newton’s method minimises at each step (§7.1), and exactly the object whose curvature the Hessian describes (§5.7).

Two standard expansions worth knowing by sight, both around a=0a = 0:

ex=1+x+x22+x36+,ln(1+x)=xx22+x33e^x = 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \cdots, \qquad \ln(1+x) = x - \frac{x^2}{2} + \frac{x^3}{3} - \cdots

The second is why ln(1+x)x\ln(1+x)\approx x for small xx, an approximation that turns up whenever a log-likelihood is expanded around its maximum.

Differentiate f(x)=x2e3xf(x) = x^2 e^{3x} and check it numerically at x=0.5x = 0.5.

By the product rule with u=x2u = x^2 and v=e3xv = e^{3x}. The chain rule gives v=3e3xv' = 3e^{3x}, and u=2xu' = 2x, so

f(x)=2xe3x+x23e3x=e3x(2x+3x2)f'(x) = 2x\,e^{3x} + x^2\cdot 3e^{3x} = e^{3x}\bigl(2x + 3x^2\bigr)

At x=0.5x = 0.5: e1.5=4.481689e^{1.5} = 4.481689, and 2(0.5)+3(0.25)=1+0.75=1.752(0.5) + 3(0.25) = 1 + 0.75 = 1.75, so

f(0.5)=4.481689×1.75=7.842956f'(0.5) = 4.481689 \times 1.75 = 7.842956

Numerically, with the central difference (f(x+h)f(xh))/2h\bigl(f(x+h)-f(x-h)\bigr)/2h:

hhcentral differenceabsolute error
10110^{-1}8.2332728.2332723.9×1013.9\times10^{-1}
10210^{-2}7.8468227.8468223.9×1033.9\times10^{-3}
10310^{-3}7.8429957.8429953.9×1053.9\times10^{-5}
10410^{-4}7.8429567.8429563.9×1073.9\times10^{-7}
10610^{-6}7.8429567.8429561.2×10101.2\times10^{-10}
10910^{-9}7.8429567.8429563.3×1083.3\times10^{-8}

Read the error column carefully. It falls by a factor of 100100 each time hh falls by 1010 — the central difference is second-order accurate, error proportional to h2h^2 — until h=109h = 10^{-9}, where it gets worse. At that point subtracting two nearly equal numbers has destroyed most of the significant digits, and rounding error dominates the truncation error.

That U-shape is the single most useful thing to know about numerical differentiation, and §5.5 uses it as the basis of gradient checking: pick hh near the bottom of the U, around 10510^{-5} to 10610^{-6} in double precision.

Drag the number of rectangles and watch the Riemann sum climb towards the true integral. Drag the second knob to switch which corner of each rectangle sets its height — left, right, or midpoint.

sketch A Riemann sum converging on an integral p5.js
Each rectangle's height is the function value at one point of its base. Drag n up and the total area approaches the true integral; the midpoint rule gets there fastest because its errors cancel in pairs.

Two things to notice. On the left rule the rectangles all sit below the curve and the sum is an underestimate; on the right rule they all poke above it. On the midpoint rule each rectangle is too low on one half of its base and too high on the other, and those errors cancel — so at the same nn it is dramatically more accurate. That cancellation is the same mechanism that makes the central difference beat the forward difference for derivatives.

figure Error against the number of rectangles matplotlib
Log-log plot of the absolute error of the left endpoint, right endpoint and midpoint Riemann sums against the number of rectangles. The endpoint rules fall along a line of slope minus one; the midpoint rule falls along a line of slope minus two. Log-log plot of the absolute error of the left endpoint, right endpoint and midpoint Riemann sums against the number of rectangles. The endpoint rules fall along a line of slope minus one; the midpoint rule falls along a line of slope minus two.
The endpoint rules lose one digit per tenfold increase in n; the midpoint rule loses two. Cancellation is worth an order of accuracy.
figure Why there is a best step size for a numerical derivative matplotlib
Log-log plot of the error in a numerical derivative against the step size h, for the forward and central difference formulas. Both curves fall as h decreases, reach a minimum, and then rise again as rounding error takes over. Log-log plot of the error in a numerical derivative against the step size h, for the forward and central difference formulas. Both curves fall as h decreases, reach a minimum, and then rise again as rounding error takes over.
Truncation error falls as h shrinks; rounding error grows. The bottom of the U is the usable h, around 1e-5 for the central difference in double precision.

The second figure is the one to keep. Its left branch is mathematics — the truncation error of the formula, falling like hh for the forward difference and h2h^2 for the central one, which is why the central curve is steeper. Its right branch is floating point — subtracting two numbers that agree to fifteen digits leaves noise, and dividing that noise by a tiny hh amplifies it.

The minimum sits where the two effects balance. For the central difference in float64 that is around h=6×106h = 6\times10^{-6}, with an achievable error near 7×10137\times10^{-13} — while the forward difference bottoms out four orders worse, at about 6×1096\times10^{-9}, and only at h108h \approx 10^{-8}. If you ever “verify” an analytic gradient by setting h=1015h = 10^{-15} to be safe, this plot is why the check fails.

operationone variablemany variables (Chapter 5)
first derivativef(x)f'(x), a numberf\nabla f, a vector — §5.2
chain rulef(g(x))g(x)f'(g(x))g'(x)Jacobian product — §5.2, §5.6
second derivativef(x)f''(x), a numberHessian H\mathbf{H}, a matrix — §5.7
minimum testf>0f'' > 0H\mathbf{H} positive definite — §5.7
linearisationtangent linetangent plane — §5.8
Taylor seriespowers of (xa)(x-a)powers with tensors — §5.8
integralarea under a curvevolume, and expectations — §6.4
pch.quizTag Check yourself
  1. The derivative of the logistic sigmoid can be written in terms of the sigmoid itself. What is its largest possible value?

    pch.quizShowAnswer

    B — One quarter — Sigma times one minus sigma is maximised when sigma is one half, giving one quarter. Every sigmoid layer therefore multiplies a backward signal by at most 0.25, which is the vanishing-gradient problem in one line.

  2. Why does making the step size in a numerical derivative smaller and smaller eventually make the answer worse?

    pch.quizShowAnswer

    B — Subtracting two nearly equal floating-point numbers destroys significant digits, and dividing by a tiny step amplifies the leftover noise — Truncation error falls with the step size while rounding error grows, giving a U-shaped total. The usable step size sits at the bottom, around 1e-5 for a central difference in double precision.

  3. What is the relationship between the Fundamental Theorem of Calculus and the telescoping sum identity?

    pch.quizShowAnswer

    B — The Fundamental Theorem is the telescoping identity with the step size taken to zero — Both say the same thing: sum a difference of consecutive values and everything interior cancels, leaving the two endpoints. Shrinking the step to zero turns the sum into an integral.

  4. At the same number of rectangles, the midpoint rule is far more accurate than the left or right endpoint rules. Why?

    pch.quizShowAnswer

    B — Each rectangle is too low over half its base and too high over the other half, so the errors cancel — Sampling at the midpoint makes each rectangle's error split into two pieces of opposite sign that largely cancel. That buys an extra order of accuracy — the same mechanism that makes the central difference beat the forward difference.

Exercise 1 – The power rule by numerical check

Section titled “Exercise 1 – The power rule by numerical check”

Exercise 2 – The sigmoid derivative in terms of itself

Section titled “Exercise 2 – The sigmoid derivative in terms of itself”

Exercise 5 – Taylor, one order at a time

Section titled “Exercise 5 – Taylor, one order at a time”
  • The derivative is local — it describes ff near a point and says nothing far away, which is why gradient descent takes many small steps.
  • Differentiation is linear, so a loss summed over a dataset differentiates term by term.
  • The power rule is what the binomial expansion leaves behind once every term carrying an hh has vanished.
  • The product rule comes from adding and subtracting the same interpolating term — a trick reused in the multivariate chain-rule proof.
  • The chain rule multiplies rates, so composing LL layers multiplies LL derivatives — which is vanishing and exploding gradients in one line.
  • The sigmoid derivative is at most one quarter, so ten stacked sigmoids attenuate a gradient by a factor of about a million.
  • The second derivative is curvature, and positive curvature at a stationary point means a minimum; in many variables that becomes a positive definite Hessian.
  • An integral is a sum with the limit taken, which is why integrals inherit linearity for free.
  • The Fundamental Theorem is telescoping with the step size taken to zero.
  • Numerical differentiation has a U-shaped error curve — truncation error falls with hh, rounding error grows, and the usable hh is at the bottom, near 10510^{-5}.
  • The midpoint rule beats the endpoint rules by an order because each rectangle’s two half-errors cancel.

Next: the one thing a real matrix can do that real numbers cannot describe — Complex Numbers in One Page.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading