Antiderivatives
An antiderivative runs differentiation backwards. If you know how fast something changes, the antiderivative tells you what the thing itself looks like. This is how a game gets a position from a velocity, and how a physics engine gets a velocity from an acceleration.
The idea
A function is an antiderivative of when . Differentiation asks "what is the slope of this curve?". Antidifferentiation asks the reverse question: "which curve has this slope everywhere?".
For example is an antiderivative of , because . But so is , and . A constant disappears when you differentiate, so it can be anything when you go back. The complete answer is a whole family of functions:
The symbol is the indefinite integral, the notation for "all antiderivatives of". is the constant of integration. Graphically, the family is one curve shifted up and down: every member has the same slope at each .
Note: Two antiderivatives of the same function on an interval always differ by a constant. That is why one covers every possible answer.
A starter table
Every row comes from reading a derivative rule backwards. You can check any of them by differentiating the right-hand side.
| Check | ||
|---|---|---|
| (a constant) | ||
| () | power rule | |
| is its own derivative | ||
The power rule fails for because it would divide by zero; that gap is filled by .
Checking with SymPy
SymPy's integrate returns one antiderivative and leaves out the . Differentiating the result must give the original function back, which is a habit worth copying when you integrate by hand.
import sympy as sp
x = sp.symbols("x")
for f in [3 * x**2, x**5, 1 / x, sp.exp(x), sp.sin(x), 1 / sp.sqrt(x)]:
F = sp.integrate(f, x)
back = sp.simplify(sp.diff(F, x) - f)
print(f"f = {str(f):12} F = {str(F):12} F' - f = {back}")f = 3*x**2 F = x**3 F' - f = 0
f = x**5 F = x**6/6 F' - f = 0
f = 1/x F = log(x) F' - f = 0
f = exp(x) F = exp(x) F' - f = 0
f = sin(x) F = -cos(x) F' - f = 0
f = 1/sqrt(x) F = 2*sqrt(x) F' - f = 0
SymPy writes log(x) for the natural log and does not add the absolute value; on it is the same thing.
Pinning down C with a starting value
The constant is fixed by one known value, an initial condition. A ball is thrown up at 12 m/s from a height of 2 m. Gravity gives the acceleration . Integrate twice:
import sympy as sp
t, C1, C2 = sp.symbols("t C1 C2")
v = sp.integrate(-9.8, t) + C1
v = v.subs(C1, sp.solve(v.subs(t, 0) - 12, C1)[0])
s = sp.integrate(v, t) + C2
s = s.subs(C2, sp.solve(s.subs(t, 0) - 2, C2)[0])
print("v(t) =", v)
print("s(t) =", s)
t_top = sp.solve(v, t)[0]
print("highest point at t =", round(float(t_top), 4), "s, height", round(float(s.subs(t, t_top)), 4), "m")v(t) = 12 - 9.8*t
s(t) = -4.9*t**2 + 12.0*t + 2
highest point at t = 1.2245 s, height 9.3469 m
In software
A game cannot run SymPy every frame. It antidifferentiates numerically: each frame it adds to the position, which is a tiny step of "". Done with a small time step, it tracks the exact formula closely:
dt = 1 / 60 # one frame at 60 fps
y, v = 2.0, 12.0 # start height and velocity
for frame in range(60): # simulate one second
v += -9.8 * dt # velocity = antiderivative of acceleration
y += v * dt # position = antiderivative of velocity
exact = -4.9 * 1**2 + 12 * 1 + 2
print(f"game loop after 1 s: y = {y:.4f}")
print(f"exact formula : y = {exact:.4f}")
print(f"difference : {y - exact:.4f} m")game loop after 1 s: y = 9.0183
exact formula : y = 9.1000
difference : -0.0817 m
The small gap is the price of taking steps instead of integrating exactly; Numerical Integration and Physics Integration in Game Loops look at it closely, and Integrators compares the schemes engines use.
Exercises
Exercise 1: Read the table backwards
Find .
Answer
Solution
Exercise 2: Roots and negative powers
Find .
Answer
Solution
Exercise 3: Find the particular function
and . Find .
Answer
Solution
Exercise 4: Braking car
A car moving at 30 m/s brakes with constant acceleration . Find its position function (start at 0) and how far it travels before it stops. Check with Python.
Answer
; it stops at after .
Solution
import sympy as sp
t = sp.symbols("t")
v = 30 + sp.integrate(-6, t)
s = sp.integrate(v, t)
t_stop = sp.solve(v, t)[0]
print("s(t) =", s, " stops at t =", t_stop, " distance =", s.subs(t, t_stop))s(t) = -3*t**2 + 30*t stops at t = 5 distance = 75
Test yourself
Which function is an antiderivative of ?
Why does an indefinite integral carry ?
- It is a rounding correction
- SymPy needs it
- Constants differentiate to zero, so any constant could have been there
- It marks the variable of integration
Why does not work for ?
- It would divide by zero; the answer is instead
- has no antiderivative
- Negative powers cannot be integrated
- It works fine