Derivatives: the slope at a point
About this lesson
A derivative tells you how fast a function changes at one exact point: the slope of its curve there. Follow one worked example, f(x) = x² at x = 3. Watch a secant line turn into the tangent as the step h shrinks, see the slopes 7, 6.1 and 6.01 close in on the limit 6, estimate it in Python (and see floats break when h is too small), then use the derivative to roll downhill with gradient descent, the core move of machine learning.
How steep is a curve?
A derivative measures how fast a function's output changes as its input changes, at one exact point. A straight line has one slope everywhere: rise ÷ run. The curve f(x) = x² gets steeper as x grows. How steep is it at x = 3?
>>> def f(x): return x**2 ... >>> f(3) 9 >>> [f(x) for x in range(5)] [0, 1, 4, 9, 16]
Read the lesson step by step
How steep is a curve?
A derivative measures how fast a function's output changes as its input changes, at one exact point. A straight line has one slope everywhere: rise ÷ run. The curve f(x) = x² gets steeper as x grows. How steep is it at x = 3?
>>> def f(x): return x**2 ... >>> f(3) 9 >>> [f(x) for x in range(5)] [0, 1, 4, 9, 16]Slope between two points
Take a second point Q a step h to the right of P. With h = 1, Q is (4, 16). The straight line through P and Q is called a secant. Its slope is rise ÷ run: (16 − 9) ÷ 1 = 7.
>>> def f(x): return x**2 ... >>> (f(4) - f(3)) / 1 # rise ÷ run 7.0Shrink the step
Now shrink h. With h = 0.1, Q is (3.1, 9.61) and the slope is 0.61 ÷ 0.1 = 6.1. With h = 0.01 it is 6.01. As Q slides toward P, the secant turns and its slope closes in on 6.
>>> def f(x): return x**2 ... >>> for h in [1, 0.1, 0.01]: ... print(h, (f(3 + h) - f(3)) / h) ... 1 7.0 0.1 6.100000000000012 0.01 6.009999999999849Algebra shows the target
Expand: (3 + h)² − 9 = 6h + h². Divide by h, and the secant's slope is exactly 6 + h for every h that is not 0. So as h shrinks toward 0, the slope gets as close to 6 as we like.
>>> from fractions import Fraction >>> h = Fraction(1, 100) >>> ((3 + h)**2 - 9) / h # exact, no rounding Fraction(601, 100) >>> 6 + h Fraction(601, 100)The limit is the derivative
That target is called a limit. The derivative is f′(x) = lim (f(x + h) − f(x)) ÷ h as h → 0. Here f′(3) = 6. The line through P with slope 6 is the tangent: it touches the curve at P.
>>> def f(x): return x**2 ... >>> for k in range(1, 6): ... h = 10**-k ... print(h, round((f(3 + h) - f(3)) / h, 6)) ... 0.1 6.1 0.01 6.01 0.001 6.001 0.0001 6.0001 1e-05 6.00001A slope for every x
At any x the same algebra gives (x + h)² − x² = 2xh + h², so f′(x) = 2x. Slide P along the curve: the tangent's slope is 2, 4, then 6 at x = 1, 2, 3. In general, xⁿ has derivative n·xⁿ⁻¹.
>>> [2*x for x in (1, 2, 3)] # f'(x) = 2x [2, 4, 6] >>> def g(x): return x**3 ... >>> round((g(2 + 1e-6) - g(2)) / 1e-6, 3) # 3·2² = 12 12.0Estimate it in code
A program can estimate a derivative with a small h: slope(x, h) returns (f(x + h) − f(x)) ÷ h. At x = 3 with h = 0.001 it gives 6.000999999999479. That is 6 + h, give or take a tiny rounding error.
>>> def f(x): return x**2 ... >>> def slope(x, h): ... return (f(x+h)-f(x))/h ... >>> slope(3, 0.001) 6.000999999999479Too small breaks it
A tinier h does not always help. A Python float keeps about 16 significant digits (the digits that carry real information). The same slope() with h = 10⁻⁸ gives 5.99999996, with 10⁻¹⁵ 5.33, and with 10⁻¹⁶, 3 + h rounds to 3: slope 0.0.
>>> def f(x): return x**2 ... >>> def slope(x, h): ... return (f(x+h)-f(x))/h ... >>> for h in [1e-8, 1e-15, 1e-16]: ... print(h, slope(3, h)) ... 1e-08 5.999999963535174 1e-15 5.329070518200751 1e-16 0.0 >>> 3 + 1e-16 == 3 TrueRolling downhill to learn
Machine learning uses derivatives to shrink an error. Gradient descent steps against the slope, with a step size (learning rate) of 0.25: x ← x − 0.25 · f′(x). From x = 3: 3 − 0.25 × 6 = 1.5, then 0.75, then 0.375.
>>> x = 3 >>> for step in range(3): ... x = x - 0.25 * 2*x # f'(x) = 2x ... print(x) ... 1.5 0.75 0.375
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01For f(x) = x², what is the slope of the secant from x = 3 to x = 3.5 (h = 0.5)?
f(3.5) = 12.25 and f(3) = 9, so the slope is (12.25 − 9) ÷ 0.5 = 6.5. It matches the rule 6 + h with h = 0.5.
02Using the power rule, what is the derivative of x³ at x = 2?
xⁿ has derivative n·xⁿ⁻¹, so x³ has derivative 3x². At x = 2 that is 3 × 4 = 12.
03Why does slope(3, 1e-16) return 0.0 in Python?
A float keeps about 16 significant digits. 3 + 0.0000000000000001 needs more, so it rounds to 3; the top of the fraction becomes 9 − 9 = 0.
04Gradient descent on f(x) = x² uses x ← x − 0.25 · f′(x). Starting at x = 2, where is x after one step?
f′(2) = 2 × 2 = 4, so x becomes 2 − 0.25 × 4 = 1. Each step halves x here, moving it toward the minimum at 0.
05What does the tangent line at a point have that a secant line does not?
A secant joins two points; as the second point slides in (h → 0), its slope approaches the tangent's slope, f′(x).
Glossary
- Slope
- How steep a line is: rise ÷ run, the change in y for each step of 1 in x.
- Secant
- A straight line through two points of a curve. Its slope is (f(x + h) − f(x)) ÷ h.
- Limit
- The value an expression gets as close to as we like when h gets close to 0, without h ever being 0.
- Derivative
- f′(x), the limit of the secant slopes as h → 0: how fast f changes at the point x. Also written dy/dx.
- Tangent
- The straight line that touches a curve at one point with the same slope as the curve there.
- Power rule
- xⁿ has derivative n·xⁿ⁻¹; for example x² has derivative 2x.
- Finite difference
- An estimate of a derivative made with a small but non-zero step h, as computers do.
- Gradient descent
- A way to find a minimum: repeatedly step against the slope, x ← x − rate · f′(x).
Notes and sources
- f′(x) (read 'f prime of x') and dy/dx are two ways to write the same derivative. The derivative of x² is 2x, so the slope of y = x² at x = 3 is 6.
- The slopes 6.1 and 6.01 are exact maths. Python prints 6.100000000000012 and 6.009999999999849, because 0.1 and 0.01 have no exact binary form and each float keeps 53 binary digits (about 16 decimal digits).
- A forward difference has two errors that pull in opposite ways: a large h is off by about h (here exactly h, since the slope is 6 + h), and a tiny h loses digits when two almost equal numbers are subtracted. For a float near 1, an h around 10⁻⁸ is a common compromise.
- The power rule: xⁿ has derivative n·xⁿ⁻¹ for every whole number n. So x³ has derivative 3x², and at x = 2 that is 12.
- With many inputs, each input gets its own slope (a partial derivative), and together they form the gradient vector. Training a neural network repeats the descent step on millions of inputs at once; backpropagation computes the gradient with the chain rule.
- MIT OpenCourseWare 18.01SC Single Variable Calculus, Unit 1: Differentiation
- Gilbert Strang, Calculus (MIT OpenCourseWare RES.18-001), chapter 2 Derivatives
- OpenStax Calculus Volume 1, 3.1 Defining the Derivative
- OpenStax Calculus Volume 1, 3.3 Differentiation Rules (power rule)
- Python tutorial: Floating-Point Arithmetic, Issues and Limitations
- Goodfellow, Bengio and Courville, Deep Learning, chapter 4 Numerical Computation (4.3 Gradient-Based Optimization)
- Python library: fractions (exact rational arithmetic)
Quiz Unlocks after the last step.