Secants and tangent slopes
Calculate slopes, use the chain rule and distinguish derivatives from partial derivatives.
THE IDEA
A slope needs two points. A derivative needs one.
The slope between two points of a graph is easy: rise over run. For f(x) = x² between x = 1 and x = 3 the rise is f(3) − f(1) = 9 − 1 = 8 and the run is 2, so the slope is 4. The line through those two points is called a secant, and that number is an average rate of change over the whole interval. It says nothing about what the function is doing right at x = 1.
Training a model needs the local question: if I nudge this one number by a tiny amount, how fast does the output react? That is the derivative f′(a). It is the slope of the tangent, the line that just touches the curve at a. To find it we cannot use two points that are far apart, and we cannot use the same point twice (the run would be 0, and 0 / 0 means nothing). So we take a second point at a + h and squeeze h toward zero.
Slide the second point toward the first
Below, the blue dot is fixed at a and the orange dot sits at a + h. The orange line is the secant. As h shrinks the orange line rotates until it lies on top of the green dashed tangent. Watch the table: the slopes settle on one number. That number, the limit of the secant slopes, is the derivative.
| h | slope from a to a + h |
|---|---|
| 2 | 4.00000 |
| 1 | 3.00000 |
| 0.5 | 2.50000 |
| 0.1 | 2.10000 |
| 0.01 | 2.01000 |
| 0.001 | 2.00100 |
The table uses positive h. Slide h through the negative values too: the slope arrives at the same limit from the other side.
f = lambda x: x * x
for h in [1, 0.1, 0.01, 0.001]:
print(h, (f(1 + h) - f(1)) / h)
# output:
1 3.0
0.1 2.100000000000002
0.01 2.0100000000000007
0.001 2.0009999999996975
Notice the pattern: the slopes are 3, 2.1, 2.01, 2.001 (the tiny digits at the end are floating-point noise, which we meet again in lesson M4.6). For x² at a the secant slope is exactly 2a + h, so the limit as h goes to 0 is 2a. We write this f′(a) = lim (f(a + h) − f(a)) / h.
Predict first. For f(x) = x², what is the slope of the secant from x = 1 to x = 1.5?
The limit may not exist
The phrase "where the limit exists" matters. Take the secant slopes of |x| at 0: from the right they are all +1, from the left all −1. They never settle on one number, so |x| has no derivative at 0 (a corner). Smooth curves such as x² and sin x have tangents everywhere.
Chapter context
A derivative measures a local rate of change. It describes what a function does under a very small input change, rather than its average behavior over an arbitrary interval. This local sensitivity is what training algorithms use.
TRY IT YOURSELF
Part 1. Do it on the graph
Move the controls. Each task ticks from the real numbers on the screen and stays ticked once earned.
The function is f(x) = x². Slide the base point a and the step h.
| h | slope from a to a + h |
|---|---|
| 2 | 3.00000 |
| 1 | 2.00000 |
| 0.5 | 1.50000 |
| 0.1 | 1.10000 |
| 0.01 | 1.01000 |
| 0.001 | 1.00100 |
The table uses positive h. Slide h through the negative values too: the slope arrives at the same limit from the other side.
- To do: Choose the base point a and the step h so that the secant slope of x² is exactly 4
- To do: Put a at x = 1 and make the secant slope within 0.005 of the tangent slope 2
- To do: Approach from the left: use a negative h at a = 1 and still land within 0.05 of 2
- To do: Find the point where the tangent is flat: put a where the secant slope is within 0.005 of 0 for a tiny h
Part 2. Do it in code
- Write secant_slope(f, x, h): the slope of the line through (x, f(x)) and (x + h, f(x + h)). h = 0 has no meaning, so raise ValueError.
- Write slope_table(f, x, hs): a list of (h, slope) pairs, one for each h.
- Write estimate_derivative(f, x, tol): start with h = 1.0, halve h each round, and stop when two successive secant slopes differ by less than tol. Return the latest slope.
Stuck, or something not working? You can come back to it any time.
CHECK YOUR UNDERSTANDING
For f(x)=x squared, what is the slope between 1 and 3?
Chapter reference: math, worked example & code
This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.
Math and notation
For numerical checking, use and report a tolerance.
Worked example
Let f(x)=(3x+1)^2. At x=2, the inner value is 7, its derivative is 3, and the outer derivative is 14. Multiplying gives 42. A central difference with h=0.00001 gives approximately the same answer, within numerical error.
Reference code
The following example uses the Python standard library and does not call a remote service.
from math import isclose
f = lambda x: (3*x + 1)**2
x, h = 2, 1e-5
analytic = 6*(3*x + 1)
numeric = (f(x+h)-f(x-h))/(2*h)
assert analytic == 42
assert isclose(numeric, analytic, rel_tol=1e-7)
Python examples are provided for study and local execution; this page does not execute Python.
Common mistakes
A function value is not its derivative. The chain rule multiplies sensitivities, not intermediate values. At a corner such as abs(x) at zero, a two-sided derivative does not exist.