Derivatives and Rates of Change
The tangent slope and the instantaneous velocity from The Tangent and Velocity Problems are the same limit. Giving that limit a name, the derivative of at , unifies slopes, velocities and every other instantaneous rate of change.
The survey lesson The Derivative introduces it with examples. Here we state the definition in both standard forms, compute derivatives from the definition, and interpret as a rate with units.
The definition
The derivative of at is
if this limit exists. Writing gives the equivalent form
The quotient inside is the difference quotient, the slope of the secant through and . So:
- the tangent line to at is the line through that point with slope : ;
- if is a position, is the velocity at time , and the speed.
Computing from the definition
Example 1. at .
So and the tangent line is , i.e. .
Example 2. at , using the second form.
Example 3. at , rationalizing:
import sympy as sp
x, h = sp.symbols('x h')
for f, a in [(3*x**2 - 5*x, 2), (1/x, 4), (sp.sqrt(x), 9)]:
dq = (f.subs(x, a + h) - f.subs(x, a)) / h
print(f"f = {str(f):12} a = {a}: f'(a) = {sp.limit(dq, h, 0)}")f = 3*x**2 - 5*x a = 2: f'(a) = 7
f = 1/x a = 4: f'(a) = -1/16
f = sqrt(x) a = 9: f'(a) = 1/6
When the derivative does not exist
at : the difference quotient is , which is for and for . The one-sided limits differ, so does not exist; the graph has a corner. at gives : a vertical tangent, again no derivative. More on this in The Derivative as a Function.
Rates of change
If , the change over gives the average rate of change . Its limit as is the instantaneous rate of change, . Its units are units of per unit of .
| means | ||
|---|---|---|
| position (m) | time (s) | velocity (m/s) |
| cost of producing items (€) | items | marginal cost (€ per item) |
| temperature (°C) | depth (m) | temperature gradient (°C/m) |
| loss of a model | a weight | sensitivity of the loss to that weight |
Example. A server's memory use after minutes is MB. Then
so at memory grows at MB per minute, while the average over the first four minutes was MB/min. The rate is falling.
Estimating from data
When is only known at samples, approximate the limit with a small . The central difference is much more accurate than the one-sided quotient: its error shrinks like instead of .
import math
f, a, exact = math.sin, 1.0, math.cos(1.0)
print(f"{'h':>8} {'forward error':>15} {'central error':>15}")
for k in range(1, 9):
h = 10.0**-k
fwd = (f(a + h) - f(a)) / h
cen = (f(a + h) - f(a - h)) / (2*h)
print(f"{h:8.0e} {abs(fwd - exact):15.2e} {abs(cen - exact):15.2e}") h forward error central error
1e-01 4.29e-02 9.00e-04
1e-02 4.22e-03 9.00e-06
1e-03 4.21e-04 9.01e-08
1e-04 4.21e-05 9.00e-10
1e-05 4.21e-06 1.11e-11
1e-06 4.21e-07 2.77e-11
1e-07 4.18e-08 1.94e-10
1e-08 2.97e-09 2.58e-09
The forward error falls by 10 per step (order ), the central one by 100 per step (order ), until rounding takes over near : then a smaller makes things worse, because loses digits to cancellation.
Tip: A good default step for central differences in double precision is about , roughly the cube root of machine epsilon.
In software
- Gradient checking. Before trusting a hand-written backpropagation, ML engineers compare each partial derivative with a central difference; agreement to about relative error means the gradient code is right (Machine Learning).
- Marginal analysis in pricing or capacity planning is reading off : the extra cost of one more unit, the extra latency of one more request per second.
- Physics and animation compute velocity as the derivative of position; finite-difference velocity estimates from sampled positions (mouse input, motion capture) are this lesson's difference quotients.
Exercises
Exercise 1: From the definition
Use the definition to find for , and write the tangent line at .
Answer
; tangent .
Solution
Exercise 2: A rational function
Find for .
Answer
Solution
Exercise 3: Units
A tank's volume is litres after minutes, and . Interpret this in one sentence.
Answer
At 10 minutes the tank is draining at 3 litres per minute.
Solution
has units litres per minute; the negative sign means the volume is decreasing at that instant. Over the next short interval of minutes, the volume drops by about litres.
Exercise 4: Numerical derivative
Estimate the derivative of at with a central difference using , and report the error.
Answer
About , error about (the exact value is ).
Solution
The error of the central difference is about .
import math
h = 1e-4
est = (math.exp(h) - math.exp(-h)) / (2*h)
print(f"{est:.10f} error {abs(est - 1):.1e}")1.0000000017 error 1.7e-09
Test yourself
Which limit defines ?
- as
- as
- as
- as
If is the cost in euros of producing items, what are the units of ?
- euros
- items
- euros per item
- items per euro
Why does a central difference estimate get worse when is made extremely small (like 1e-12)?
- The truncation error grows
- Rounding error from subtracting nearly equal values dominates
- The derivative changes
- Python limits h to 1e-8