Lesson 3.8 · Stage 03: The maths behind machine learning

Maths chapter 6: Calculus basics: how fast things change

The derivative tells you how fast a function's output changes when you nudge its input. ML uses it to find which way to adjust a model to reduce its error.

Published 27 November 2026

The derivative tells you how fast a function’s output changes when you nudge its input. ML uses it to find which way to adjust a model to reduce its error.

6.1 Slope of a curve

A straight line has one slope everywhere (Chapter 1). A curve, like a hill, is steeper in some places than others. The derivative is the slope at one exact point.

Everyday example. A car’s speedometer shows how fast distance is changing right now. Speed is the derivative of distance with respect to time.

6.2 Finding a slope by nudging

Take f(x) = x². What is the slope at x = 3?

  1. Nudge x a tiny bit: from 3 to 3.01.
  2. Output changes from 9 to 3.01² = 9.0601, a rise of 0.0601.
  3. Slope ≈ rise ÷ run = 0.0601 ÷ 0.01 = 6.01.

Make the nudge smaller and smaller, and the answer gets closer to exactly 6. That limiting value is the derivative:

f′(x)=lim⁡h→0f(x+h)−f(x)hf'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}

We write the derivative as f′(x) or df/dx (“the change in f per change in x”).

6.3 Rules, so you never nudge by hand

Function Derivative Example
a number c 0 7 → 0
xⁿ n·xⁿ⁻¹ x³ → 3x²
c·f(x) c·f′(x) 5x² → 10x
f(x) + g(x) f′(x) + g′(x) x² + x → 2x + 1
eˣ eˣ (it is its own slope)
ln x 1/x

Worked example. f(x) = 3x² + 5x − 2. Derivative = 3 × 2x + 5 + 0 = 6x + 5. At x = 1 the slope is 11.

Check the first rule against 6.2: x² → 2x, and at x = 3 that gives 6.

6.4 The chain rule

When one function is inside another, multiply the slopes, outside first, then inside.

ddxf(g(x))=f′(g(x))×g′(x)\frac{d}{dx} f(g(x)) = f'(g(x)) \times g'(x)

Worked example. y = (2x + 1)³. The outside is “something cubed”, the inside is 2x + 1.

  1. Slope of the outside: 3 × (2x + 1)².
  2. Slope of the inside: 2.
  3. Multiply: y′ = 6(2x + 1)². At x = 1 this is 6 × 9 = 54.

Gear analogy: if gear A turns 3 times for each turn of gear B, and B turns 2 times for each turn of C, then A turns 3 × 2 = 6 times for each turn of C.

Why ML needs it: a neural network is functions inside functions inside functions. The chain rule is how we get its slopes. That process is backpropagation (Chapter 7).

6.5 Slope zero means top or bottom

At the bottom of a valley or the top of a hill, the ground is flat: the derivative is 0.

Worked example. f(x) = x² − 4x + 5. Derivative 2x − 4 = 0 gives x = 2. f(2) = 4 − 8 + 5 = 1. This is the lowest point of the curve.

Finding the lowest error is the whole goal of training a model.

Try it

  1. Find the derivative of x⁴.
  2. Find the derivative of 5x³ − 2x + 7.
  3. What is the slope of x² at x = −2?
  4. Use the chain rule on (3x − 1)².
  5. Where is x² − 6x + 10 smallest, and what is the smallest value?