TExcelTAD
Home › Calculus in Depth › Derivatives and Rates of Change

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 ff at aa, 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 f′(a)f'(a) as a rate with units.

The definition

The derivative of ff at aa is

f′(a)=lim⁡h→0f(a+h)−f(a)hf'(a) = \lim_{h \to 0} \frac{f(a + h) - f(a)}{h}

if this limit exists. Writing x=a+hx = a + h gives the equivalent form

f′(a)=lim⁡x→af(x)−f(a)x−af'(a) = \lim_{x \to a} \frac{f(x) - f(a)}{x - a}

The quotient inside is the difference quotient, the slope of the secant through (a,f(a))(a, f(a)) and (a+h,f(a+h))(a + h, f(a + h)). So:

  • the tangent line to y=f(x)y = f(x) at (a,f(a))(a, f(a)) is the line through that point with slope f′(a)f'(a): y−f(a)=f′(a)(x−a)y - f(a) = f'(a)(x - a);
  • if s(t)s(t) is a position, s′(a)s'(a) is the velocity at time aa, and ∣s′(a)∣\lvert s'(a) \rvert the speed.

Computing from the definition

Example 1. f(x)=3x2−5xf(x) = 3x^2 - 5x at a=2a = 2.

f(2+h)=3(4+4h+h2)−10−5h=2+7h+3h2f(2)=2f(2+h)−f(2)h=7h+3h2h=7+3h→7\begin{aligned} f(2 + h) &= 3(4 + 4h + h^2) - 10 - 5h = 2 + 7h + 3h^2 \\ f(2) &= 2 \\ \frac{f(2 + h) - f(2)}{h} &= \frac{7h + 3h^2}{h} = 7 + 3h \to 7 \end{aligned}

So f′(2)=7f'(2) = 7 and the tangent line is y−2=7(x−2)y - 2 = 7(x - 2), i.e. y=7x−12y = 7x - 12.

Example 2. g(x)=1/xg(x) = 1/x at a=4a = 4, using the second form.

1/x−1/4x−4=(4−x)/(4x)x−4=−14x→−116\frac{1/x - 1/4}{x - 4} = \frac{(4 - x)/(4x)}{x - 4} = -\frac{1}{4x} \to -\frac{1}{16}

Example 3. k(x)=xk(x) = \sqrt{x} at a=9a = 9, rationalizing:

9+h−3h=hh(9+h+3)=19+h+3→16\frac{\sqrt{9 + h} - 3}{h} = \frac{h}{h(\sqrt{9 + h} + 3)} = \frac{1}{\sqrt{9 + h} + 3} \to \frac{1}{6}
python · try it yourself
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

f(x)=∣x∣f(x) = \lvert x \rvert at 00: the difference quotient is ∣h∣/h\lvert h \rvert/h, which is 11 for h>0h > 0 and −1-1 for h<0h < 0. The one-sided limits differ, so f′(0)f'(0) does not exist; the graph has a corner. f(x)=x3f(x) = \sqrt[3]{x} at 00 gives h1/3/h=h−2/3→∞h^{1/3}/h = h^{-2/3} \to \infty: a vertical tangent, again no derivative. More on this in The Derivative as a Function.

Rates of change

If y=f(x)y = f(x), the change Δy=f(x2)−f(x1)\Delta y = f(x_2) - f(x_1) over Δx=x2−x1\Delta x = x_2 - x_1 gives the average rate of change Δy/Δx\Delta y/\Delta x. Its limit as Δx→0\Delta x \to 0 is the instantaneous rate of change, f′(x1)f'(x_1). Its units are units of yy per unit of xx.

yy xx f′(a)f'(a) means
position (m) time (s) velocity (m/s)
cost of producing xx 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 tt minutes is M(t)=200+40tM(t) = 200 + 40\sqrt{t} MB. Then

M(4+h)−M(4)h=40(4+h−2)h=404+h+2→404=10\frac{M(4 + h) - M(4)}{h} = \frac{40(\sqrt{4 + h} - 2)}{h} = \frac{40}{\sqrt{4 + h} + 2} \to \frac{40}{4} = 10

so at t=4t = 4 memory grows at 1010 MB per minute, while the average over the first four minutes was (280−200)/4=20(280 - 200)/4 = 20 MB/min. The rate is falling.

Estimating f′(a)f'(a) from data

When ff is only known at samples, approximate the limit with a small hh. The central difference f(a+h)−f(a−h)2h\frac{f(a + h) - f(a - h)}{2h} is much more accurate than the one-sided quotient: its error shrinks like h2h^2 instead of hh.

python · try it yourself
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 hh), the central one by 100 per step (order h2h^2), until rounding takes over near h=10−5h = 10^{-5}: then a smaller hh makes things worse, because f(a+h)−f(a−h)f(a + h) - f(a - h) loses digits to cancellation.

Tip: A good default step for central differences in double precision is about h≈10−5⋅max⁡(1,∣a∣)h \approx 10^{-5} \cdot \max(1, \lvert a \rvert), 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 10−710^{-7} relative error means the gradient code is right (Machine Learning).
  • Marginal analysis in pricing or capacity planning is reading off f′(a)f'(a): 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 f′(1)f'(1) for f(x)=x3+2xf(x) = x^3 + 2x, and write the tangent line at x=1x = 1.

Answer

f′(1)=5f'(1) = 5; tangent y=5x−2y = 5x - 2.

Solution
f(1+h)=1+3h+3h2+h3+2+2h=3+5h+3h2+h3f(1+h)−f(1)h=5+3h+h2→5tangent through (1,3): y−3=5(x−1)  ⟹  y=5x−2\begin{aligned} &f(1 + h) = 1 + 3h + 3h^2 + h^3 + 2 + 2h = 3 + 5h + 3h^2 + h^3 \\ &\frac{f(1 + h) - f(1)}{h} = 5 + 3h + h^2 \to 5 \\ &\text{tangent through } (1, 3){:}\ y - 3 = 5(x - 1) \implies y = 5x - 2 \end{aligned}

Exercise 2: A rational function

Find f′(2)f'(2) for f(x)=xx+2f(x) = \frac{x}{x + 2}.

Answer

18\frac{1}{8}

Solution
f(2+h)−f(2)=2+h4+h−12=2(2+h)−(4+h)2(4+h)=h2(4+h)divide by h: 12(4+h)→18\begin{aligned} &f(2 + h) - f(2) = \frac{2 + h}{4 + h} - \frac{1}{2} = \frac{2(2 + h) - (4 + h)}{2(4 + h)} = \frac{h}{2(4 + h)} \\ &\text{divide by } h{:}\ \frac{1}{2(4 + h)} \to \frac{1}{8} \end{aligned}

Exercise 3: Units

A tank's volume is V(t)V(t) litres after tt minutes, and V′(10)=−3V'(10) = -3. Interpret this in one sentence.

Answer

At 10 minutes the tank is draining at 3 litres per minute.

Solution

V′V' has units litres per minute; the negative sign means the volume is decreasing at that instant. Over the next short interval of Δt\Delta t minutes, the volume drops by about 3Δt3\Delta t litres.

Exercise 4: Numerical derivative

Estimate the derivative of f(x)=exf(x) = e^x at x=0x = 0 with a central difference using h=10−4h = 10^{-4}, and report the error.

Answer

About 1.00000000171.0000000017, error about 1.7×10−91.7 \times 10^{-9} (the exact value is 11).

Solution

The error of the central difference is about h2⋅f′′′(0)/6=10−8/6≈1.7×10−9h^2 \cdot f'''(0)/6 = 10^{-8}/6 \approx 1.7 \times 10^{-9}.

python · try it yourself
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 f′(a)f'(a)?

  1. lim⁡f(a+h)−f(a)h\lim \frac{f(a + h) - f(a)}{h} as h→0h \to 0
  2. lim⁡(f(a+h)−f(a))\lim (f(a + h) - f(a)) as h→0h \to 0
  3. lim⁡f(a+h)h\lim \frac{f(a + h)}{h} as h→0h \to 0
  4. lim⁡f(x)−f(a)x+a\lim \frac{f(x) - f(a)}{x + a} as x→ax \to a

If C(x)C(x) is the cost in euros of producing xx items, what are the units of C′(500)C'(500)?

  1. euros
  2. items
  3. euros per item
  4. items per euro

Why does a central difference estimate get worse when hh is made extremely small (like 1e-12)?

  1. The truncation error grows
  2. Rounding error from subtracting nearly equal values dominates
  3. The derivative changes
  4. Python limits h to 1e-8