Math for AILoading progress…

Secants and tangent slopes

Calculate slopes, use the chain rule and distinguish derivatives from partial derivatives.

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.

-2-1012302468
slope = (f(a + h) − f(a)) / h
= (4.0000 − 1.0000) / 1
= 3.0000   (orange line)
tangent slope f′(1) = 2.0000   (green dashes)
hslope from a to a + h
24.00000
13.00000
0.52.50000
0.12.10000
0.012.01000
0.0012.00100

The table uses positive h. Slide h through the negative values too: the slope arrives at the same limit from the other side.

Move h down through 2, 1, 0.5, 0.1 ... and also through the negative values. The table rows are for positive h and show the limit.
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.

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

f′(x)=lim⁡h→0f(x+h)−f(x)h.f'(x)=\lim_{h\to0}\frac{f(x+h)-f(x)}h. ddxf(g(x))=f′(g(x))g′(x),∂∂x(x2+3xy)=2x+3y.\frac{d}{dx}f(g(x))=f'(g(x))g'(x),\quad \frac{\partial}{\partial x}(x^2+3xy)=2x+3y.

For numerical checking, use (f(x+h)−f(x−h))/(2h)(f(x+h)-f(x-h))/(2h) 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.