Antiderivatives
Differentiation goes from a function to its rate of change. Antidifferentiation goes back: given the rate, recover the function. This lesson defines antiderivatives, proves that they all differ by a constant, builds the basic table, and uses initial conditions to solve motion problems, the same reconstruction a game engine does every frame. For a first look see Antiderivatives.
Definition and the ""
A function is an antiderivative of on an interval if for all in .
Theorem. If is an antiderivative of on an interval , then every antiderivative of on has the form for a constant .
Proof. If on , then on , so by the Mean Value Theorem is constant there. ∎
The word interval matters. On the domain , the function
is an antiderivative of and is not of the form with one constant: each piece of the domain gets its own constant.
The basic table
Read each derivative formula backwards (valid on any interval where both sides are defined):
| antiderivative | |
|---|---|
| () | |
| , | , |
| , | , |
| , | , |
| , | , |
| (linearity) |
Every entry can be checked by differentiating the right column. That check, "differentiate your answer", is the single most useful habit in integration. SymPy can do it for you; note that it omits the :
import sympy as sp
x = sp.symbols('x', positive=True)
fs = [3*x**4 - 2/sp.sqrt(x), 5*sp.exp(x) + 2**x, 4/(1 + x**2), sp.sec(x)**2 - 6*sp.cos(x)]
for f in fs:
F = sp.integrate(f, x)
ok = sp.simplify(sp.diff(F, x) - f) == 0
print(f"f = {str(f):28} F = {str(F):36} F' == f: {ok}")f = 3*x**4 - 2/sqrt(x) F = -4*sqrt(x) + 3*x**5/5 F' == f: True
f = 2**x + 5*exp(x) F = 2**x/log(2) + 5*exp(x) F' == f: True
f = 4/(x**2 + 1) F = 4*atan(x) F' == f: True
f = -6*cos(x) + sec(x)**2 F = -6*sin(x) + sin(x)/cos(x) F' == f: True
SymPy writes log for , atan for , and here chose sin(x)/cos(x) for . Different-looking answers can be equal, or differ by a constant; differentiating settles it.
Initial value problems
An equation like together with a value fixes the constant. Higher derivatives need one condition per antidifferentiation.
Example. , , .
import sympy as sp
x = sp.symbols('x')
f = sp.Function('f')
sol = sp.dsolve(sp.Eq(f(x).diff(x, 2), 6*x - 4), f(x), ics={f(0): 3, f(1): 2})
print(sol)Eq(f(x), x**3 - 2*x**2 + 3)
Rectilinear motion
If is position, then and . Given the acceleration and the initial velocity and position, two antiderivatives recover the motion. A ball thrown upward at from a 20 m ledge, with :
It peaks when , at , and lands when , at the positive root of .
What a physics engine does
A game engine does not know in closed form. It knows the acceleration and antidifferentiates numerically, frame by frame: v += a·dt, then x += v·dt (semi-implicit Euler). For constant acceleration the error comes only from the time step, and we can compare it with the exact antiderivative:
import math
g, v0, s0 = -9.8, 15.0, 20.0
t_land = (-v0 - math.sqrt(v0**2 - 4 * (g / 2) * s0)) / g # positive root, quadratic formula
print(f"exact: peak at t = {v0 / -g:.4f} s, height {s0 + v0**2 / (2 * -g):.4f} m, lands at t = {t_land:.4f} s")
for dt in (1 / 30, 1 / 60, 1 / 240):
s, v, t = s0, v0, 0.0
while True:
v += g * dt # antiderivative of a
s_new = s + v * dt # antiderivative of v
if s_new <= 0:
t += dt * s / (s - s_new) # interpolate the landing time
break
s, t = s_new, t + dt
print(f"dt = 1/{round(1 / dt):3}: lands at t = {t:.4f} s (error {t - t_land:+.4f})")exact: peak at t = 1.5306 s, height 31.4796 m, lands at t = 4.0653 s
dt = 1/ 30: lands at t = 4.0385 s (error -0.0267)
dt = 1/ 60: lands at t = 4.0519 s (error -0.0134)
dt = 1/240: lands at t = 4.0619 s (error -0.0033)
Halving dt roughly halves the error: the method is first-order. Better integrators (Verlet, RK4) are covered in Game Physics Integration.
In software
- Physics and animation: positions are antiderivatives of velocities, which are antiderivatives of accelerations. Every engine's integrator is a numerical antiderivative.
- Prefix sums: the discrete analogue of an antiderivative is a running total (
itertools.accumulate,np.cumsum); a difference of two prefix sums answers a range query in O(1), just as will give a definite integral in the Fundamental Theorem. - Odometry and dead reckoning: robots and phones integrate accelerometer and gyroscope data to estimate position. The unknown (initial conditions) and accumulated error are why they drift without GPS.
Exercises
Exercise 1: General antiderivative
Find the general antiderivative of .
Answer
(one constant on each interval of and ).
Solution
Exercise 2: An initial value problem
Find with and .
Answer
.
Solution
; .
import sympy as sp
x = sp.symbols('x')
f = sp.Function('f')
print(sp.dsolve(sp.Eq(f(x).diff(x), sp.exp(x) + 1 / (1 + x**2)), f(x), ics={f(0): 5}))Eq(f(x), exp(x) + atan(x) + 4)
Exercise 3: Braking distance
A car at 30 m/s brakes with constant deceleration . How far does it travel before stopping?
Answer
(it stops after 5 s).
Solution
Exercise 4: Two constants from a graph
, the graph passes through and has slope there. Find .
Answer
.
Solution
Test yourself
Why do two antiderivatives of on an interval differ by a constant?
- Their difference has derivative 0, so it is constant by the Mean Value Theorem
- By definition
- Because is continuous
- By the chain rule
Which is an antiderivative of ?
How many initial conditions are needed to pin down if you know ?
- 1
- 3
- 2
- None