A First Look at the Derivative
How fast is a car going at exactly 3 seconds? Average speed over an interval is easy: distance divided by time. Speed at one instant is the limit of those averages as the interval shrinks to zero. That limit is the derivative, the central idea of differential calculus. This lesson builds it from slopes of secant lines, computes a few derivatives by hand and numerically, and shows where derivatives appear in code.
From average rate to instantaneous rate
The average rate of change of between and is the slope of the secant line through the two points:
As shrinks, the second point slides toward the first and the secant line turns into the tangent line, the line that just touches the curve at . Its slope is the derivative:
The difference quotient has the form at , so this is exactly the kind of limit from Introduction to Limits: simplify first, then let .
A worked example
A stone falls metres after seconds. What is its speed at ?
def s(t):
return 4.9 * t**2
print(" h average speed on [3, 3+h]")
for h in [1, 0.1, 0.01, 0.001, 0.0001]:
print(f"{h:<8} {(s(3 + h) - s(3)) / h:.6f}") h average speed on [3, 3+h]
1 34.300000
0.1 29.890000
0.01 29.449000
0.001 29.404900
0.0001 29.400490
The averages approach . Algebra shows why:
Doing the same with a general instead of 3 gives : the speed grows by every second, which is the acceleration due to gravity.
The derivative as a function
Repeating the calculation at every point gives a new function . A few results you will prove in calculus:
| Meaning | ||
|---|---|---|
| (constant) | 0 | a flat line has slope 0 |
| a line has the same slope everywhere | ||
| steeper the further from 0 | ||
| the power rule | ||
| the function that is its own slope |
The sign of tells you the shape: where increases, where it decreases, and at flat points such as the top of a hill or the bottom of a valley. That is how calculus finds maxima and minima.
import numpy as np
import matplotlib.pyplot as plt
f = lambda x: x**3 - 3 * x
a = 1.5
slope = 3 * a**2 - 3 # f'(x) = 3x² − 3
x = np.linspace(-2.5, 2.5, 300)
plt.figure(figsize=(6, 4))
plt.plot(x, f(x), "k", lw=2, label="f(x) = x³ − 3x")
for h, style in [(1.0, "C1--"), (0.4, "C2--")]:
m = (f(a + h) - f(a)) / h
plt.plot(x, f(a) + m * (x - a), style, label=f"secant, h = {h}: slope {m:.2f}")
plt.plot(x, f(a) + slope * (x - a), "C0", label=f"tangent: slope {slope:.2f}")
plt.plot([a], [f(a)], "ko")
plt.ylim(-4, 6); plt.legend(fontsize=8); plt.grid(alpha=0.3)
print("tangent slope at x = 1.5:", slope)tangent slope at x = 1.5: 3.75
The secants (dashed) tilt toward the tangent as shrinks.
In software
- Numerical derivatives. Code can approximate with a small . The central difference is far more accurate than the one-sided quotient. But cannot be too small: floating-point subtraction of nearly equal numbers loses digits.
- Gradient descent. Training a neural network means nudging each weight against the derivative of the loss, the slope that says which direction is downhill (Gradient Descent). Libraries compute those derivatives exactly with automatic differentiation rather than with small .
- Physics and animation. Velocity is the derivative of position, acceleration the derivative of velocity. A game estimates velocity from two frames as : a difference quotient.
import math
f, a, exact = math.sin, 1.0, math.cos(1.0)
print(" h forward error central error")
for h in [1e-1, 1e-3, 1e-5, 1e-8, 1e-11]:
fwd = (f(a + h) - f(a)) / h
cen = (f(a + h) - f(a - h)) / (2 * h)
print(f"{h:<8.0e} {abs(fwd - exact):14.2e} {abs(cen - exact):14.2e}") h forward error central error
1e-01 4.29e-02 9.00e-04
1e-03 4.21e-04 9.01e-08
1e-05 4.21e-06 1.11e-11
1e-08 2.97e-09 2.58e-09
1e-11 1.17e-06 1.17e-06
The error first shrinks as shrinks (the limit at work), then grows again when rounding dominates. Picking well is a real engineering decision. The full story starts in The Derivative and, in depth, Tangents and Velocity and The Derivative as a Function.
Exercises
Exercise 1: Average rate
Find the average rate of change of from to .
Answer
5
Solution
Exercise 2: Derivative from the definition
Use the limit definition to find for .
Answer
Solution
Exercise 3: Tangent line
Find the equation of the tangent line to at .
Answer
Solution
f = lambda x: x**3
h = 1e-5
print(round((f(2 + h) - f(2 - h)) / (2 * h), 6))12.0
Exercise 4: Where is it flat?
has . Where is the tangent horizontal, and what are the function values there?
Answer
At (, a local maximum) and (, a local minimum).
Solution
Test yourself
The derivative is the slope of…
- the secant line from 0 to
- the tangent line at
- the -axis
- the line through the origin and
Position is . What is the velocity at ?
- 10
- 20
- 40
- 5
Why can't you make as small as possible in a numerical derivative?
- Because must be an integer
- Because the limit does not exist
- Because floating-point rounding error grows when subtracting nearly equal numbers
- Because computers cannot divide by small numbers