Ran Wei/ Calculus/Module 4
中文
Calculus · Part II · Derivatives

Module 4The derivative

Turn the average rates of Module 1 into instantaneous ones. You will define the derivative as a limit, read it as the slope of a tangent line and as a velocity, prove the first rules of differentiation, and compute derivatives by computer, exactly with SymPy and numerically, where small steps meet the limits of floating-point arithmetic.

⏱ 255 minYou need: Modules 1, 2 and 3

In this module you will

  • Compute derivatives from the limit definition, and write the equation of a tangent line
  • Interpret a derivative as a slope, a velocity and a rate of change, with its units
  • Sketch the graph of f′ from the graph of f, and recognise where a function has no derivative
  • Prove the power, constant multiple and sum rules, and use them to differentiate polynomials
  • Find higher derivatives, and the velocity, acceleration and jerk of a moving object
  • Estimate derivatives numerically, and choose a step that balances truncation and rounding error
1

Tangent lines and velocity

In Module 1 you dropped a ball. After tt seconds it has fallen s(t)=4.9t2s(t) = 4.9t^2 metres, and from t=1t = 1 to t=1+ht = 1 + h its average speed is

s(1+h)−s(1)h=9.8+4.9h(h≠0),\frac{s(1 + h) - s(1)}{h} = 9.8 + 4.9h \qquad (h \ne 0),

as Example 1.12 showed. As hh shrinks towards 0, from either side, these averages settle on 9.8 m/s. Module 2 gave that settling a precise meaning, a limit, and found the speed of the ball at every time a,a\mathclose{\text{,}} not only at t=1:t = 1\mathclose{\text{:}}

lim⁡h→0s(a+h)−s(a)h=lim⁡h→0 (9.8a+4.9h)=9.8a m/s.\begin{aligned} \lim_{h\to0}\frac{s(a + h) - s(a)}{h} &= \lim_{h\to0}\,(9.8a + 4.9h) \\ &= 9.8a \text{ m/s}. \end{aligned}

The quotient equals 9.8a+4.9h9.8a + 4.9h for every h≠0,h \ne 0\mathclose{\text{,}} and a limit as h→0h \to 0 never uses the value at h=0h = 0 itself; then direct substitution gives 9.8a.9.8a\mathclose{\text{.}} So the ball falls at 9.8 m/s after one second and at 19.6 m/s after two. This limit is what a speedometer on the ball would show.

Definition 4.1Instantaneous velocity

Suppose an object moves along a straight line, and s(t)s(t) is its position at time t,t\mathclose{\text{,}} measured from a fixed point. Its instantaneous velocity, or velocity for short, at time aa is

v(a)=lim⁡h→0s(a+h)−s(a)h,v(a) = \lim_{h\to0}\frac{s(a + h) - s(a)}{h},

provided that this limit exists. Its speed at time aa is ∣v(a)∣.\abs{v(a)}\mathclose{\text{.}}

Velocity has a sign: a positive velocity means that the object is moving in the positive direction, a negative one that it is moving the other way. Speed ignores the direction. For the falling ball, with distances measured downwards, the two are the same.

Example 4.1A braking car✓ Checked

Suppose a driver brakes steadily, and tt seconds after braking begins the car has travelled s(t)=20t−2t2s(t) = 20t - 2t^2 metres. (The numbers are made up.) Find the velocity at time a,a\mathclose{\text{,}} what the speedometer shows at t=0t = 0 and at t=2,t = 2\mathclose{\text{,}} and when and where the car stops.

Substitute a+ha + h for t,t\mathclose{\text{,}} keeping the brackets, and expand, with (a+h)2=a2+2ah+h2:(a + h)^2 = a^2 + 2ah + h^2\mathclose{\text{:}}

s(a+h)=20(a+h)−2(a+h)2=20a+20h−2(a2+2ah+h2)=20a+20h−2a2−4ah−2h2.\begin{aligned} &s(a + h) \\ &\quad= 20(a + h) - 2(a + h)^2 \\ &\quad= 20a + 20h - 2(a^2 + 2ah + h^2) \\ &\quad= 20a + 20h - 2a^2 - 4ah - 2h^2. \end{aligned}

Subtract s(a)=20a−2a2:s(a) = 20a - 2a^2\mathclose{\text{:}} the terms 20a20a and −2a2-2a^2 cancel, and s(a+h)−s(a)=20h−4ah−2h2.s(a + h) - s(a) = 20h - 4ah - 2h^2\mathclose{\text{.}} Every remaining term contains h,h\mathclose{\text{,}} so divide by hh (allowed, since h≠0),h \ne 0\mathclose{\text{),}} then let h→0:h \to 0\mathclose{\text{:}}

v(a)=lim⁡h→020h−4ah−2h2h=lim⁡h→0 (20−4a−2h)=20−4a m/s.\begin{aligned} v(a) &= \lim_{h\to0}\frac{20h - 4ah - 2h^2}{h} \\ &= \lim_{h\to0}\,(20 - 4a - 2h) \\ &= 20 - 4a \text{ m/s}. \end{aligned}

At t=0t = 0 the speedometer shows 20 m/s, which is 72 km/h (multiply m/s by 3.6 to get km/h); at t=2t = 2 it shows 20−8=1220 - 8 = 12 m/s. The velocity falls by 4 m/s every second and reaches 0 when 20−4a=0,20 - 4a = 0\mathclose{\text{,}} at t=5t = 5 s. By then the car has travelled s(5)=100−50=50s(5) = 100 - 50 = 50 m. The formula describes the car only for 0≤t≤5:0 \le t \le 5\mathclose{\text{:}} after t=5t = 5 it would have the car reversing, which brakes do not do.

The tangent line

Module 1 described the tangent line at a point PP of a curve as the line that the secant lines through PP settle on as a second point QQ slides towards PP (Figure 1.5 shows it for the ball). The slope of the secant through P=(a,f(a))P = (a, f(a)) and Q=(a+h,f(a+h))Q = (a + h, f(a + h)) is the difference quotient, so the slope of the tangent is the limit of the difference quotients: the same limit as the velocity.

Definition 4.2Tangent line

Let ff be defined on an open interval that contains a,a\mathclose{\text{,}} and let P=(a,f(a)).P = (a, f(a))\mathclose{\text{.}} If the limit

m=lim⁡h→0f(a+h)−f(a)hm = \lim_{h\to0}\frac{f(a + h) - f(a)}{h}

exists, the tangent line to the graph of ff at PP is the line through PP with slope m.m\mathclose{\text{.}} By the point-slope form of Section 1.4, its equation is

y=f(a)+m(x−a).y = f(a) + m(x - a).

The open interval makes sure that f(a+h)f(a + h) is defined for every small h,h\mathclose{\text{,}} positive or negative, so that secants can approach from both sides.

Example 4.2The tangent to y=x3y = x^3 at (1,1)(1, 1)✓ Checked

Find the tangent line to y=x3y = x^3 at the point (1,1),(1, 1)\mathclose{\text{,}} and find where else it meets the curve.

In Exercise 1.8 you expanded (1+h)3=1+3h+3h2+h3(1 + h)^3 = 1 + 3h + 3h^2 + h^3 and found, for h≠0,h \ne 0\mathclose{\text{,}}

(1+h)3−1h=3h+3h2+h3h=3+3h+h2.\begin{aligned} \frac{(1 + h)^3 - 1}{h} &= \frac{3h + 3h^2 + h^3}{h} \\ &= 3 + 3h + h^2. \end{aligned}

The table shows these secant slopes on both sides of P:P\mathclose{\text{:}}

hh(1+h)3−1h\dfrac{(1+h)^3 - 1}{h}
0.10.13.313.31
0.010.013.03013.0301
0.0010.0013.0030013.003001
−0.001-0.0012.9970012.997001
−0.01-0.012.97012.9701
−0.1-0.12.712.71

They approach 3, from above when h>0h > 0 and from below when h<0.h < 0\mathclose{\text{.}} By direct substitution, m=lim⁡h→0(3+3h+h2)=3,m = \lim_{h\to0}(3 + 3h + h^2) = 3\mathclose{\text{,}} so the tangent line is y=1+3(x−1),y = 1 + 3(x - 1)\mathclose{\text{,}} that is,

y=3x−2.y = 3x - 2.

It meets the curve where x3=3x−2,x^3 = 3x - 2\mathclose{\text{,}} that is, where x3−3x+2=0.x^3 - 3x + 2 = 0\mathclose{\text{.}} Since x=1x = 1 is a solution, x−1x - 1 is a factor (Module 2). To find the other factor, write −3x+2-3x + 2 as −x−(2x−2)-x - (2x - 2) and group the terms:

x3−3x+2=(x3−x)−(2x−2)=x(x−1)(x+1)−2(x−1)=(x−1)(x2+x−2)=(x−1)2(x+2),\begin{aligned} &x^3 - 3x + 2 \\ &\quad= (x^3 - x) - (2x - 2) \\ &\quad= x(x - 1)(x + 1) - 2(x - 1) \\ &\quad= (x - 1)(x^2 + x - 2) \\ &\quad= (x - 1)^2(x + 2), \end{aligned}

where the last step factors x2+x−2=(x−1)(x+2).x^2 + x - 2 = (x - 1)(x + 2)\mathclose{\text{.}} So the line meets the curve again at x=−2,x = -2\mathclose{\text{,}} at the point (−2,−8).(-2, -8)\mathclose{\text{.}} The repeated factor (x−1)2(x - 1)^2 is the algebraic mark of a tangent: the line meets the curve “twice” at x=1.x = 1\mathclose{\text{.}}

Watch out

“A tangent line touches the curve at only one point.” That is true for a circle, not in general: the tangent of Example 4.2 crosses y=x3y = x^3 again at (−2,−8).(-2, -8)\mathclose{\text{.}} And a line that meets a curve only once need not be a tangent: the vertical line x=1x = 1 meets y=x3y = x^3 only at (1,1).(1, 1)\mathclose{\text{.}} What makes a line the tangent at PP is its slope, the limit of the secant slopes.

Zoom in on the point of tangency and you see why the tangent deserves its name. Figure 4.1 magnifies the graph of y=x3y = x^3 around (1,1)(1, 1) tenfold, and then tenfold again. In the first window the curve bends away from its tangent, in the second much less, and in the third the two cannot be told apart. Close to the point, the curve looks straight, and the straight line it looks like is the tangent.

Zooming in on y = x^3 (blue) at the point (1, 1), with its tangent line y = 3x - 2 (dashed). Each box marks the window of the next panel, ten times smaller in both directions. In the last window, 0.03 wide, the curve and the tangent look the same.
Figure 4.1

Zooming in on y=x3y = x^3 (blue) at the point (1,1),(1, 1)\mathclose{\text{,}} with its tangent line y=3x−2y = 3x - 2 (dashed). Each box marks the window of the next panel, ten times smaller in both directions. In the last window, 0.03 wide, the curve and the tangent look the same.

2

The derivative

Velocity and the slope of a tangent are the same limit. So is every other instantaneous rate of change: how fast water drains from a tank, in litres per second; how fast a cup of tea cools, in degrees per minute; how fast the error of a machine-learning model changes as one of its parameters changes. The limit gets its own name.

Definition 4.3Derivative at a point

Let ff be defined on an open interval that contains a.a\mathclose{\text{.}} The derivative of ff at aa is

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

provided that this limit exists (as a real number). Then ff is differentiable at aa. The symbol f′(a)f'(a) reads “f prime of a”.

Putting x=a+hx = a + h gives a second form, often more convenient:

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

The two forms say the same thing. For every x≠a,x \ne a\mathclose{\text{,}} the quotient in the second form at xx equals the quotient in the definition at h=x−a;h = x - a\mathclose{\text{;}} and ∣x−a∣<δ\abs{x - a} < \delta is the same condition as ∣h∣<δ,\abs{h} < \delta\mathclose{\text{,}} so by the ε–δ definition of Module 2 one limit exists exactly when the other does, with the same value.

With this name, three ideas become one.

Key idea

The derivative f′(a)f'(a) is

  • the slope of the tangent line to y=f(x)y = f(x) at (a,f(a)),(a, f(a))\mathclose{\text{,}} which is y=f(a)+f′(a)(x−a);y = f(a) + f'(a)(x - a)\mathclose{\text{;}}
  • the instantaneous rate of change of ff at a,a\mathclose{\text{,}} measured in units of ff per unit of x;x\mathclose{\text{;}}
  • the velocity at time a,a\mathclose{\text{,}} when f(t)f(t) is the position of a moving object at time t.t\mathclose{\text{.}}
Example 4.3A derivative from the definition✓ Checked

Let f(x)=2x2−5x+1.f(x) = 2x^2 - 5x + 1\mathclose{\text{.}} Find f′(2)f'(2) and the tangent line to the graph at x=2.x = 2\mathclose{\text{.}}

First, f(2)=8−10+1=−1.f(2) = 8 - 10 + 1 = -1\mathclose{\text{.}} Next substitute 2+h,2 + h\mathclose{\text{,}} keeping the brackets, and expand, with (2+h)2=4+4h+h2:(2 + h)^2 = 4 + 4h + h^2\mathclose{\text{:}}

f(2+h)=2(2+h)2−5(2+h)+1=2(4+4h+h2)−10−5h+1=8+8h+2h2−10−5h+1=−1+3h+2h2.\begin{aligned} &f(2 + h) \\ &\quad= 2(2 + h)^2 - 5(2 + h) + 1 \\ &\quad= 2(4 + 4h + h^2) - 10 - 5h + 1 \\ &\quad= 8 + 8h + 2h^2 - 10 - 5h + 1 \\ &\quad= -1 + 3h + 2h^2. \end{aligned}

So f(2+h)−f(2)=3h+2h2,f(2 + h) - f(2) = 3h + 2h^2\mathclose{\text{,}} and

f′(2)=lim⁡h→03h+2h2h=lim⁡h→0 (3+2h)=3.\begin{aligned} f'(2) &= \lim_{h\to0}\frac{3h + 2h^2}{h} \\ &= \lim_{h\to0}\,(3 + 2h) = 3. \end{aligned}

The tangent line is y=−1+3(x−2),y = -1 + 3(x - 2)\mathclose{\text{,}} that is, y=3x−7.y = 3x - 7\mathclose{\text{.}} Check that it passes through the point: at x=2x = 2 it gives 6−7=−1=f(2).6 - 7 = -1 = f(2)\mathclose{\text{.}}

Try it

Start with f(x)=x2f(x) = x^2 and a=1;a = 1\mathclose{\text{;}} the slider for ∣h∣\abs{h} runs on a logarithmic scale, from 1 down to 0.001. The purple line is the secant through (a,f(a))(a, f(a)) and (a+h,f(a+h));(a + h, f(a + h))\mathclose{\text{;}} the dashed orange line is the tangent at a.a\mathclose{\text{.}} Press Halve h again and again and watch the secant turn towards the tangent, and the difference quotient approach f′(1)=2:f'(1) = 2\mathclose{\text{:}} for x2x^2 the quotient is 2a+h,2a + h\mathclose{\text{,}} so each halving halves the gap. Switch the sign of hh to see secants from the left. Then try x\sqrt x at a=0,a = 0\mathclose{\text{,}} where the quotients grow without bound, and type |x| with a=0,a = 0\mathclose{\text{,}} where the two sides disagree (Section 4.4). For sin⁡x\sin x and exe^x the widget takes f′(a)f'(a) from formulas that Modules 5 and 6 prove; for a function you type, it estimates f′(a)f'(a) numerically, as Section 4.7 explains.

Example 4.4Two more derivatives from the definition✓ Checked

Find (a) f′(2)f'(2) for f(x)=1x,f(x) = \dfrac1x\mathclose{\text{,}} using the second form, and (b) g′(4)g'(4) for g(x)=x,g(x) = \sqrt x\mathclose{\text{,}} using the first. Give the tangent line in each case.

(a) Combine the fractions over the common denominator 2x:2x\mathclose{\text{:}}

f(x)−f(2)=1x−12=2−x2x=−x−22x.\begin{aligned} f(x) - f(2) &= \frac1x - \frac12 = \frac{2 - x}{2x} \\ &= -\frac{x - 2}{2x}. \end{aligned}

Divide by x−2x - 2 (allowed, since x≠2x \ne 2 inside the limit) and let x→2:x \to 2\mathclose{\text{:}}

f′(2)=lim⁡x→2(−12x)=−14.f'(2) = \lim_{x\to2}\left(-\frac{1}{2x}\right) = -\frac14.

The tangent line is y=12−14(x−2),y = \tfrac12 - \tfrac14(x - 2)\mathclose{\text{,}} that is, y=1−x4.y = 1 - \tfrac{x}{4}\mathclose{\text{.}} The slope is negative because 1/x1/x decreases; Exercise 1.9 found the same number, −1/a2-1/a^2 at a=2,a = 2\mathclose{\text{,}} with the first form.

(b) Putting h=0h = 0 in the quotient 4+h−2h\dfrac{\sqrt{4 + h} - 2}{h} gives 00,\tfrac00\mathclose{\text{,}} and the quotient contains a square root, so rationalise, as in Module 2: multiply the top and the bottom by 4+h+2,\sqrt{4 + h} + 2\mathclose{\text{,}} use (p−q)(p+q)=p2−q2(p - q)(p + q) = p^2 - q^2 on the top, and cancel the factor hh (allowed, since h≠0):h \ne 0\mathclose{\text{):}}

4+h−2h=(4+h)−4h(4+h+2)=14+h+2.\begin{aligned} \frac{\sqrt{4 + h} - 2}{h} &= \frac{(4 + h) - 4}{h\big(\sqrt{4 + h} + 2\big)} \\ &= \frac{1}{\sqrt{4 + h} + 2}. \end{aligned}

The square root is continuous at 4 (Module 3), so 4+h→2\sqrt{4 + h} \to 2 as h→0,h \to 0\mathclose{\text{,}} and g′(4)=12+2=14.g'(4) = \dfrac{1}{2 + 2} = \dfrac14\mathclose{\text{.}} The tangent line is y=2+14(x−4),y = 2 + \tfrac14(x - 4)\mathclose{\text{,}} that is, y=1+x4.y = 1 + \tfrac{x}{4}\mathclose{\text{.}} Near x=4x = 4 it is a good stand-in for the curve: at x=4.1x = 4.1 it gives 2.025, while 4.1≈2.0248.\sqrt{4.1} \approx 2.0248\mathclose{\text{.}} Module 7 turns this observation into a method, linear approximation.

Watch out

Two slips in derivatives from the definition. First, keep writing “lim⁡\lim” until the step where you let h→0:h \to 0\mathclose{\text{:}} 3h+2h2h=3+2h\dfrac{3h + 2h^2}{h} = 3 + 2h is true for h≠0,h \ne 0\mathclose{\text{,}} but it is not 3; its limit is 3. Second, never put h=0h = 0 into the quotient before cancelling: that gives 00,\tfrac00\mathclose{\text{,}} which is not a number. Cancel the factor hh first (allowed, since h≠0),h \ne 0\mathclose{\text{),}} then let h→0.h \to 0\mathclose{\text{.}}

3

The derivative as a function

Module 2 and Example 4.1 found velocities at every time at once: 9.8a9.8a for the ball and 20−4a20 - 4a for the car. Letting the point vary turns the derivative into a new function.

Definition 4.4The derivative function

The derivative of ff is the function f′f' given by

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

at every xx at which ff is differentiable, that is, at which this limit exists in the sense of Definition 4.3. Its domain is the set of those x,x\mathclose{\text{,}} which may be smaller than the domain of f.f\mathclose{\text{.}} Finding f′f' is called differentiating f.f\mathclose{\text{.}}

In the limit, xx stays fixed while hh moves.

Example 4.5The derivatives of x2x^2 and x\sqrt x✓ Checked

Find f′(x)f'(x) for f(x)=x2f(x) = x^2 and g′(x)g'(x) for g(x)=x,g(x) = \sqrt x\mathclose{\text{,}} with their domains.

For f:f\mathclose{\text{:}} (x+h)2−x2=(x2+2xh+h2)−x2=2xh+h2,(x + h)^2 - x^2 = (x^2 + 2xh + h^2) - x^2 = 2xh + h^2\mathclose{\text{,}} so for h≠0h \ne 0 the difference quotient is 2xh+h2h=2x+h,\dfrac{2xh + h^2}{h} = 2x + h\mathclose{\text{,}} and f′(x)=lim⁡h→0(2x+h)=2xf'(x) = \lim_{h\to0}(2x + h) = 2x for every real x.x\mathclose{\text{.}}

For g,g\mathclose{\text{,}} rationalise as in Example 4.4: for x>0x > 0 (and h≠0h \ne 0 small enough that x+h>0),x + h > 0\mathclose{\text{),}}

x+h−xh=(x+h)−xh(x+h+x)=1x+h+x.\begin{aligned} \frac{\sqrt{x + h} - \sqrt x}{h} &= \frac{(x + h) - x}{h\big(\sqrt{x + h} + \sqrt x\big)} \\ &= \frac{1}{\sqrt{x + h} + \sqrt x}. \end{aligned}

As h→0,h \to 0\mathclose{\text{,}} x+h→x,\sqrt{x + h} \to \sqrt x\mathclose{\text{,}} because the square root is continuous, so g′(x)=1x+x=12xg'(x) = \dfrac{1}{\sqrt x + \sqrt x} = \dfrac{1}{2\sqrt x} for x>0.x > 0\mathclose{\text{.}} (At x=4x = 4 this gives 14,\tfrac14\mathclose{\text{,}} as before.) At x=0x = 0 the definition does not apply, because x\sqrt x is not defined to the left of 0; and even from the right, the quotient h−0h=1h\dfrac{\sqrt h - 0}{h} = \dfrac{1}{\sqrt h} grows without bound as h→0+.h \to 0^+\mathclose{\text{.}} So gg has domain [0,∞),[0, \infty)\mathclose{\text{,}} but g′g' has domain (0,∞):(0, \infty)\mathclose{\text{:}} the graph of x\sqrt x starts out vertically at the origin.

Reading the graph of f′f'

The height of the graph of f′f' at xx is the slope of the graph of ff at x.x\mathclose{\text{.}} That one sentence lets you sketch f′f' from a picture of f,f\mathclose{\text{,}} without a formula. Figure 4.2 shows f(x)=x3−3xf(x) = x^3 - 3x with its tangent lines at five points, and below it the graph of its derivative: each dot on the lower graph records the slope of one tangent above it.

Top: f(x) = x^3 - 3x with its tangent lines at x = -2, -1, 0, 1, 2. Bottom: its derivative f'(x) = 3x^2 - 3; the dots are the slopes of those five tangents (9, 0, -3, 0, 9). Where the tangent is horizontal, at x = pm1 (dashed), f' is zero; where f rises, f' is positive; where f falls, f' is negative.
Figure 4.2

Top: f(x)=x3−3xf(x) = x^3 - 3x with its tangent lines at x=−2,−1,0,1,2.x = -2, -1, 0, 1, 2\mathclose{\text{.}} Bottom: its derivative f′(x)=3x2−3;f'(x) = 3x^2 - 3\mathclose{\text{;}} the dots are the slopes of those five tangents (9, 0, −3,-3\mathclose{\text{,}} 0, 9). Where the tangent is horizontal, at x=±1x = \pm1 (dashed), f′f' is zero; where ff rises, f′f' is positive; where ff falls, f′f' is negative.

Example 4.6Reading Figure 4.2✓ Checked

For f(x)=x3−3x,f(x) = x^3 - 3x\mathclose{\text{,}} find f′(x)f'(x) from the definition, and compare it with what the graph of ff shows.

Expand (x+h)3=x3+3x2h+3xh2+h3(x + h)^3 = x^3 + 3x^2h + 3xh^2 + h^3 (multiply x2+2xh+h2x^2 + 2xh + h^2 by x+hx + h and collect terms). Then

f(x+h)−f(x)=[(x+h)3−x3]−3[(x+h)−x]=3x2h+3xh2+h3−3h,\begin{aligned} &f(x + h) - f(x) \\ &\quad= \big[(x + h)^3 - x^3\big] - 3\big[(x + h) - x\big] \\ &\quad= 3x^2h + 3xh^2 + h^3 - 3h, \end{aligned}

so the difference quotient is 3x2+3xh+h2−33x^2 + 3xh + h^2 - 3 for h≠0,h \ne 0\mathclose{\text{,}} and

f′(x)=lim⁡h→0(3x2+3xh+h2−3)=3x2−3.\begin{aligned} f'(x) &= \lim_{h\to0}\big(3x^2 + 3xh + h^2 - 3\big) \\ &= 3x^2 - 3. \end{aligned}

Now compare with the top graph.

  • The graph of ff has horizontal tangents at its peak (−1,2)(-1, 2) and at its trough (1,−2).(1, -2)\mathclose{\text{.}} Indeed f′(x)=3x2−3=0f'(x) = 3x^2 - 3 = 0 exactly when x=±1.x = \pm1\mathclose{\text{.}}
  • To the left of −1-1 and to the right of 1 the graph rises, and there f′(x)=3(x2−1)>0,f'(x) = 3(x^2 - 1) > 0\mathclose{\text{,}} since x2>1.x^2 > 1\mathclose{\text{.}}
  • Between −1-1 and 1 the graph falls, and f′(x)<0.f'(x) < 0\mathclose{\text{.}} It falls most steeply at x=0,x = 0\mathclose{\text{,}} where f′(0)=−3f'(0) = -3 is the smallest value of f′.f'\mathclose{\text{.}}
  • A half turn about the origin leaves the graph of ff unchanged (ff is odd), so it takes the point at xx to the point at −x,-x\mathclose{\text{,}} and the tangent there to the tangent at −x.-x\mathclose{\text{.}} A half turn turns every line into a parallel one, so these two tangents have the same slope: f′f' is even, f′(−x)=f′(x).f'(-x) = f'(x)\mathclose{\text{.}}

The same reading works for any graph.

Key idea

To sketch f′f' from the graph of f:f\mathclose{\text{:}}

  • where the tangent to ff is horizontal (at peaks, troughs and some flat points), f′=0:f' = 0\mathclose{\text{:}} the graph of f′f' meets the xx-axis;
  • where ff rises, f′≥0;f' \ge 0\mathclose{\text{;}} where ff falls, f′≤0;f' \le 0\mathclose{\text{;}}
  • the steeper ff is, the further the graph of f′f' is from the xx-axis;
  • where ff has a corner, a cusp, a vertical tangent or a jump, f′f' is not defined (Section 4.4).

The second rule follows from the definition. Call ff increasing on an interval if x1<x2x_1 < x_2 in the interval always gives f(x1)<f(x2)f(x_1) < f(x_2) (strictly increasing, in Exercise 1.10), and decreasing if it always gives f(x1)>f(x2).f(x_1) > f(x_2)\mathclose{\text{.}} Suppose that ff is increasing on an open interval that contains x,x\mathclose{\text{,}} and differentiable at x.x\mathclose{\text{.}} For every h≠0h \ne 0 small enough that x+hx + h lies in the interval, the numerator f(x+h)−f(x)f(x + h) - f(x) has the same sign as h,h\mathclose{\text{,}} so every such difference quotient is positive. Their limit f′(x)f'(x) cannot be negative: if it were, the quotients for small hh would lie within ∣f′(x)∣/2\abs{f'(x)}/2 of it and so would be negative too. The limit can be 0, though: x3x^3 is increasing, yet its tangent at 0 is horizontal. In the same way, f′(x)≤0f'(x) \le 0 where ff is decreasing. A partial converse is true as well: if f′>0f' > 0 at every point of an interval, then ff is increasing there. Its proof needs the Mean Value Theorem of Module 8.

Watch out

The graph of f′f' records slopes, not heights. At the peak of f,f\mathclose{\text{,}} where ff is largest nearby, f′f' is 0; and f′f' can be large where ff is negative, as at x=−2x = -2 in Figure 4.2, where f(−2)=−2f(-2) = -2 but f′(−2)=9.f'(-2) = 9\mathclose{\text{.}} Before you sketch f′,f'\mathclose{\text{,}} ask at each point “how steep, and uphill or downhill?”, not “how high?”.

Notation

Several notations for the derivative are in use, and you will meet them all. If y=f(x),y = f(x)\mathclose{\text{,}} each of these means the derivative:

f′(x)=y′=dydx=dfdx=ddxf(x)=Df(x).f'(x) = y' = \frac{dy}{dx} = \frac{df}{dx} = \frac{d}{dx}f(x) = Df(x).
Notation Read aloud
f′(x),f'(x)\mathclose{\text{,}} y′y' “f prime of x”, “y prime”
dydx\dfrac{dy}{dx} “dee y by dee x”
ddxf(x)\dfrac{d}{dx}f(x) “dee by dee x of f of x”
Df(x)Df(x) “D f of x”

The prime is compact and names the function. Leibniz’s notation dydx\dfrac{dy}{dx} shows which variable changes, and recalls the quotient ΔyΔx\dfrac{\Delta y}{\Delta x} whose limit it is. The symbol ddx\dfrac{d}{dx} is an instruction, “differentiate with respect to xx”: ddx(x2)=2x.\dfrac{d}{dx}\big(x^2\big) = 2x\mathclose{\text{.}} For the value at a point, write f′(a)f'(a) or, in Leibniz’s notation,

dydx∣x=a,\left.\frac{dy}{dx}\right|_{x=a},

read “dee y by dee x at x equals a”. For the falling ball, dsdt=9.8t\dfrac{ds}{dt} = 9.8t and dsdt∣t=2=19.6\left.\dfrac{ds}{dt}\right|_{t=2} = 19.6 m/s. Physicists also write a dot for a derivative with respect to time: s˙,\dot s\mathclose{\text{,}} read “s dot”, means dsdt.\dfrac{ds}{dt}\mathclose{\text{.}}

The units come with the notation: dsdt\dfrac{ds}{dt} is measured in units of ss per unit of t,t\mathclose{\text{,}} here metres per second; the derivative of a temperature in degrees Celsius with respect to a time in minutes is in degrees Celsius per minute.

Watch out

dydx\dfrac{dy}{dx} is not a fraction: dydy and dxdx are not numbers here, and you cannot cancel the dds to get yx.\dfrac{y}{x}\mathclose{\text{.}} (For y=x2,y = x^2\mathclose{\text{,}} dydx=2x,\dfrac{dy}{dx} = 2x\mathclose{\text{,}} while yx=x.)\dfrac{y}{x} = x\mathclose{\text{.)}} Module 5 shows that it often behaves like a fraction all the same, which is much of its appeal. A second slip: f′(3)f'(3) means “differentiate, then put x=3x = 3”. Putting x=3x = 3 first gives a number, and a number does not change, so its derivative is 0: for f(x)=x2,f(x) = x^2\mathclose{\text{,}} f′(3)=2⋅3=6,f'(3) = 2 \cdot 3 = 6\mathclose{\text{,}} but ddx(f(3))=ddx(9)=0.\dfrac{d}{dx}\big(f(3)\big) = \dfrac{d}{dx}(9) = 0\mathclose{\text{.}}

4

Differentiability

Where derivatives fail

Not every function has a derivative at every point. Figure 4.3 shows four functions that have none at 0. In it, x1/3=x3x^{1/3} = \sqrt[3]{x} is the real cube root, defined for every xx (Section 1.4), x2/3=(x3)2,x^{2/3} = \big(\sqrt[3]{x}\big)^2\mathclose{\text{,}} and HH is the unit step function, H(x)=0H(x) = 0 for x<0x < 0 and H(x)=1H(x) = 1 for x≥0.x \ge 0\mathclose{\text{.}}

Four ways to have no derivative at 0. A corner: abs{x} has slope -1 on the left and 1 on the right (the dashed lines). A cusp: the slopes of x^{2/3} fall towards -infty on the left and rise towards infty on the right. A vertical tangent: the graph of x^{1/3} becomes vertical at 0 (dashed). A jump: the step function H is not continuous at 0.
Figure 4.3

Four ways to have no derivative at 0. A corner: ∣x∣\abs{x} has slope −1-1 on the left and 1 on the right (the dashed lines). A cusp: the slopes of x2/3x^{2/3} fall towards −∞-\infty on the left and rise towards ∞\infty on the right. A vertical tangent: the graph of x1/3x^{1/3} becomes vertical at 0 (dashed). A jump: the step function HH is not continuous at 0.

Since f(0)=0f(0) = 0 for the first three functions, their difference quotients at 0 are f(h)/h:f(h)/h\mathclose{\text{:}}

hh∣h∣h\dfrac{\abs{h}}{h}h2/3h\dfrac{h^{2/3}}{h}h1/3h\dfrac{h^{1/3}}{h}
0.10.1112.1542.1544.6424.642
0.0010.001111010100100
0.000010.000011146.4246.4221542154
−0.00001-0.00001−1-1−46.42-46.4221542154
−0.001-0.001−1-1−10-10100100
−0.1-0.1−1-1−2.154-2.1544.6424.642

The three columns behave in three different ways. For ∣x∣\abs{x} the quotients are 1 on one side of 0 and −1-1 on the other; for x2/3x^{2/3} they grow without bound, positive on the right and negative on the left; for x1/3x^{1/3} they grow without bound on both sides. Limits from one side, as in Module 2, describe each column precisely.

Definition 4.5One-sided derivatives

The right-hand derivative and the left-hand derivative of ff at aa are

f+′(a)=lim⁡h→0+f(a+h)−f(a)h,f−′(a)=lim⁡h→0−f(a+h)−f(a)h,\begin{aligned} f'_+(a) &= \lim_{h\to0^+}\frac{f(a + h) - f(a)}{h}, \\ f'_-(a) &= \lim_{h\to0^-}\frac{f(a + h) - f(a)}{h}, \end{aligned}

when these limits exist (as real numbers). A function is differentiable on an open interval if it is differentiable at every point of the interval, and differentiable on a closed interval [a,b][a, b] if it is differentiable on (a,b)(a, b) and has a right-hand derivative at aa and a left-hand derivative at b.b\mathclose{\text{.}}

The symbols read “f prime plus of a” and “f prime minus of a”. A two-sided limit exists exactly when both one-sided limits exist and are equal (Module 2), so f′(a)f'(a) exists exactly when both one-sided derivatives exist and are equal, and then all three are the same number. Now the four functions, one by one.

  • Corner. For ∣x∣,\abs{x}\mathclose{\text{,}} the quotient ∣h∣/h\abs{h}/h is 1 for every h>0h > 0 and −1-1 for every h<0.h < 0\mathclose{\text{.}} So f+′(0)=1f'_+(0) = 1 and f−′(0)=−1:f'_-(0) = -1\mathclose{\text{:}} both one-sided derivatives exist, but they differ, so f′(0)f'(0) does not. The graph has a corner, and no single line is its tangent there. Away from 0, ∣x∣\abs{x} is differentiable, with derivative −1-1 for x<0x < 0 and 1 for x>0.x > 0\mathclose{\text{.}} The ReLU of neural networks, max⁡(0,x)\max(0, x) (Section 1.3), has a corner at 0 too, with one-sided derivatives 0 and 1; software that trains networks must still return some number there, and usually returns 0.
  • Cusp. For x2/3,x^{2/3}\mathclose{\text{,}} the quotient is h2/3/h=h−1/3=1/h3,h^{2/3}/h = h^{-1/3} = 1/\sqrt[3]{h}\mathclose{\text{,}} which tends to ∞\infty as h→0+h \to 0^+ and to −∞-\infty as h→0−.h \to 0^-\mathclose{\text{.}} The graph plunges into the origin and leaves it as steeply: a cusp, where the quotients of a continuous function tend to ∞\infty on one side and −∞-\infty on the other.
  • Vertical tangent. For x1/3,x^{1/3}\mathclose{\text{,}} the quotient is h−2/3=1/(h3)2,h^{-2/3} = 1\big/\big(\sqrt[3]{h}\big)^2\mathclose{\text{,}} which tends to ∞\infty from both sides, so the secants become vertical. When ff is continuous at aa and its difference quotients tend to ∞\infty from both sides (or to −∞-\infty from both sides), the graph has a vertical tangent at a,a\mathclose{\text{,}} the line x=a.x = a\mathclose{\text{.}} Zoomed in, the graph looks like this line, as in Figure 4.1; but a vertical line has no slope, so f′(0)f'(0) still does not exist.
  • Jump. For H,H\mathclose{\text{,}} with H(0)=1,H(0) = 1\mathclose{\text{,}} the quotient for h<0h < 0 is H(h)−H(0)h=0−1h=−1h,\dfrac{H(h) - H(0)}{h} = \dfrac{0 - 1}{h} = -\dfrac1h\mathclose{\text{,}} which grows without bound as h→0−.h \to 0^-\mathclose{\text{.}} The next theorem shows that this is no accident: a function is never differentiable where it is not continuous.

Differentiable functions are continuous

Theorem 4.1Differentiable implies continuous

If ff is differentiable at a,a\mathclose{\text{,}} then ff is continuous at a.a\mathclose{\text{.}}

Proof

For x≠ax \ne a in the open interval where ff is defined, write

f(x)−f(a)=f(x)−f(a)x−a⋅(x−a).f(x) - f(a) = \frac{f(x) - f(a)}{x - a}\cdot(x - a).

As x→a,x \to a\mathclose{\text{,}} the first factor tends to f′(a),f'(a)\mathclose{\text{,}} by the second form of the definition, and the second factor tends to 0. By the product law for limits (Module 2),

lim⁡x→a(f(x)−f(a))=f′(a)⋅0=0,\lim_{x\to a}\big(f(x) - f(a)\big) = f'(a)\cdot0 = 0,

so lim⁡x→af(x)=f(a),\lim_{x\to a}f(x) = f(a)\mathclose{\text{,}} which is what continuity at aa means (Module 3).

∎

The hypothesis does its work in the product law, which needs both factors to have limits that are numbers. For x1/3x^{1/3} at 0 the first factor tends to ∞,\infty\mathclose{\text{,}} and the argument proves nothing (though x1/3x^{1/3} happens to be continuous).

Two consequences are worth separating.

  • The contrapositive: if ff is not continuous at a,a\mathclose{\text{,}} it is not differentiable at a.a\mathclose{\text{.}} That settles the jump of Figure 4.3, and every other discontinuity, at once.
  • The converse is false: a continuous function need not be differentiable. ∣x∣,\abs{x}\mathclose{\text{,}} x2/3x^{2/3} and x1/3x^{1/3} are all continuous at 0 (Module 3), and none of them is differentiable there.

So differentiability is the stronger property. Continuity at aa says that the graph does not break there; differentiability says, in addition, that zoomed in at a,a\mathclose{\text{,}} the graph looks like a line that is not vertical. That rules out corners, cusps and vertical tangents, and other failures too: in Exercise 4.11 a continuous function wiggles so fast near 0 that its secants from the origin never settle on any slope, from either side.

Watch out

“ff is continuous at a,a\mathclose{\text{,}} so f′(a)f'(a) exists” is the converse of Theorem 4.1, and it is false: ∣x∣\abs{x} at 0 is the standard counterexample. A related slip is to trust a formula for f′f' at a point where it was not derived. The derivative of ∣x∣\abs{x} is −1-1 for x<0x < 0 and 1 for x>0,x > 0\mathclose{\text{,}} and some programs write it as sign⁡(x);\operatorname{sign}(x)\mathclose{\text{;}} but sign⁡(0)=0\operatorname{sign}(0) = 0 does not make ∣x∣\abs{x} differentiable at 0. Section 4.5 shows SymPy doing exactly this.

Going deeperDifferentiable means locally linear

Figure 4.1 suggests what differentiability means geometrically: near a,a\mathclose{\text{,}} the graph is almost a straight line. Here is the precise statement. For a slope m,m\mathclose{\text{,}} let

E(h)=f(a+h)−(f(a)+mh)E(h) = f(a + h) - \big(f(a) + mh\big)

be the error of the line y=f(a)+m(x−a)y = f(a) + m(x - a) at x=a+h.x = a + h\mathclose{\text{.}} Then ff is differentiable at aa with f′(a)=mf'(a) = m exactly when E(h)/h→0E(h)/h \to 0 as h→0.h \to 0\mathclose{\text{.}} Indeed,

E(h)h=f(a+h)−f(a)h−m,\frac{E(h)}{h} = \frac{f(a + h) - f(a)}{h} - m,

which tends to 0 exactly when the difference quotient tends to m.m\mathclose{\text{.}} For any other slope m1≠f′(a),m_1 \ne f'(a)\mathclose{\text{,}} the same computation gives E1(h)/h→f′(a)−m1≠0.E_1(h)/h \to f'(a) - m_1 \ne 0\mathclose{\text{.}} So the tangent is the one line through (a,f(a))(a, f(a)) whose error shrinks faster than hh itself. For f(x)=x3f(x) = x^3 at a=1,a = 1\mathclose{\text{,}} E(h)=(1+h)3−(1+3h)=3h2+h3,E(h) = (1 + h)^3 - (1 + 3h) = 3h^2 + h^3\mathclose{\text{,}} so E(h)/h=3h+h2→0;E(h)/h = 3h + h^2 \to 0\mathclose{\text{;}} in the last window of Figure 4.1, where ∣h∣≤0.015,\abs{h} \le 0.015\mathclose{\text{,}} the error is at most about 0.0007, too small to see. Module 7 builds linear approximation on this, and Module 17 uses the same idea to define differentiability for functions of several variables.

5

The first rules

Computing every derivative from the definition is slow. A handful of rules, each proved once from the definition, makes it quick: with the rules of this section you can differentiate any polynomial in a line. Module 5 adds the product, quotient and chain rules.

Constants and powers

Look at what the definition has given so far: (x2)′=2x\big(x^2\big)' = 2x (Example 4.5), and the computation in Example 4.6 shows (x3)′=3x2.\big(x^3\big)' = 3x^2\mathclose{\text{.}} The pattern: the exponent comes down in front, and the new exponent is one less. Example 4.6 also shows why. In

(x+h)3=x3+3x2h+3xh2+h3,(x + h)^3 = x^3 + 3x^2h + 3xh^2 + h^3,

the x3x^3 cancels against −x3,-x^3\mathclose{\text{,}} the term 3x2h3x^2h becomes 3x23x^2 after dividing by h,h\mathclose{\text{,}} and every other term contains h2,h^2\mathclose{\text{,}} so it still contains hh after dividing and vanishes in the limit. That is the idea of the proof, and the proof below carries it out for every power.

Theorem 4.2Constant rule and power rule

For every constant c,c\mathclose{\text{,}} every positive integer nn and every real x,x\mathclose{\text{,}}

ddx(c)=0andddx(xn)=nxn−1.\frac{d}{dx}(c) = 0 \quad\text{and}\quad \frac{d}{dx}\big(x^n\big) = nx^{n-1}.

In particular, ddx(x)=1\dfrac{d}{dx}(x) = 1 (here x0x^0 means 1, also at x=0).x = 0\mathclose{\text{).}}

Proof

Constant. If f(x)=c,f(x) = c\mathclose{\text{,}} every difference quotient is c−ch=0,\dfrac{c - c}{h} = 0\mathclose{\text{,}} so the limit is 0.

Power. Write (x+h)n=(x+h)(x+h)⋯(x+h),(x + h)^n = (x + h)(x + h)\cdots(x + h)\mathclose{\text{,}} with nn factors. Multiplying out, each term of the result picks either xx or hh from each of the nn factors and multiplies the picks together. Picking xx every time gives xn.x^n\mathclose{\text{.}} Picking hh from exactly one factor and xx from the other n−1n - 1 gives xn−1h,x^{n-1}h\mathclose{\text{,}} and there are nn ways to choose that one factor, so these terms add up to nxn−1h.nx^{n-1}h\mathclose{\text{.}} Every other term picks hh at least twice, so it contains h2.h^2\mathclose{\text{.}} Collecting those terms,

(x+h)n=xn+nxn−1h+h2 Q(x,h),(x + h)^n = x^n + nx^{n-1}h + h^2\,Q(x, h),

where Q(x,h)Q(x, h) is a polynomial in xx and hh (and Q=0Q = 0 when n=1).n = 1\mathclose{\text{).}} So, for h≠0,h \ne 0\mathclose{\text{,}}

(x+h)n−xnh=nxn−1+h Q(x,h).\frac{(x + h)^n - x^n}{h} = nx^{n-1} + h\,Q(x, h).

As h→0,h \to 0\mathclose{\text{,}} Q(x,h)Q(x, h) tends to Q(x,0)Q(x, 0) by direct substitution, since it is a polynomial in h;h\mathclose{\text{;}} so h Q(x,h)h\,Q(x, h) tends to 0⋅Q(x,0)=0,0 \cdot Q(x, 0) = 0\mathclose{\text{,}} and the quotient tends to nxn−1.nx^{n-1}\mathclose{\text{.}}

∎
Good to know

Counting the picks exactly gives the binomial theorem. The term xn−khkx^{n-k}h^k appears once for each way of choosing which kk of the nn factors supply an h,h\mathclose{\text{,}} so

(x+h)n=xn+(n1)xn−1h+(n2)xn−2h2+⋯+hn,\begin{aligned} (x + h)^n &= x^n + \binom{n}{1}x^{n-1}h \\ &\quad + \binom{n}{2}x^{n-2}h^2 + \dots + h^n, \end{aligned}

where (nk)=n!k! (n−k)!,\binom{n}{k} = \dfrac{n!}{k!\,(n - k)!}\mathclose{\text{,}} read “n choose k”, counts the ways to choose kk things from nn (with 0!=1).0! = 1\mathclose{\text{).}} For example, (x+h)4=x4+4x3h+6x2h2+4xh3+h4.(x + h)^4 = x^4 + 4x^3h + 6x^2h^2 + 4xh^3 + h^4\mathclose{\text{.}} The proof above needs only the first two terms.

The formula nxn−1nx^{n-1} seems to work beyond positive integers too: Example 4.4 found the slope of 1/x=x−11/x = x^{-1} at 2 to be −14=(−1)⋅2−2,-\tfrac14 = (-1) \cdot 2^{-2}\mathclose{\text{,}} and Example 4.5 found (x)′=12x=12x−1/2.\big(\sqrt x\big)' = \dfrac{1}{2\sqrt x} = \tfrac12 x^{-1/2}\mathclose{\text{.}} Theorem 4.2 does not prove these, since its proof multiplies out nn factors. Module 5 proves the rule for negative integers and Module 6 for every real exponent.

Constant multiples and sums

Theorem 4.3Constant multiple rule and sum rule

Let ff and gg be differentiable at x,x\mathclose{\text{,}} and let cc be a constant. Then cf,cf\mathclose{\text{,}} f+gf + g and f−gf - g are differentiable at x,x\mathclose{\text{,}} and

(cf)′(x)=c f′(x),(f+g)′(x)=f′(x)+g′(x),(f−g)′(x)=f′(x)−g′(x).\begin{aligned} (cf)'(x) &= c\,f'(x), \\ (f + g)'(x) &= f'(x) + g'(x), \\ (f - g)'(x) &= f'(x) - g'(x). \end{aligned}
Proof

Each rule follows from a limit law of Module 2. For the constant multiple, the difference quotient of cfcf is

cf(x+h)−cf(x)h=c⋅f(x+h)−f(x)h,\begin{aligned} &\frac{cf(x + h) - cf(x)}{h} \\ &\quad= c\cdot\frac{f(x + h) - f(x)}{h}, \end{aligned}

which tends to c f′(x)c\,f'(x) by the constant multiple law. For the sum, write Δf=f(x+h)−f(x)\Delta f = f(x + h) - f(x) and Δg=g(x+h)−g(x)\Delta g = g(x + h) - g(x) for the changes in ff and g,g\mathclose{\text{,}} with the Δ\Delta of Module 1. Regrouping the terms, the change in f+gf + g is

(f(x+h)+g(x+h))−(f(x)+g(x))=Δf+Δg,\begin{aligned} &\big(f(x + h) + g(x + h)\big) \\ &\quad - \big(f(x) + g(x)\big) = \Delta f + \Delta g, \end{aligned}

so the difference quotient of f+gf + g is Δf+Δgh=Δfh+Δgh,\dfrac{\Delta f + \Delta g}{h} = \dfrac{\Delta f}{h} + \dfrac{\Delta g}{h}\mathclose{\text{,}} which tends to f′(x)+g′(x)f'(x) + g'(x) by the sum law. Finally, f−g=f+(−1)g,f - g = f + (-1)g\mathclose{\text{,}} so the first two rules give (f−g)′=f′+(−1)g′=f′−g′.(f - g)' = f' + (-1)g' = f' - g'\mathclose{\text{.}}

∎

The hypotheses matter: the formulas need f′(x)f'(x) and g′(x)g'(x) to exist. Without them the sum may or may not be differentiable: ∣x∣\abs{x} and −∣x∣-\abs{x} have no derivative at 0, but their sum, the constant 0, has one.

Applied repeatedly, the sum rule covers sums of any number of terms. In words: the derivative of a sum is the sum of the derivatives, and constant factors come along unchanged. Together, these two rules say that differentiation is linear.

Polynomials

With these rules, a polynomial is differentiated term by term:

ddx(anxn+⋯+a1x+a0)=nanxn−1+⋯+2a2x+a1.\begin{aligned} &\frac{d}{dx}\big(a_nx^n + \dots + a_1x + a_0\big) \\ &\quad= na_nx^{n-1} + \dots + 2a_2x + a_1. \end{aligned}

The constant term a0a_0 disappears. So every polynomial is differentiable on R,\R\mathclose{\text{,}} and its derivative is again a polynomial, of degree one less when the degree is at least 1.

Example 4.7Differentiating a polynomial✓ Checked

Differentiate p(x)=4x5−3x3+7x−2,p(x) = 4x^5 - 3x^3 + 7x - 2\mathclose{\text{,}} and find the tangent line at x=1.x = 1\mathclose{\text{.}}

By the sum and difference rules, differentiate term by term; by the constant multiple rule, keep each coefficient; then use the power rule and the constant rule:

p′(x)=4ddx(x5)−3ddx(x3)+7ddx(x)−ddx(2)=4⋅5x4−3⋅3x2+7⋅1−0=20x4−9x2+7.\begin{aligned} p'(x) &= 4\frac{d}{dx}\big(x^5\big) - 3\frac{d}{dx}\big(x^3\big) \\ &\quad + 7\frac{d}{dx}(x) - \frac{d}{dx}(2) \\ &= 4 \cdot 5x^4 - 3 \cdot 3x^2 + 7 \cdot 1 - 0 \\ &= 20x^4 - 9x^2 + 7. \end{aligned}

At x=1:x = 1\mathclose{\text{:}} p(1)=4−3+7−2=6p(1) = 4 - 3 + 7 - 2 = 6 and p′(1)=20−9+7=18,p'(1) = 20 - 9 + 7 = 18\mathclose{\text{,}} so the tangent line is y=6+18(x−1),y = 6 + 18(x - 1)\mathclose{\text{,}} that is, y=18x−12.y = 18x - 12\mathclose{\text{.}}

Example 4.8Horizontal tangents✓ Checked

Find the points where the graph of g(x)=2x3−9x2+12x−4g(x) = 2x^3 - 9x^2 + 12x - 4 has a horizontal tangent.

A horizontal tangent has slope 0, so solve g′(x)=0.g'(x) = 0\mathclose{\text{.}} By the rules,

g′(x)=6x2−18x+12=6(x2−3x+2)=6(x−1)(x−2),\begin{aligned} g'(x) &= 6x^2 - 18x + 12 \\ &= 6(x^2 - 3x + 2) \\ &= 6(x - 1)(x - 2), \end{aligned}

which is 0 exactly at x=1x = 1 and x=2.x = 2\mathclose{\text{.}} Since g(1)=2−9+12−4=1g(1) = 2 - 9 + 12 - 4 = 1 and g(2)=16−36+24−4=0,g(2) = 16 - 36 + 24 - 4 = 0\mathclose{\text{,}} the graph has horizontal tangents at (1,1)(1, 1) and (2,0).(2, 0)\mathclose{\text{.}} For 1<x<21 < x < 2 one factor is positive and the other negative, so g′(x)<0:g'(x) < 0\mathclose{\text{:}} the tangents there slope downwards. Module 8 proves that the graph then falls from (1,1)(1, 1) to (2,0),(2, 0)\mathclose{\text{,}} and turns this reasoning into a method for finding maxima and minima.

Watch out

Three tempting rules that are false. (1) The derivative of a product is not the product of the derivatives: (x⋅x)′=(x2)′=2x,\big(x \cdot x\big)' = \big(x^2\big)' = 2x\mathclose{\text{,}} not 1⋅1=1.1 \cdot 1 = 1\mathclose{\text{.}} Until Module 5’s product rule, multiply out first: ddx((x2+1)(x−3))=ddx(x3−3x2+x−3)=3x2−6x+1.\dfrac{d}{dx}\big((x^2 + 1)(x - 3)\big) = \dfrac{d}{dx}\big(x^3 - 3x^2 + x - 3\big) = 3x^2 - 6x + 1\mathclose{\text{.}} (2) A constant has derivative 0, however it looks: ddx(π2)=0,\dfrac{d}{dx}\big(\pi^2\big) = 0\mathclose{\text{,}} not 2π,2\pi\mathclose{\text{,}} because π2\pi^2 is a number. (3) The power rule needs the variable in the base and a constant exponent: ddx(2x)\dfrac{d}{dx}\big(2^x\big) is not x⋅2x−1;x \cdot 2^{x-1}\mathclose{\text{;}} Module 6 finds it.

Compute itDerivatives with SymPy

SymPy differentiates with sp.diff, which knows the rules of this section and many more. Here is Example 4.7, the derivative at a point, and the definition itself as a limit:

Python
import sympy as sp
x, h = sp.symbols('x h')
p = 4*x**5 - 3*x**3 + 7*x - 2
dp = sp.diff(p, x)                 # the derivative p'(x)
print(dp)
print(dp.subs(x, 1))               # p'(1): differentiate first, then substitute
q = (p.subs(x, x + h) - p)/h       # the difference quotient
print(sp.limit(q, h, 0, '+-'))     # its two-sided limit: the definition
Output
20*x**4 - 9*x**2 + 7
18
20*x**4 - 9*x**2 + 7

The fourth argument of sp.limit, '+-', asks for the two-sided limit; without it, SymPy approaches from the right only. The answers can look different from yours, because SymPy may factor or reorder:

Python
print(sp.diff(p, x, 2))            # the second derivative (Section 4.6)
print(sp.diff(sp.sqrt(x), x))      # Example 4.5
Output
2*x*(40*x**2 - 9)
1/(2*sqrt(x))

2*x*(40*x**2 - 9) is 80x3−18x80x^3 - 18x in factored form, the derivative of 20x4−9x2+7.20x^4 - 9x^2 + 7\mathclose{\text{.}} Now the warning of Section 4.4. With xx declared real (for complex numbers SymPy’s answer is more complicated):

Python
xr = sp.symbols('x', real=True)
d = sp.diff(sp.Abs(xr), xr)
print(d, d.subs(xr, 0))
print(sp.limit(sp.Abs(h)/h, h, 0, '+'), sp.limit(sp.Abs(h)/h, h, 0, '-'))
Output
sign(x) 0
1 -1

For real x≠0,x \ne 0\mathclose{\text{,}} sign(x) is right: −1-1 for x<0x < 0 and 1 for x>0.x > 0\mathclose{\text{.}} But substituting 0 gives 0, as if ∣x∣\abs{x} had derivative 0 there, while the one-sided limits of its difference quotient, 1 and −1,-1\mathclose{\text{,}} show that it has none; with '+-', SymPy would refuse the two-sided limit, as in Module 2. A formula for f′f' tells you nothing at a point where ff is not differentiable.

6

Higher derivatives and motion

The derivative f′f' is a function, so it can be differentiated in its turn.

Definition 4.6Higher derivatives

The second derivative of ff is the derivative of f′:f'\mathclose{\text{:}} f′′=(f′)′.f'' = (f')'\mathclose{\text{.}} The third derivative is f′′′=(f′′)′,f''' = (f'')'\mathclose{\text{,}} and in general the nnth derivative f(n)f^{(n)} is the derivative of f(n−1),f^{(n-1)}\mathclose{\text{,}} wherever these derivatives exist. In Leibniz’s notation, for y=f(x),y = f(x)\mathclose{\text{,}}

f′′(x)=d2ydx2=ddx(dydx),f(n)(x)=dnydxn.\begin{aligned} f''(x) &= \frac{d^2y}{dx^2} = \frac{d}{dx}\left(\frac{dy}{dx}\right), \\ f^{(n)}(x) &= \frac{d^ny}{dx^n}. \end{aligned}

Read f′′(x)f''(x) as “f double prime of x”, f′′′(x)f'''(x) as “f triple prime of x”, f(4)(x)f^{(4)}(x) as “the fourth derivative of f at x”, and d2ydx2\dfrac{d^2y}{dx^2} as “dee two y by dee x squared”. The 2s count differentiations; they are not powers. The brackets in f(4)f^{(4)} keep it apart from the power f4.f^4\mathclose{\text{.}}

Example 4.9All the derivatives of a polynomial✓ Checked

Find every derivative of p(x)=x4−2x3+5x.p(x) = x^4 - 2x^3 + 5x\mathclose{\text{.}}

Apply the rules again and again:

p′(x)=4x3−6x2+5,p′′(x)=12x2−12x,p′′′(x)=24x−12,p(4)(x)=24,p(5)(x)=0,\begin{aligned} p'(x) &= 4x^3 - 6x^2 + 5, \\ p''(x) &= 12x^2 - 12x, \\ p'''(x) &= 24x - 12, \\ p^{(4)}(x) &= 24, \\ p^{(5)}(x) &= 0, \end{aligned}

and every later derivative is 0 as well. Each differentiation lowers the degree by 1, so a polynomial of degree nn has a constant nnth derivative, and all its derivatives after the nnth are 0. For xnx^n itself the nnth derivative is n(n−1)⋯2⋅1=n!;n(n - 1)\cdots2 \cdot 1 = n!\mathclose{\text{;}} here p(4)=24=4!,p^{(4)} = 24 = 4!\mathclose{\text{,}} because the leading coefficient of pp is 1.

Velocity, acceleration and jerk

For an object moving along a line, with position s(t)s(t) at time t,t\mathclose{\text{,}} the first three derivatives have names:

Quantity Definition Units
velocity v(t)=s′(t)=dsdtv(t) = s'(t) = \dfrac{ds}{dt} m/s
acceleration a(t)=v′(t)=s′′(t)=d2sdt2a(t) = v'(t) = s''(t) = \dfrac{d^2s}{dt^2} m/s²
jerk j(t)=a′(t)=s′′′(t)=d3sdt3j(t) = a'(t) = s'''(t) = \dfrac{d^3s}{dt^3} m/s³

The units are those for ss in metres and tt in seconds. Acceleration is the rate of change of velocity: the falling ball has v(t)=9.8t,v(t) = 9.8t\mathclose{\text{,}} so its acceleration is constant, a(t)=9.8a(t) = 9.8 m/s², the acceleration due to gravity of Module 1. The braking car of Example 4.1 has v(t)=20−4tv(t) = 20 - 4t and a(t)=−4a(t) = -4 m/s²: negative, because its velocity decreases. Jerk, the rate of change of acceleration, is what you feel as a jolt when a lift or a train starts or stops abruptly; lifts, trains and robot arms are designed to keep it small.

The sign of aa tells you which way vv is changing (Module 8 proves that a positive derivative means an increasing function). So an object speeds up when its velocity and acceleration have the same sign, and slows down when their signs differ.

Example 4.10A particle moving back and forth✓ Checked

Suppose a particle moves along a line so that its position after tt seconds is s(t)=t3−6t2+9ts(t) = t^3 - 6t^2 + 9t metres, for t≥0;t \ge 0\mathclose{\text{;}} the motion is made up, with numbers chosen to be easy to work with. Find its velocity, acceleration and jerk. When is it at rest, when does it move backwards, and when does it speed up?

Differentiate three times:

v(t)=3t2−12t+9=3(t−1)(t−3),a(t)=6t−12=6(t−2),j(t)=6.\begin{aligned} v(t) &= 3t^2 - 12t + 9 = 3(t - 1)(t - 3), \\ a(t) &= 6t - 12 = 6(t - 2), \\ j(t) &= 6. \end{aligned}

The particle is at rest when v(t)=0:v(t) = 0\mathclose{\text{:}} at t=1,t = 1\mathclose{\text{,}} at the position s(1)=4s(1) = 4 m, and at t=3,t = 3\mathclose{\text{,}} back at s(3)=0.s(3) = 0\mathclose{\text{.}} For 1<t<31 < t < 3 the factors t−1t - 1 and t−3t - 3 have opposite signs, so v<0v < 0 and the particle moves backwards; before t=1t = 1 and after t=3,t = 3\mathclose{\text{,}} v>0v > 0 and it moves forwards. In the first 4 seconds it goes forwards from 0 to 4 m, back to 0, and forwards again to s(4)=4s(4) = 4 m: it covers 4+4+4=124 + 4 + 4 = 12 m of ground, although it ends only 4 m from where it started.

The acceleration changes sign at t=2.t = 2\mathclose{\text{.}} Comparing the signs of vv and a:a\mathclose{\text{:}}

Interval vv aa The particle
0<t<10 < t < 1 ++ −- moves forwards, slowing down
1<t<21 < t < 2 −- −- moves backwards, speeding up
2<t<32 < t < 3 −- ++ moves backwards, slowing down
t>3t > 3 ++ ++ moves forwards, speeding up

Figure 4.4 shows the three graphs. Where ss has a horizontal tangent, vv is 0; where vv has its horizontal tangent, at its lowest point t=2,t = 2\mathclose{\text{,}} aa is 0: each graph is the slope picture of the one above it.

The particle of Example 4.10. Top: its position s(t), with horizontal tangents at t = 1 and t = 3. Middle: its velocity v(t) = s'(t), zero at t = 1 and t = 3 and negative in between, while the particle moves backwards; its lowest point is at t = 2. Bottom: its acceleration a(t) = v'(t), zero at t = 2.
Figure 4.4

The particle of Example 4.10. Top: its position s(t),s(t)\mathclose{\text{,}} with horizontal tangents at t=1t = 1 and t=3.t = 3\mathclose{\text{.}} Middle: its velocity v(t)=s′(t),v(t) = s'(t)\mathclose{\text{,}} zero at t=1t = 1 and t=3t = 3 and negative in between, while the particle moves backwards; its lowest point is at t=2.t = 2\mathclose{\text{.}} Bottom: its acceleration a(t)=v′(t),a(t) = v'(t)\mathclose{\text{,}} zero at t=2.t = 2\mathclose{\text{.}}

Watch out

Velocity is not speed, and a negative acceleration does not mean slowing down. Between t=1t = 1 and t=2t = 2 in Example 4.10 the acceleration is negative, yet the particle speeds up: it moves backwards faster and faster, its velocity going from 0 to −3-3 m/s and its speed from 0 to 3 m/s. Whether an object speeds up depends on whether vv and aa have the same sign, not on the sign of aa alone.

7

Numerical differentiation

Sometimes there is no formula to differentiate. A function may be known only as a program, such as a simulation that takes minutes to run, or only through measurements, such as the positions of a car recorded once a second. Then the derivative is estimated from a few values of the function, with a difference quotient and a small step h.h\mathclose{\text{.}} Two choices decide how good the estimate is: which quotient, and which h.h\mathclose{\text{.}}

Forward and central differences

Definition 4.7Forward and central differences

For a step h>0,h > 0\mathclose{\text{,}} the forward difference and the central difference of ff at aa are

forward:f(a+h)−f(a)h,central:f(a+h)−f(a−h)2h.\begin{aligned} &\text{forward:} && \frac{f(a + h) - f(a)}{h}, \\ &\text{central:} && \frac{f(a + h) - f(a - h)}{2h}. \end{aligned}

The error of such an estimate is f′(a)f'(a) minus the estimate: the exact value minus the approximation, as for every approximation in this series. It is positive when the estimate is too small, and negative when it is too large.

The forward difference is the difference quotient itself, the slope of the secant from aa to a+h.a + h\mathclose{\text{.}} The central difference is the slope of the secant from a−ha - h to a+h,a + h\mathclose{\text{,}} over an interval centred on a.a\mathclose{\text{.}} It is the average of the forward difference and the backward difference f(a)−f(a−h)h,\dfrac{f(a) - f(a - h)}{h}\mathclose{\text{,}} the quotient with −h-h in place of h.h\mathclose{\text{.}} If ff is differentiable at a,a\mathclose{\text{,}} the forward and backward differences both tend to f′(a)f'(a) as h→0,h \to 0\mathclose{\text{,}} and so does their average.

Example 4.11Two differences for x3x^3✓ Checked

For f(x)=x3,f(x) = x^3\mathclose{\text{,}} find the forward and central differences at a,a\mathclose{\text{,}} and their errors.

By the expansion in Example 4.6, (a+h)3=a3+3a2h+3ah2+h3,(a + h)^3 = a^3 + 3a^2h + 3ah^2 + h^3\mathclose{\text{,}} and with −h-h in place of h,h\mathclose{\text{,}} (a−h)3=a3−3a2h+3ah2−h3.(a - h)^3 = a^3 - 3a^2h + 3ah^2 - h^3\mathclose{\text{.}} So

(a+h)3−a3h=3a2+3ah+h2,(a+h)3−(a−h)32h=6a2h+2h32h=3a2+h2.\begin{aligned} \frac{(a + h)^3 - a^3}{h} &= 3a^2 + 3ah + h^2, \\ \frac{(a + h)^3 - (a - h)^3}{2h} &= \frac{6a^2h + 2h^3}{2h} \\ &= 3a^2 + h^2. \end{aligned}

Since f′(a)=3a2,f'(a) = 3a^2\mathclose{\text{,}} the errors, f′(a)f'(a) minus the estimates, are exactly −3ah−h2-3ah - h^2 and −h2.-h^2\mathclose{\text{.}} At a=1a = 1 with h=0.1h = 0.1 the forward difference is 3.31 and the central difference 3.01; with h=0.01h = 0.01 they are 3.0301 and 3.0001. Both estimates are too large, so both errors are negative. A tenfold cut in hh cuts the forward error about tenfold in size, but the central error a hundredfold. In the central difference the terms 3ah2,3ah^2\mathclose{\text{,}} which do not change sign with h,h\mathclose{\text{,}} cancel: that is why centring pays.

The pattern holds for other functions. The table shows the two errors for x\sqrt x at a=4,a = 4\mathclose{\text{,}} where f′(4)=14f'(4) = \tfrac14 (Example 4.4), computed exactly:

hh14−forward\tfrac14 - \text{forward}14−central\tfrac14 - \text{central}
0.10.10.0015430.001543−1.954×10−5-1.954\times10^{-5}
0.010.010.00015610.0001561−1.953×10−7-1.953\times10^{-7}
0.0010.0011.562×10−51.562\times10^{-5}−1.953×10−9-1.953\times10^{-9}
0.00010.00011.562×10−61.562\times10^{-6}−1.953×10−11-1.953\times10^{-11}

Each row divides hh by 10; the forward error shrinks by a factor of 10 and the central error by a factor of 100. The signs differ: the forward difference is too small here and the central difference too large.

Central differences are also the natural choice for data measured at equally spaced times: the estimate at each inner time uses the measurements on both sides of it. And Exercise 1.11 showed that for a quadratic, such as the falling ball’s s(t)=4.9t2,s(t) = 4.9t^2\mathclose{\text{,}} the central difference is exact for every h.h\mathclose{\text{.}}

Compute itVelocities from measurements

Suppose you know the falling ball only from its table of positions in Section 1.3, measured every 0.5 s. Central differences estimate the velocity at the inner times; NumPy’s np.gradient computes the same central differences, with a forward and a backward difference at the two ends:

Python
import numpy as np
dt = 0.5
t = np.arange(0, 3.01, dt)            # 0, 0.5, 1, ..., 3
s = 4.9*t**2                          # the measured positions
central = (s[2:] - s[:-2])/(2*dt)     # at t = 0.5, 1, ..., 2.5
print(central)
print(np.gradient(s, dt))
print(9.8*t)                          # the exact velocities
Output
[ 4.9  9.8 14.7 19.6 24.5]
[ 2.45  4.9   9.8  14.7  19.6  24.5  26.95]
[ 0.   4.9  9.8 14.7 19.6 24.5 29.4]

s[2:] - s[:-2] subtracts from each position the one two places before it. The central differences are exact, as Exercise 1.11 predicts. The estimates at the ends, 2.45 and 26.95 m/s, are each off by 2.45 m/s. They are the one-sided quotients 9.8a+4.9h9.8a + 4.9h of Module 2, with h=0.5h = 0.5 at t=0t = 0 and h=−0.5h = -0.5 at t=3,t = 3\mathclose{\text{,}} so their errors are 9.8a−(9.8a+4.9h)=−4.9h:9.8a - (9.8a + 4.9h) = -4.9h\mathclose{\text{:}} −2.45-2.45 m/s at the start and 2.452.45 m/s at the end.

Truncation error

The error that remains when the arithmetic is exact comes from stopping at a step h>0h > 0 instead of going to the limit; it is called truncation error. To compare such errors, use the big-O notation of Module 3, now as h→0h \to 0 rather than for large inputs.

Definition 4.8Big-O as h→0h \to 0

An error E(h)E(h) is O(hp)O(h^p) as h→0,h \to 0\mathclose{\text{,}} read “big O of h to the p”, if there are constants CC and δ>0\delta > 0 such that ∣E(h)∣≤C∣h∣p\abs{E(h)} \le C\abs{h}^p whenever 0<∣h∣<δ.0 < \abs{h} < \delta\mathclose{\text{.}}

For x3x^3 at a,a\mathclose{\text{,}} the forward error −3ah−h2-3ah - h^2 is O(h),O(h)\mathclose{\text{,}} because for 0<h<1,0 < h < 1\mathclose{\text{,}} h2≤hh^2 \le h and so ∣3ah+h2∣≤(3∣a∣+1)h;\abs{3ah + h^2} \le (3\abs{a} + 1)h\mathclose{\text{;}} the central error −h2-h^2 is O(h2).O(h^2)\mathclose{\text{.}} A method whose error is O(hp),O(h^p)\mathclose{\text{,}} and no better, has order p:p\mathclose{\text{:}} the forward difference has order 1 and the central difference order 2. Halving hh roughly halves an error of order 1, and quarters an error of order 2.

This is not special to x3:x^3\mathclose{\text{:}}

Key idea

If f′′f'' is continuous near a,a\mathclose{\text{,}} the forward difference has error about −h2f′′(a),-\tfrac{h}{2}f''(a)\mathclose{\text{,}} which is O(h).O(h)\mathclose{\text{.}} If f′′′f''' is continuous near a,a\mathclose{\text{,}} the central difference has error about −h26f′′′(a),-\tfrac{h^2}{6}f'''(a)\mathclose{\text{,}} which is O(h2).O(h^2)\mathclose{\text{.}} Module 15 proves both, with Taylor’s theorem.

For x3,x^3\mathclose{\text{,}} with f′′(a)=6af''(a) = 6a and f′′′(a)=6,f'''(a) = 6\mathclose{\text{,}} these give −3ah-3ah and −h2,-h^2\mathclose{\text{,}} the leading parts of the errors of Example 4.11. For x\sqrt x at 4, where f′′(4)=−132f''(4) = -\tfrac{1}{32} and f′′′(4)=3256f'''(4) = \tfrac{3}{256} (by the power rule for fractional exponents, which Module 6 proves), they predict h64\tfrac{h}{64} and −h2512;-\tfrac{h^2}{512}\mathclose{\text{;}} for h=0.1h = 0.1 that is 0.001560.00156 and −0.0000195,-0.0000195\mathclose{\text{,}} close to the first row of the table above.

Rounding error and the best step

On paper, the smaller h,h\mathclose{\text{,}} the better. On a computer, not so. Floating-point numbers (Section 1.7) carry about 16 significant digits: the gap between 1 and the next larger number that the computer can store is the machine epsilon

ε=2−52≈2.2×10−16,\varepsilon = 2^{-52} \approx 2.2 \times 10^{-16},

and a computed value of ff may be off by a few units of ε∣f(a)∣.\varepsilon\abs{f(a)}\mathclose{\text{.}} In the forward difference, f(a+h)f(a + h) and f(a)f(a) are nearly equal when hh is small, so their difference keeps only the few digits in which they differ: cancellation, as in Module 2. Dividing by the small hh then magnifies what is left of the rounding. The part of the error of the quotient that rounding causes, its rounding error, can have either sign; its size is about ε∣f(a)∣/h,\varepsilon\abs{f(a)}/h\mathclose{\text{,}} and it grows as hh shrinks.

So the total error has two parts, which behave in opposite ways as hh shrinks. For the forward difference of x3x^3 at 1, where ∣f(1)∣=1,\abs{f(1)} = 1\mathclose{\text{,}} its size is at most about

3h+εh.3h + \frac{\varepsilon}{h}.

The first part, the size of the truncation error, is large when hh is large; the second, the size of the rounding error, is large when hh is small.

Compute itRounding error in action

Python’s floats are the 16-digit numbers described above. The machine epsilon, and what it means:

Python
import sys
print(sys.float_info.epsilon)
print(1.0 + 1e-16 == 1.0, 1.0 + 1e-15 == 1.0)
Output
2.220446049250313e-16
True False

Adding 10−1610^{-16} to 1 changes nothing: the result rounds back to 1. Now the errors, exact value minus estimate, of the two differences of f(x)=x3f(x) = x^3 at a=1,a = 1\mathclose{\text{,}} where f′(1)=3,f'(1) = 3\mathclose{\text{,}} as hh shrinks from 10−110^{-1} to 10−15:10^{-15}\mathclose{\text{:}}

Python
def f(x):
    return x**3

a, exact = 1.0, 3.0
print(f"{'h':>7}  {'forward error':>13}  {'central error':>13}")
for k in range(1, 16):
    h = 10.0**(-k)
    forward = (f(a + h) - f(a))/h
    central = (f(a + h) - f(a - h))/(2*h)
    print(f"{h:7.0e}  {exact - forward:13.2e}  {exact - central:13.2e}")
Output
      h  forward error  central error
  1e-01      -3.10e-01      -1.00e-02
  1e-02      -3.01e-02      -1.00e-04
  1e-03      -3.00e-03      -1.00e-06
  1e-04      -3.00e-04      -1.00e-08
  1e-05      -3.00e-05      -9.74e-11
  1e-06      -3.00e-06       8.03e-11
  1e-07      -3.02e-07      -8.63e-11
  1e-08      -3.97e-09       7.13e-09
  1e-09      -2.48e-07      -8.17e-08
  1e-10      -2.48e-07      -2.48e-07
  1e-11      -2.48e-07      -2.48e-07
  1e-12      -2.67e-04      -1.00e-04
  1e-13       2.40e-03       7.32e-04
  1e-14       2.40e-03       2.40e-03
  1e-15      -3.31e-01      -1.64e-01

Read down the columns. In the first rows both errors are negative, close to the truncation errors −3h−h2-3h - h^2 and −h2-h^2 of Example 4.11: both estimates are too large. The forward error shrinks tenfold per row in size, as 3h3h predicts, down to about −3×10−7-3 \times 10^{-7} at h=10−7.h = 10^{-7}\mathclose{\text{.}} At h=10−8h = 10^{-8} it is smaller still, about −4×10−9,-4 \times 10^{-9}\mathclose{\text{,}} but by luck: there the truncation error, −3h−h2≈−3.0×10−8,-3h - h^2 \approx -3.0 \times 10^{-8}\mathclose{\text{,}} and the rounding error, about +2.6×10−8,+2.6 \times 10^{-8}\mathclose{\text{,}} nearly cancel. For smaller hh the rounding error takes over, and the error grows again, with either sign. The central error shrinks a hundredfold per row in size, to about 10−1010^{-10} at h=10−5,h = 10^{-5}\mathclose{\text{,}} stays near that size down to h=10−7,h = 10^{-7}\mathclose{\text{,}} and then grows too. At h=10−15h = 10^{-15} both estimates are wrong in the first decimal place.

Figure 4.5 plots the sizes of the same errors for 481 values of h.h\mathclose{\text{.}} On logarithmic axes a power ChpCh^p appears as a straight line of slope p,p\mathclose{\text{,}} because log⁡(Chp)=log⁡C+plog⁡h:\log\big(Ch^p\big) = \log C + p\log h\mathclose{\text{:}} the sizes of the truncation errors are lines of slopes 1 and 2, and the size of the rounding error a ragged band along ε/h,\varepsilon/h\mathclose{\text{,}} of slope −1.-1\mathclose{\text{.}}

The sizes of the errors of the forward (blue) and central (orange) differences of f(x) = x^3 at a = 1, computed in floating point for 481 steps h between 10^{-16} and 1, on logarithmic scales. On the right, truncation error dominates, along the dashed lines 3h and h^2. On the left, rounding error dominates, ragged and close to varepsilon/h (dashed grey). The dotted lines mark the best steps, about 10^{-8} for the forward difference and 5 times 10^{-6} for the central one.
Figure 4.5

The sizes of the errors of the forward (blue) and central (orange) differences of f(x)=x3f(x) = x^3 at a=1,a = 1\mathclose{\text{,}} computed in floating point for 481 steps hh between 10−1610^{-16} and 1, on logarithmic scales. On the right, truncation error dominates, along the dashed lines 3h3h and h2.h^2\mathclose{\text{.}} On the left, rounding error dominates, ragged and close to ε/h\varepsilon/h (dashed grey). The dotted lines mark the best steps, about 10−810^{-8} for the forward difference and 5×10−65 \times 10^{-6} for the central one.

The best step is where the two kinds of error balance. For the forward difference, the bound 3h+ε/h3h + \varepsilon/h is smallest when its two parts are equal (Exercise 4.12 proves it), at h=ε/3≈9×10−9,h = \sqrt{\varepsilon/3} \approx 9 \times 10^{-9}\mathclose{\text{,}} where it is about 23ε≈5×10−8.2\sqrt{3\varepsilon} \approx 5 \times 10^{-8}\mathclose{\text{.}} For the central difference, the size of the error is at most about h2+ε/h,h^2 + \varepsilon/h\mathclose{\text{,}} which is smallest at h=(ε/2)1/3≈5×10−6h = (\varepsilon/2)^{1/3} \approx 5 \times 10^{-6} (Module 8 shows how to find such minima), where it is about 7×10−11.7 \times 10^{-11}\mathclose{\text{.}} The table agrees: its smallest errors, in size, lie near those steps. The central ones, about 10−10,10^{-10}\mathclose{\text{,}} match the estimate; the forward one, −4×10−9-4 \times 10^{-9} at h=10−8,h = 10^{-8}\mathclose{\text{,}} beats it, thanks to the lucky cancellation (Exercise 4.12 looks at the rows near the best step).

Key idea

A rule of thumb for 16-digit floating point: a forward difference with h≈10−8h \approx 10^{-8} gets about 8 correct digits, and a central difference with h≈10−5h \approx 10^{-5} about 10. Make hh smaller and the answer gets worse, not better. When aa is far from 1, scale the step with it, as in h=10−5(1+∣a∣):h = 10^{-5}(1 + \abs{a})\mathclose{\text{:}} the widget of Section 4.2 uses that step, with central differences, for the functions you type.

Watch out

“The smaller h,h\mathclose{\text{,}} the more accurate the derivative.” True in exact arithmetic, false in floating point. With h=10−15,h = 10^{-15}\mathclose{\text{,}} the forward difference of x3x^3 at 1 is off by about 0.33, in the first decimal place; with h=10−16,h = 10^{-16}\mathclose{\text{,}} 1+h1 + h rounds to 1 and the forward difference is 0. Choose hh by the rule of thumb, or avoid the problem: SymPy differentiates exactly, and automatic differentiation (Module 5) computes derivatives of programs to full precision, without any h.h\mathclose{\text{.}}

8

What you learned

  • The velocity of a moving object and the slope of the tangent line to a graph are both limits of difference quotients. That limit is the derivative, f′(a)=lim⁡h→0f(a+h)−f(a)h=lim⁡x→af(x)−f(a)x−a.f'(a) = \lim_{h\to0}\dfrac{f(a + h) - f(a)}{h} = \lim_{x\to a}\dfrac{f(x) - f(a)}{x - a}\mathclose{\text{.}}
  • The tangent line at (a,f(a))(a, f(a)) is y=f(a)+f′(a)(x−a);y = f(a) + f'(a)(x - a)\mathclose{\text{;}} zoomed in, the graph of a differentiable function looks like it. A derivative is measured in units of ff per unit of x.x\mathclose{\text{.}}
  • The derivative is a function, f′,f'\mathclose{\text{,}} whose height at xx is the slope of ff at x;x\mathclose{\text{;}} it is written f′(x),f'(x)\mathclose{\text{,}} y′,y'\mathclose{\text{,}} dydx,\dfrac{dy}{dx}\mathclose{\text{,}} ddxf(x)\dfrac{d}{dx}f(x) or Df(x).Df(x)\mathclose{\text{.}}
  • A function has no derivative at a corner, a cusp, a vertical tangent or a discontinuity, nor where its difference quotients keep oscillating. Differentiable implies continuous; continuous does not imply differentiable (∣x∣\abs{x} at 0).
  • ddx(c)=0,\dfrac{d}{dx}(c) = 0\mathclose{\text{,}} ddx(xn)=nxn−1\dfrac{d}{dx}\big(x^n\big) = nx^{n-1} for positive integers n,n\mathclose{\text{,}} (cf)′=cf′(cf)' = cf' and (f±g)′=f′±g′:(f \pm g)' = f' \pm g'\mathclose{\text{:}} polynomials are differentiated term by term.
  • Higher derivatives f′′,f''\mathclose{\text{,}} f′′′,f'''\mathclose{\text{,}} f(n).f^{(n)}\mathclose{\text{.}} For motion along a line, velocity v=s′,v = s'\mathclose{\text{,}} acceleration a=s′′a = s'' and jerk j=s′′′;j = s'''\mathclose{\text{;}} an object speeds up when vv and aa have the same sign.
  • Numerically, the forward difference has truncation error O(h)O(h) and the central difference O(h2).O(h^2)\mathclose{\text{.}} Rounding error grows like ε/h,\varepsilon/h\mathclose{\text{,}} so in floating point the best steps are about 10−810^{-8} and 10−5,10^{-5}\mathclose{\text{,}} and smaller steps make things worse.
  • SymPy: sp.diff(f, x), sp.diff(f, x, 2), and sp.limit(q, h, 0, '+-') for the definition; a formula for f′f' says nothing at a point where ff is not differentiable.
9

Exercises

Exercise 4.1★Derivatives from the definition
  • (a) Use the definition f′(a)=lim⁡h→0f(a+h)−f(a)hf'(a) = \lim_{h\to0}\dfrac{f(a + h) - f(a)}{h} to find f′(−1)f'(-1) for f(x)=3x2−x,f(x) = 3x^2 - x\mathclose{\text{,}} and the tangent line at x=−1.x = -1\mathclose{\text{.}}
  • (b) Use the form lim⁡x→ag(x)−g(a)x−a\lim_{x\to a}\dfrac{g(x) - g(a)}{x - a} to find g′(3)g'(3) for g(x)=x2−4x.g(x) = x^2 - 4x\mathclose{\text{.}}
Show solution✓ Checked

(a) First f(−1)=3+1=4.f(-1) = 3 + 1 = 4\mathclose{\text{.}} Substitute −1+h,-1 + h\mathclose{\text{,}} with brackets, and expand, with (−1+h)2=1−2h+h2:(-1 + h)^2 = 1 - 2h + h^2\mathclose{\text{:}}

f(−1+h)=3(−1+h)2−(−1+h)=3(1−2h+h2)+1−h=3−6h+3h2+1−h=4−7h+3h2.\begin{aligned} &f(-1 + h) \\ &\quad= 3(-1 + h)^2 - (-1 + h) \\ &\quad= 3(1 - 2h + h^2) + 1 - h \\ &\quad= 3 - 6h + 3h^2 + 1 - h \\ &\quad= 4 - 7h + 3h^2. \end{aligned}

Subtract f(−1)=4:f(-1) = 4\mathclose{\text{:}} f(−1+h)−f(−1)=−7h+3h2.f(-1 + h) - f(-1) = -7h + 3h^2\mathclose{\text{.}} So

f′(−1)=lim⁡h→0−7h+3h2h=lim⁡h→0 (−7+3h)=−7.\begin{aligned} f'(-1) &= \lim_{h\to0}\frac{-7h + 3h^2}{h} \\ &= \lim_{h\to0}\,(-7 + 3h) = -7. \end{aligned}

The tangent line is y=4−7(x+1),y = 4 - 7(x + 1)\mathclose{\text{,}} that is, y=−7x−3;y = -7x - 3\mathclose{\text{;}} at x=−1x = -1 it gives 7−3=4.7 - 3 = 4\mathclose{\text{.}} The common slip is to drop the brackets around −1+h-1 + h in the term −x,-x\mathclose{\text{,}} which gives −1+h-1 + h instead of +1−h.+1 - h\mathclose{\text{.}}

(b) g(3)=9−12=−3,g(3) = 9 - 12 = -3\mathclose{\text{,}} so g(x)−g(3)=x2−4x+3=(x−1)(x−3).g(x) - g(3) = x^2 - 4x + 3 = (x - 1)(x - 3)\mathclose{\text{.}} Cancel the factor x−3x - 3 (allowed, since x≠3x \ne 3 inside the limit):

g′(3)=lim⁡x→3(x−1)(x−3)x−3=lim⁡x→3 (x−1)=2.\begin{aligned} g'(3) &= \lim_{x\to3}\frac{(x - 1)(x - 3)}{x - 3} \\ &= \lim_{x\to3}\,(x - 1) = 2. \end{aligned}
Exercise 4.2★Polynomials by the rules

Differentiate:

  • (a) f(x)=7x4−2x3+x−9f(x) = 7x^4 - 2x^3 + x - 9
  • (b) g(x)=(2x−1)(x2+3)g(x) = (2x - 1)(x^2 + 3)
  • (c) k(x)=x3−4xx,k(x) = \dfrac{x^3 - 4x}{x}\mathclose{\text{,}} for x≠0x \ne 0
  • (d) s(t)=πt2+π2s(t) = \pi t^2 + \pi^2
Show solution✓ Checked

(a) Term by term: f′(x)=7⋅4x3−2⋅3x2+1−0=28x3−6x2+1.f'(x) = 7 \cdot 4x^3 - 2 \cdot 3x^2 + 1 - 0 = 28x^3 - 6x^2 + 1\mathclose{\text{.}}

(b) Multiply out first: g(x)=2x3+6x−x2−3=2x3−x2+6x−3,g(x) = 2x^3 + 6x - x^2 - 3 = 2x^3 - x^2 + 6x - 3\mathclose{\text{,}} so g′(x)=6x2−2x+6.g'(x) = 6x^2 - 2x + 6\mathclose{\text{.}} Multiplying the derivatives of the factors, 2⋅2x=4x,2 \cdot 2x = 4x\mathclose{\text{,}} would be wrong: the derivative of a product is not the product of the derivatives (Module 5 gives the right rule).

(c) For x≠0,x \ne 0\mathclose{\text{,}} divide each term by x:x\mathclose{\text{:}} k(x)=x2−4.k(x) = x^2 - 4\mathclose{\text{.}} So k′(x)=2x,k'(x) = 2x\mathclose{\text{,}} for x≠0:x \ne 0\mathclose{\text{:}} kk is not defined at 0, so neither is k′.k'\mathclose{\text{.}}

(d) π\pi and π2\pi^2 are constants: s′(t)=2πt,s'(t) = 2\pi t\mathclose{\text{,}} and the constant term π2\pi^2 contributes 0.

Exercise 4.3★A cooling cup of tea

Suppose a cup of tea is poured at t=0,t = 0\mathclose{\text{,}} and its temperature after tt minutes is T(t)T(t) degrees Celsius, with T(10)=62T(10) = 62 and T′(10)=−1.5.T'(10) = -1.5\mathclose{\text{.}} (The numbers are made up.)

  • (a) What are the units of T′(10),T'(10)\mathclose{\text{,}} and what does T′(10)=−1.5T'(10) = -1.5 say in words?
  • (b) Use the tangent line at t=10t = 10 to estimate the temperature at t=10.5t = 10.5 and at t=12.t = 12\mathclose{\text{.}}
  • (c) Does dTdt∣t=10=−1.5\left.\dfrac{dT}{dt}\right|_{t=10} = -1.5 say the same as T′(10)=−1.5?T'(10) = -1.5\mathclose{\text{?}} Does ddt(T(10))=−1.5?\dfrac{d}{dt}\big(T(10)\big) = -1.5\mathclose{\text{?}}
Show solution✓ Checked

(a) Degrees Celsius per minute. Ten minutes after it was poured, the tea is cooling at a rate of 1.5 degrees per minute: its temperature is falling, which is what the minus sign says.

(b) The tangent line is T≈62−1.5(t−10).T \approx 62 - 1.5(t - 10)\mathclose{\text{.}} At t=10.5t = 10.5 it gives 62−0.75=61.2562 - 0.75 = 61.25 °C, and at t=12t = 12 it gives 62−3=5962 - 3 = 59 °C. The second estimate is less reliable: the tangent line follows the curve only near t=10,t = 10\mathclose{\text{,}} and tea cools more slowly as it approaches room temperature.

(c) Yes: dTdt∣t=10=−1.5\left.\dfrac{dT}{dt}\right|_{t=10} = -1.5 is T′(10)=−1.5T'(10) = -1.5 in Leibniz’s notation. No: ddt(T(10))\dfrac{d}{dt}\big(T(10)\big) is the derivative of the number T(10)=62,T(10) = 62\mathclose{\text{,}} a constant, so it is 0, not −1.5.-1.5\mathclose{\text{.}}

Exercise 4.4★Where is it not differentiable?

For each function, find the points where it is not differentiable, and say whether each is a corner, a cusp, a vertical tangent or a discontinuity. Give the one-sided derivatives where they exist.

  • (a) f(x)=∣x−2∣f(x) = \abs{x - 2}
  • (b) g(x)=(x+1)2/3,g(x) = (x + 1)^{2/3}\mathclose{\text{,}} with the real cube root
  • (c) k(x)=x2k(x) = x^2 for x<1,x < 1\mathclose{\text{,}} and k(x)=2xk(x) = 2x for x≥1x \ge 1
Show solution✓ Checked

(a) Away from 2, ff is x−2x - 2 or 2−x2 - x near each point, so it is differentiable, with derivative 1 or −1.-1\mathclose{\text{.}} At 2, f(2)=0f(2) = 0 and the quotient is ∣h∣h,\dfrac{\abs{h}}{h}\mathclose{\text{,}} as for ∣x∣\abs{x} at 0: f+′(2)=1f'_+(2) = 1 and f−′(2)=−1.f'_-(2) = -1\mathclose{\text{.}} A corner at x=2.x = 2\mathclose{\text{.}}

(b) At −1,-1\mathclose{\text{,}} g(−1)=0g(-1) = 0 and the quotient is (h2)1/3h=h−1/3,\dfrac{(h^2)^{1/3}}{h} = h^{-1/3}\mathclose{\text{,}} which tends to ∞\infty as h→0+h \to 0^+ and to −∞-\infty as h→0−.h \to 0^-\mathclose{\text{.}} A cusp at x=−1,x = -1\mathclose{\text{,}} the cusp of x2/3x^{2/3} moved 1 to the left; no one-sided derivative exists there. Elsewhere gg is differentiable: the chain rule of Module 5 and the power rule for fractional exponents of Module 6 give g′(x)=23(x+1)−1/3g'(x) = \tfrac23(x + 1)^{-1/3} for x≠−1.x \ne -1\mathclose{\text{.}}

(c) k(1)=2,k(1) = 2\mathclose{\text{,}} but as x→1−,x \to 1^-\mathclose{\text{,}} k(x)=x2→1:k(x) = x^2 \to 1\mathclose{\text{:}} kk is not continuous at 1, so by Theorem 4.1 it is not differentiable there. A discontinuity (a jump). With k(1)=2,k(1) = 2\mathclose{\text{,}} the quotients for h>0h > 0 and for h<0h < 0 are

2(1+h)−2h=2,(1+h)2−2h=−1+2h+h2h.\begin{aligned} \frac{2(1 + h) - 2}{h} &= 2, \\ \frac{(1 + h)^2 - 2}{h} &= \frac{-1 + 2h + h^2}{h}. \end{aligned}

So k+′(1)=2,k'_+(1) = 2\mathclose{\text{,}} while the left-hand quotient grows without bound as h→0−h \to 0^- (its numerator tends to −1),-1\mathclose{\text{),}} and there is no left-hand derivative. Away from 1, kk is a polynomial near each point, hence differentiable.

Exercise 4.5★★Tangent lines with a given slope

Let f(x)=x3−3x2.f(x) = x^3 - 3x^2\mathclose{\text{.}}

  • (a) Find the points where the tangent line is parallel to the line y=9x+1,y = 9x + 1\mathclose{\text{,}} and the equations of those tangent lines.
  • (b) Is there a point where the tangent line has slope −4?-4\mathclose{\text{?}}
Show solution✓ Checked

(a) Parallel lines have equal slopes, so solve f′(x)=9.f'(x) = 9\mathclose{\text{.}} By the rules f′(x)=3x2−6x,f'(x) = 3x^2 - 6x\mathclose{\text{,}} and

3x2−6x=9  ⟺  x2−2x−3=0  ⟺  (x−3)(x+1)=0,\begin{aligned} &3x^2 - 6x = 9 \\ &\iff x^2 - 2x - 3 = 0 \\ &\iff (x - 3)(x + 1) = 0, \end{aligned}

so x=3x = 3 or x=−1.x = -1\mathclose{\text{.}} Since f(3)=27−27=0,f(3) = 27 - 27 = 0\mathclose{\text{,}} the tangent at (3,0)(3, 0) is y=9(x−3),y = 9(x - 3)\mathclose{\text{,}} that is, y=9x−27.y = 9x - 27\mathclose{\text{.}} Since f(−1)=−1−3=−4,f(-1) = -1 - 3 = -4\mathclose{\text{,}} the tangent at (−1,−4)(-1, -4) is y=−4+9(x+1),y = -4 + 9(x + 1)\mathclose{\text{,}} that is, y=9x+5.y = 9x + 5\mathclose{\text{.}}

(b) Complete the square: f′(x)=3(x2−2x)=3(x−1)2−3≥−3f'(x) = 3(x^2 - 2x) = 3(x - 1)^2 - 3 \ge -3 for every x.x\mathclose{\text{.}} Every tangent line has slope at least −3,-3\mathclose{\text{,}} so none has slope −4.-4\mathclose{\text{.}} (The equation 3x2−6x+4=03x^2 - 6x + 4 = 0 has no real solution: its discriminant is 36−48<0.)36 - 48 < 0\mathclose{\text{.)}}

Exercise 4.6★★A derivative function from the definition

Let f(x)=1x+1.f(x) = \dfrac{1}{x + 1}\mathclose{\text{.}}

  • (a) Use the definition to show that f′(x)=−1(x+1)2f'(x) = -\dfrac{1}{(x + 1)^2} for x≠−1.x \ne -1\mathclose{\text{.}}
  • (b) Find the tangent line at x=1.x = 1\mathclose{\text{.}}
  • (c) Explain from the formula why every tangent line of this graph slopes downwards.
Show solution✓ Checked

(a) Let x≠−1.x \ne -1\mathclose{\text{.}} Combine the fractions over the common denominator (x+h+1)(x+1):(x + h + 1)(x + 1)\mathclose{\text{:}}

f(x+h)−f(x)=1x+h+1−1x+1=(x+1)−(x+h+1)(x+h+1)(x+1)=−h(x+h+1)(x+1).\begin{aligned} &f(x + h) - f(x) \\ &\quad= \frac{1}{x + h + 1} - \frac{1}{x + 1} \\ &\quad= \frac{(x + 1) - (x + h + 1)}{(x + h + 1)(x + 1)} \\ &\quad= \frac{-h}{(x + h + 1)(x + 1)}. \end{aligned}

Divide by h≠0h \ne 0 and let h→0;h \to 0\mathclose{\text{;}} since x+1≠0,x + 1 \ne 0\mathclose{\text{,}} direct substitution applies:

f′(x)=lim⁡h→0−1(x+h+1)(x+1)=−1(x+1)2.\begin{aligned} f'(x) &= \lim_{h\to0}\frac{-1}{(x + h + 1)(x + 1)} \\ &= -\frac{1}{(x + 1)^2}. \end{aligned}

At x=−1,x = -1\mathclose{\text{,}} ff is not defined, so neither is f′.f'\mathclose{\text{.}}

(b) f(1)=12f(1) = \tfrac12 and f′(1)=−14,f'(1) = -\tfrac14\mathclose{\text{,}} so the tangent line is y=12−14(x−1),y = \tfrac12 - \tfrac14(x - 1)\mathclose{\text{,}} that is, y=34−x4.y = \tfrac34 - \tfrac{x}{4}\mathclose{\text{.}}

(c) For x≠−1,x \ne -1\mathclose{\text{,}} (x+1)2>0,(x + 1)^2 > 0\mathclose{\text{,}} so f′(x)=−1(x+1)2<0:f'(x) = -\dfrac{1}{(x + 1)^2} < 0\mathclose{\text{:}} every tangent line has a negative slope.

Exercise 4.7★★A ball thrown upwards

Suppose you throw a ball straight up at 14.7 m/s from 1.5 m above the ground. If air resistance is negligible, its height after tt seconds is y(t)=1.5+14.7t−4.9t2y(t) = 1.5 + 14.7t - 4.9t^2 metres, until it lands.

  • (a) Find its velocity and its acceleration, and say what their signs mean.
  • (b) When is the ball highest, and how high is it then?
  • (c) When does it land, and at what speed? Give both to two decimal places.
Show solution✓ Checked

(a) v(t)=y′(t)=14.7−9.8tv(t) = y'(t) = 14.7 - 9.8t m/s and a(t)=v′(t)=−9.8a(t) = v'(t) = -9.8 m/s². Heights are measured upwards, so v>0v > 0 means rising and v<0v < 0 falling; the constant negative acceleration is gravity, pulling downwards. While the ball rises, v>0v > 0 and a<0a < 0 have opposite signs, so it slows down; once it falls, both are negative and it speeds up.

(b) At the top the ball stops rising and has not yet started to fall: v(t)=0,v(t) = 0\mathclose{\text{,}} so t=14.7/9.8=1.5t = 14.7/9.8 = 1.5 s. Then y(1.5)=1.5+22.05−11.025=12.525y(1.5) = 1.5 + 22.05 - 11.025 = 12.525 m.

(c) The ball lands when y(t)=0,y(t) = 0\mathclose{\text{,}} that is, 4.9t2−14.7t−1.5=0.4.9t^2 - 14.7t - 1.5 = 0\mathclose{\text{.}} By the quadratic formula,

t=14.7±14.72+4⋅4.9⋅1.52⋅4.9=14.7±245.499.8.\begin{aligned} t &= \frac{14.7 \pm \sqrt{14.7^2 + 4 \cdot 4.9 \cdot 1.5}}{2 \cdot 4.9} \\ &= \frac{14.7 \pm \sqrt{245.49}}{9.8}. \end{aligned}

The minus sign gives a negative time, before the throw, so t=14.7+245.499.8≈3.10t = \dfrac{14.7 + \sqrt{245.49}}{9.8} \approx 3.10 s. Then 9.8t=14.7+245.49,9.8t = 14.7 + \sqrt{245.49}\mathclose{\text{,}} so v=14.7−9.8t=−245.49≈−15.67v = 14.7 - 9.8t = -\sqrt{245.49} \approx -15.67 m/s. The ball lands at about 15.67 m/s, faster than it was thrown, because it lands 1.5 m below the point where it left your hand: it falls 12.525 m from the top, further than the 11.025 m it rose.

Exercise 4.8★★Making a piecewise function differentiable

Find the constants bb and cc for which

f(x)={x2if x≤1,bx+cif x>1f(x) = \begin{cases} x^2 & \text{if } x \le 1, \\ bx + c & \text{if } x > 1 \end{cases}

is differentiable at x=1.x = 1\mathclose{\text{.}}

Show solution✓ Checked

Two conditions are needed.

Continuity. By Theorem 4.1, ff must be continuous at 1. From the left, f(x)=x2→1=f(1);f(x) = x^2 \to 1 = f(1)\mathclose{\text{;}} from the right, f(x)=bx+c→b+c.f(x) = bx + c \to b + c\mathclose{\text{.}} So b+c=1.b + c = 1\mathclose{\text{.}}

Equal one-sided derivatives. With f(1)=1,f(1) = 1\mathclose{\text{,}} the left-hand quotient is (1+h)2−1h=2+h,\dfrac{(1 + h)^2 - 1}{h} = 2 + h\mathclose{\text{,}} so f−′(1)=2.f'_-(1) = 2\mathclose{\text{.}} For h>0h > 0 the right-hand quotient is

b(1+h)+c−1h=(b+c−1)+bhh=b,\begin{aligned} &\frac{b(1 + h) + c - 1}{h} \\ &\quad= \frac{(b + c - 1) + bh}{h} = b, \end{aligned}

using b+c=1.b + c = 1\mathclose{\text{.}} So f+′(1)=b,f'_+(1) = b\mathclose{\text{,}} and the two agree exactly when b=2.b = 2\mathclose{\text{.}} Then c=1−b=−1.c = 1 - b = -1\mathclose{\text{.}}

So b=2b = 2 and c=−1.c = -1\mathclose{\text{.}} With these values both one-sided derivatives at 1 are 2, so f′(1)=2.f'(1) = 2\mathclose{\text{.}} The line y=2x−1y = 2x - 1 is the tangent to y=x2y = x^2 at (1,1):(1, 1)\mathclose{\text{:}} the graph leaves the parabola along its own tangent line. If b+c≠1,b + c \ne 1\mathclose{\text{,}} the right-hand quotient contains the term b+c−1h,\dfrac{b + c - 1}{h}\mathclose{\text{,}} which grows without bound as h→0+:h \to 0^+\mathclose{\text{:}} there is no right-hand derivative, and ff is not differentiable at 1, as Theorem 4.1 predicts for a function that is not continuous there.

Exercise 4.9★★Forward and central differences for 1/x

Let f(x)=1xf(x) = \dfrac1x and a=1,a = 1\mathclose{\text{,}} so that f′(1)=−1f'(1) = -1 (the method of Example 4.4 gives f′(a)=−1/a2).f'(a) = -1/a^2\mathclose{\text{).}}

  • (a) Show that, for 0<h<1,0 < h < 1\mathclose{\text{,}} the forward difference equals −11+h-\dfrac{1}{1 + h} and the central difference equals −11−h2.-\dfrac{1}{1 - h^2}\mathclose{\text{.}}
  • (b) Compute both, and their errors, for h=0.1h = 0.1 and h=0.01,h = 0.01\mathclose{\text{,}} to six decimal places.
  • (c) In exact arithmetic, roughly how small must hh be for each error to be less than 10−610^{-6} in size?
Show solution✓ Checked

(a) Forward: put the two terms over the common denominator 1+h,1 + h\mathclose{\text{,}}

11+h−1=1−(1+h)1+h=−h1+h,\begin{aligned} \frac{1}{1 + h} - 1 &= \frac{1 - (1 + h)}{1 + h} \\ &= \frac{-h}{1 + h}, \end{aligned}

and dividing by hh gives −11+h.-\dfrac{1}{1 + h}\mathclose{\text{.}} Central: over the common denominator (1+h)(1−h)=1−h2,(1 + h)(1 - h) = 1 - h^2\mathclose{\text{,}}

11+h−11−h=(1−h)−(1+h)1−h2=−2h1−h2,\begin{aligned} &\frac{1}{1 + h} - \frac{1}{1 - h} \\ &\quad= \frac{(1 - h) - (1 + h)}{1 - h^2} \\ &\quad= \frac{-2h}{1 - h^2}, \end{aligned}

and dividing by 2h2h gives −11−h2.-\dfrac{1}{1 - h^2}\mathclose{\text{.}}

(b) The errors are f′(1)=−1f'(1) = -1 minus the estimates:

−1+11+h=−h1+h,−1+11−h2=h21−h2.\begin{aligned} -1 + \frac{1}{1 + h} &= -\frac{h}{1 + h}, \\[6pt] -1 + \frac{1}{1 - h^2} &= \frac{h^2}{1 - h^2}. \end{aligned}

At the two steps:

  • h=0.1:h = 0.1\mathclose{\text{:}} forward −0.909091,-0.909091\mathclose{\text{,}} error −0.090909;-0.090909\mathclose{\text{;}} central −1.010101,-1.010101\mathclose{\text{,}} error 0.010101.0.010101\mathclose{\text{.}}
  • h=0.01:h = 0.01\mathclose{\text{:}} forward −0.990099,-0.990099\mathclose{\text{,}} error −0.009901;-0.009901\mathclose{\text{;}} central −1.000100,-1.000100\mathclose{\text{,}} error 0.000100.0.000100\mathclose{\text{.}}

The forward difference is too large and the central difference too small, so their errors have opposite signs.

(c) The sizes of the errors are

h1+h<h,h21−h2>h2.\begin{aligned} \frac{h}{1 + h} &< h, \\[6pt] \frac{h^2}{1 - h^2} &> h^2. \end{aligned}

The forward error is a little less than hh in size, so its size falls below 10−610^{-6} when hh is about 10−6;10^{-6}\mathclose{\text{;}} at h=10−6h = 10^{-6} it is 10−6/(1+10−6)<10−6.10^{-6}/(1 + 10^{-6}) < 10^{-6}\mathclose{\text{.}} The central error is a little more than h2,h^2\mathclose{\text{,}} so hh about 10−310^{-3} suffices: h=0.000999h = 0.000999 gives about 9.98×10−7.9.98 \times 10^{-7}\mathclose{\text{.}} The central difference reaches the same accuracy with a step a thousand times larger; in floating point that matters, because larger steps suffer less rounding error.

Exercise 4.10★★★The power rule by factoring
  • (a) Show that for every positive integer n,n\mathclose{\text{,}}
    xn−an=(x−a)(xn−1+xn−2a+⋯+xan−2+an−1).\begin{aligned} &x^n - a^n \\ &\quad= (x - a)\big(x^{n-1} + x^{n-2}a \\ &\qquad + \dots + xa^{n-2} + a^{n-1}\big). \end{aligned}
  • (b) Use (a) and the second form of the definition, f′(a)=lim⁡x→af(x)−f(a)x−a,f'(a) = \lim_{x\to a}\dfrac{f(x) - f(a)}{x - a}\mathclose{\text{,}} to give another proof of the power rule.
Show solution✓ Checked

(a) Multiply out the right-hand side. Multiplying the long bracket by xx gives

xn+xn−1a+xn−2a2+⋯+xan−1,\begin{aligned} &x^n + x^{n-1}a + x^{n-2}a^2 \\ &\quad + \dots + xa^{n-1}, \end{aligned}

and multiplying it by −a-a gives

−xn−1a−xn−2a2−⋯−xan−1−an.\begin{aligned} &-x^{n-1}a - x^{n-2}a^2 \\ &\quad - \dots - xa^{n-1} - a^n. \end{aligned}

Every term of the first line except xnx^n cancels against a term of the second line, and every term of the second line except −an-a^n cancels against a term of the first. What remains is xn−an.x^n - a^n\mathclose{\text{.}} (For n=1n = 1 the bracket is the single term 1, and the identity reads x−a=x−a.)x - a = x - a\mathclose{\text{.)}}

(b) For x≠a,x \ne a\mathclose{\text{,}} divide (a) by x−a:x - a\mathclose{\text{:}}

xn−anx−a=xn−1+xn−2a+⋯+xan−2+an−1.\begin{aligned} &\frac{x^n - a^n}{x - a} \\ &\quad= x^{n-1} + x^{n-2}a \\ &\qquad + \dots + xa^{n-2} + a^{n-1}. \end{aligned}

The right-hand side is a polynomial in xx with nn terms. As x→a,x \to a\mathclose{\text{,}} direct substitution turns each term into an−1,a^{n-1}\mathclose{\text{,}} so the limit is nan−1.na^{n-1}\mathclose{\text{.}} So (xn)′=nxn−1\big(x^n\big)' = nx^{n-1} at every point a:a\mathclose{\text{:}} the power rule again, this time without the binomial expansion.

Exercise 4.11★★★Wiggles at the origin

Let f(x)=xsin⁡1xf(x) = x\sin\dfrac1x and g(x)=x2sin⁡1xg(x) = x^2\sin\dfrac1x for x≠0,x \ne 0\mathclose{\text{,}} with f(0)=g(0)=0.f(0) = g(0) = 0\mathclose{\text{.}}

  • (a) Show that ff is continuous at 0 but not differentiable there.
  • (b) Show that gg is differentiable at 0, with g′(0)=0.g'(0) = 0\mathclose{\text{.}}

(Use the squeeze theorem of Module 2.)

Show solution✓ Checked

(a) For x≠0,x \ne 0\mathclose{\text{,}} ∣sin⁡(1/x)∣≤1,\abs{\sin(1/x)} \le 1\mathclose{\text{,}} so −∣x∣≤xsin⁡1x≤∣x∣.-\abs{x} \le x\sin\dfrac1x \le \abs{x}\mathclose{\text{.}} Both bounds tend to 0, so by the squeeze theorem lim⁡x→0f(x)=0=f(0):\lim_{x\to0}f(x) = 0 = f(0)\mathclose{\text{:}} ff is continuous at 0. Its difference quotient at 0 is

f(h)−f(0)h=hsin⁡(1/h)h=sin⁡1h,\begin{aligned} \frac{f(h) - f(0)}{h} &= \frac{h\sin(1/h)}{h} \\ &= \sin\frac1h, \end{aligned}

which has no limit as h→0.h \to 0\mathclose{\text{.}} At h=1π/2+2kπh = \dfrac{1}{\pi/2 + 2k\pi} it equals 1, and at h=13π/2+2kπh = \dfrac{1}{3\pi/2 + 2k\pi} it equals −1,-1\mathclose{\text{,}} for every positive integer k;k\mathclose{\text{;}} both lists of hh tend to 0. A limit LL would have to be within 12\tfrac12 of both 1 and −1-1 for all small h,h\mathclose{\text{,}} which is impossible. So f′(0)f'(0) does not exist: the secants from the origin swing between slopes −1-1 and 1 forever.

(b) The difference quotient of gg at 0 is

g(h)−g(0)h=h2sin⁡(1/h)h=hsin⁡1h,\begin{aligned} \frac{g(h) - g(0)}{h} &= \frac{h^2\sin(1/h)}{h} \\ &= h\sin\frac1h, \end{aligned}

and −∣h∣≤hsin⁡1h≤∣h∣,-\abs{h} \le h\sin\dfrac1h \le \abs{h}\mathclose{\text{,}} so it tends to 0 by the squeeze theorem: g′(0)=0.g'(0) = 0\mathclose{\text{.}} The graph of gg wiggles infinitely often near 0, but the wiggles are trapped between y=x2y = x^2 and y=−x2,y = -x^2\mathclose{\text{,}} which both have the horizontal tangent y=0y = 0 at the origin, and so does g.g\mathclose{\text{.}}

Exercise 4.12★★★The best step size

Section 4.7 bounded the size of the error of the forward difference of x3x^3 at 1 by about 3h+ε/h.3h + \varepsilon/h\mathclose{\text{.}} More generally, let E(h)=Ch+Kh,E(h) = Ch + \dfrac{K}{h}\mathclose{\text{,}} with positive constants CC and K.K\mathclose{\text{.}}

  • (a) Show that E(h)≥2CKE(h) \ge 2\sqrt{CK} for every h>0,h > 0\mathclose{\text{,}} with equality exactly when h=K/C.h = \sqrt{K/C}\mathclose{\text{.}} (Expand (Ch−K/h)2≥0.)\big(\sqrt{Ch} - \sqrt{K/h}\big)^2 \ge 0\mathclose{\text{.)}}
  • (b) So the best step is h∗=K/C,h^* = \sqrt{K/C}\mathclose{\text{,}} where the two parts of EE are equal. Find h∗h^* and E(h∗)E(h^*) for C=3C = 3 and K=ε≈2.2×10−16,K = \varepsilon \approx 2.2 \times 10^{-16}\mathclose{\text{,}} and compare with the table in Section 4.7.
  • (c) Suppose the values of ff carried only about 8 correct digits, as with noisy measurements, so that K=10−8.K = 10^{-8}\mathclose{\text{.}} What are the best step and the smallest value of EE now?
Show solution✓ Checked

(a) For h>0h > 0 both square roots are defined, and

(Ch−K/h)2=Ch−2Ch⋅K/h+Kh=Ch−2CK+Kh.\begin{aligned} &\Big(\sqrt{Ch} - \sqrt{K/h}\Big)^2 \\ &\quad= Ch - 2\sqrt{Ch \cdot K/h} + \frac{K}{h} \\ &\quad= Ch - 2\sqrt{CK} + \frac{K}{h}. \end{aligned}

A square is never negative, so Ch+Kh≥2CK.Ch + \dfrac{K}{h} \ge 2\sqrt{CK}\mathclose{\text{.}} Equality holds exactly when the square is 0, that is, when Ch=K/h,\sqrt{Ch} = \sqrt{K/h}\mathclose{\text{,}} which means Ch=K/h,Ch = K/h\mathclose{\text{,}} or h2=K/C,h^2 = K/C\mathclose{\text{,}} or (since h>0)h > 0\mathclose{\text{)}} h=K/C.h = \sqrt{K/C}\mathclose{\text{.}} At that hh the two parts ChCh and K/hK/h are equal.

(b) h∗=ε/3≈8.6×10−9h^* = \sqrt{\varepsilon/3} \approx 8.6 \times 10^{-9} and E(h∗)=23ε≈5.2×10−8.E(h^*) = 2\sqrt{3\varepsilon} \approx 5.2 \times 10^{-8}\mathclose{\text{.}} The table in Section 4.7 has its smallest forward error, in size, at h=10−8,h = 10^{-8}\mathclose{\text{,}} the step nearest h∗,h^*\mathclose{\text{,}} as predicted. The error there, about −4×10−9,-4 \times 10^{-9}\mathclose{\text{,}} is much smaller in size than the model’s 5.2×10−8,5.2 \times 10^{-8}\mathclose{\text{,}} because by luck the rounding error had the opposite sign to the truncation error and cancelled most of it. At the neighbouring steps 10−710^{-7} and 10−910^{-9} the errors, about −3×10−7-3 \times 10^{-7} and −2.5×10−7,-2.5 \times 10^{-7}\mathclose{\text{,}} have the sizes that the model predicts (3.0×10−73.0 \times 10^{-7} and 2.3×10−7).2.3 \times 10^{-7}\mathclose{\text{).}}

(c) h∗=10−8/3≈5.8×10−5,h^* = \sqrt{10^{-8}/3} \approx 5.8 \times 10^{-5}\mathclose{\text{,}} and E(h∗)=23×10−8≈3.5×10−4.E(h^*) = 2\sqrt{3 \times 10^{-8}} \approx 3.5 \times 10^{-4}\mathclose{\text{.}} With values good to 8 digits, a forward difference gives only about 4 correct digits, and it needs a much larger step. Noisy data give rough derivatives.

10

Check yourself

1
For f(x)=x2,f(x) = x^2\mathclose{\text{,}} which expression is f′(3)?f'(3)\mathclose{\text{?}}
2
If f(2)=5f(2) = 5 and f′(2)=−3,f'(2) = -3\mathclose{\text{,}} the tangent line to the graph of ff at x=2x = 2 is
3
Which of these functions is continuous at 0 but not differentiable there?
4
ddx(5x4−2x+7)\dfrac{d}{dx}\big(5x^4 - 2x + 7\big) is
5
If y=x3,y = x^3\mathclose{\text{,}} then dydx∣x=2\left.\dfrac{dy}{dx}\right|_{x=2} equals
6
At a point where the graph of ff has a horizontal tangent, the graph of f′f'
7
A particle moves along a line with position s(t)=t3−3ts(t) = t^3 - 3t metres after tt seconds. Its acceleration at t=2t = 2 is
8
In floating-point arithmetic, why is the forward difference of x3x^3 at 1 worse with h=10−14h = 10^{-14} than with h=10−8?h = 10^{-8}\mathclose{\text{?}}
Finished this module?