Higher Derivatives
The derivative of a function is itself a function, so you can differentiate it again. The second derivative measures how the slope changes: acceleration is the second derivative of position, and curvature of a graph shows up as the sign of . Games, animation and physics engines work with position, velocity and acceleration every frame.
Notation
| Order | Prime notation | Leibniz notation | Physics name (for position ) |
|---|---|---|---|
| 1st | velocity | ||
| 2nd | acceleration | ||
| 3rd | jerk | ||
| nth | (4th: snap) |
Example with a polynomial: each derivative lowers the degree by one, until it hits zero.
Some functions never run out: is its own derivative at every order, and cycles with period 4.
import sympy as sp
x = sp.symbols('x')
f = x**4 - 3 * x**2 + 2 * x
for n in range(1, 6):
print(f"f^({n}) =", sp.diff(f, x, n))
print("10th derivative of sin(2x):", sp.diff(sp.sin(2 * x), x, 10))
print("3rd derivative of x*exp(x):", sp.factor(sp.diff(x * sp.exp(x), x, 3)))f^(1) = 4*x**3 - 6*x + 2
f^(2) = 6*(2*x**2 - 1)
f^(3) = 24*x
f^(4) = 24
f^(5) = 0
10th derivative of sin(2x): -1024*sin(2*x)
3rd derivative of x*exp(x): (x + 3)*exp(x)
sp.diff(f, x, n) takes the -th derivative directly. Each derivative of brings out a factor 2, so the 10th has .
Position, velocity, acceleration
A ball thrown upwards at 15 m/s from 2 m height (gravity ):
The ball is highest when , at . The acceleration is negative the whole time, even on the way up: velocity is decreasing throughout.
Numeric evidence: second differences
The second derivative can be estimated from three samples:
This is exactly what you do with positions recorded every frame. Here it recovers from sampled positions:
def s(t):
return 2 + 15 * t - 4.9 * t**2
t0 = 1.0
for h in [0.5, 0.1, 0.01, 0.001]:
v = (s(t0 + h) - s(t0 - h)) / (2 * h)
a = (s(t0 + h) - 2 * s(t0) + s(t0 - h)) / h**2
print(f"h = {h:<6} v ≈ {v:.6f} a ≈ {a:.6f}")
for h in [1e-5, 1e-6, 1e-7]:
a = (s(t0 + h) - 2 * s(t0) + s(t0 - h)) / h**2
print(f"h = {h:<6} a ≈ {a:.6f} (rounding noise / h² takes over)")h = 0.5 v ≈ 5.200000 a ≈ -9.800000
h = 0.1 v ≈ 5.200000 a ≈ -9.800000
h = 0.01 v ≈ 5.200000 a ≈ -9.800000
h = 0.001 v ≈ 5.200000 a ≈ -9.800000
h = 1e-05 a ≈ -9.799983 (rounding noise / h² takes over)
h = 1e-06 a ≈ -9.803713 (rounding noise / h² takes over)
h = 1e-07 a ≈ -9.414691 (rounding noise / h² takes over)
For a quadratic the formula is exact for any reasonable , but notice the bottom rows: dividing by makes the floating-point problem much worse than for first derivatives.
Concavity: what says about shape
| Sign of on an interval | Shape | Picture |
|---|---|---|
| concave up | holds water, like | |
| concave down | spills water, like | |
| sign changes at | inflection point at | at 0 |
This is used in Maxima, Minima and Curve Shape to tell maxima from minima.
In software: velocity and acceleration in games
A game loop stores position and velocity for each object and updates them every frame with the acceleration (from gravity, thrust, springs). The simplest scheme, semi-implicit Euler, is:
v ← v + a·dt
x ← x + v·dtThat is "velocity is the derivative of position, acceleration is the derivative of velocity", turned into code with a finite time step.
dt, g = 1 / 60, -9.8
y, vy = 2.0, 15.0
t = 0.0
peak = (0.0, y)
while y >= 0:
vy += g * dt # acceleration changes velocity
y += vy * dt # velocity changes position
t += dt
if y > peak[1]:
peak = (t, y)
print(f"simulated peak: t = {peak[0]:.3f} s, height = {peak[1]:.3f} m")
print(f"exact peak: t = {15 / 9.8:.3f} s, height = {2 + 15**2 / (2 * 9.8):.3f} m")
print(f"lands after about {t:.3f} s")simulated peak: t = 1.517 s, height = 13.355 m
exact peak: t = 1.531 s, height = 13.480 m
lands after about 3.183 s
The small differences come from the finite step ; how to shrink them is the topic of Numerical Solutions: Euler and RK4 and of integrators in game physics.
- Jerk (3rd derivative) matters for comfort: camera rigs, elevators and robot arms use motion profiles with limited jerk so that acceleration does not change abruptly. Cubic and quintic easing curves are chosen for smooth higher derivatives (easing curves).
- Second-order optimisation: Newton-type optimisers use the second derivative (the Hessian in many dimensions, Jacobian and Hessian) to choose step sizes.
Exercises
Exercise 1: Keep differentiating
Find and for .
Answer
,
Solution
Exercise 2: Second derivative of a bell curve
Find for .
Answer
Solution
So the bell curve changes concavity where , at .
Exercise 3: A car braking
A car's position is metres for . When does it stop, how far has it gone, and what is its acceleration?
Answer
Stops at after ; acceleration throughout.
Solution
Exercise 4: Acceleration from recorded frames
These heights were logged every : [10.0, 10.951, 11.804, 12.559, 13.216]. Estimate the acceleration at each interior frame with the second-difference formula.
Answer
About at every interior frame.
Solution
ys = [10.0, 10.951, 11.804, 12.559, 13.216]
dt = 0.1
for i in range(1, len(ys) - 1):
a = (ys[i + 1] - 2 * ys[i] + ys[i - 1]) / dt**2
print(f"frame {i}: a ≈ {a:.2f}")frame 1: a ≈ -9.80
frame 2: a ≈ -9.80
frame 3: a ≈ -9.80
Test yourself
If is position, what is ?
- Velocity
- Jerk
- Acceleration
- Distance travelled
What is the 4th derivative of ?
on an interval means the graph is …
- increasing
- concave up
- concave down
- linear