A First Look at the Integral
The derivative answers "how fast is it changing?". The integral answers the opposite question: "how much has accumulated?". Total distance from a changing speed, total energy from a changing power, area under a curve: all are integrals. This lesson builds the integral as the limit of sums of thin rectangles (Riemann sums), connects it to the series from earlier in the course, and shows why a game loop that adds v * dt every frame is computing an integral.
Area under a curve
If a car drives at a constant for , it covers : the area of a rectangle under the speed graph. When the speed changes, the distance is still the area under the speed curve, but the region is no longer a rectangle.
The trick is to cut the region into thin vertical strips of width , approximate each strip by a rectangle, and add:
This is a Riemann sum. The heights can be taken at the left end, right end or midpoint of each strip. As and the strips get thinner, the sum approaches a limit, the definite integral:
The sign is a stretched S for "sum", and is the infinitely thin version of .
Riemann sums in code
Let's find the area under from 0 to 2:
def riemann(f, a, b, n, where="left"):
dx = (b - a) / n
offset = {"left": 0.0, "right": 1.0, "mid": 0.5}[where]
return sum(f(a + (i + offset) * dx) for i in range(n)) * dx
f = lambda x: x * x
print(" n left right midpoint")
for n in [4, 10, 100, 1000, 10000]:
print(f"{n:>6} {riemann(f, 0, 2, n):11.6f} {riemann(f, 0, 2, n, 'right'):11.6f} {riemann(f, 0, 2, n, 'mid'):11.6f}")
print("exact 8/3 =", 8 / 3) n left right midpoint
4 1.750000 3.750000 2.625000
10 2.280000 3.080000 2.660000
100 2.626800 2.706800 2.666600
1000 2.662668 2.670668 2.666666
10000 2.666267 2.667067 2.666667
exact 8/3 = 2.6666666666666665
Left sums are too small and right sums too big here ( is increasing), and both close in on . The midpoint rule gets there much faster.
The exact answer from a series formula
For on with right endpoints, and , so the sum is
That uses the sum-of-squares formula from Series and a limit at infinity. In calculus a much quicker route opens up: the Fundamental Theorem of Calculus says integration undoes differentiation. Because the derivative of is , the area is .
import numpy as np
import matplotlib.pyplot as plt
f = lambda x: x**2
a, b, n = 0, 2, 8
dx = (b - a) / n
xs = np.linspace(a, b, 300)
mids = a + (np.arange(n) + 0.5) * dx
plt.figure(figsize=(6, 4))
plt.bar(mids, f(mids), width=dx, alpha=0.4, edgecolor="k", label=f"{n} midpoint rectangles")
plt.plot(xs, f(xs), "k", lw=2, label="f(x) = x²")
plt.legend(); plt.title("Riemann sum for ∫₀² x² dx")
print("midpoint sum with 8 rectangles:", float(np.sum(f(mids)) * dx))midpoint sum with 8 rectangles: 2.65625
Signed area and accumulation
When is negative, its rectangles count as negative, so the integral is a signed area: , because the part below the axis cancels the part above. For a velocity, that is the difference between displacement (signed, can cancel) and total distance travelled (the integral of ).
| Integrand | Integral means |
|---|---|
| speed | distance travelled |
| velocity | change in position (displacement) |
| power (watts) | energy used (joules) |
| flow rate (litres/s) | volume |
| probability density | probability |
In software: every frame is a rectangle
A game loop that updates position += velocity * dt each frame adds one thin rectangle, , per frame. Over many frames it computes a left Riemann sum of the velocity: the integral of velocity, which is the change in position. As the sum approaches the exact integral, which is why smaller time steps give more accurate physics (see Introduction to Limits and Game Physics Integration).
The same accumulation appears in data code: a running total of bytes per second gives total bytes, a running total of a probability density gives a cumulative distribution, and numpy.trapezoid or scipy.integrate.quad compute integrals numerically when no formula is available (Numerical Integration).
To go further: Riemann Sums, The Definite Integral, and in depth Areas and Distances and The Fundamental Theorem of Calculus.
Exercises
Exercise 1: A rectangle sum by hand
Estimate with 4 right-endpoint rectangles. What is the exact value?
Answer
Estimate 10; exact 8.
Solution
Exercise 2: Distance from a speed table
A runner's speed in m/s, read every 10 s, is 3, 4, 4, 5, 6, 5 (at ). Estimate the distance covered in the first 50 s using left endpoints.
Answer
220 m
Solution
Exercise 3: Code the midpoint rule
Use a midpoint Riemann sum with to estimate . Guess the exact value from your result.
Answer
About 2.0000008; the exact value is 2.
Solution
import math
n = 1000
dx = math.pi / n
total = sum(math.sin((i + 0.5) * dx) for i in range(n)) * dx
print(f"{total:.7f}")2.0000008
Exercise 4: Signed area
What is , and why? What is the total (unsigned) area between and the -axis on that interval?
Answer
The integral is 0; the unsigned area is .
Solution
Test yourself
What does the definite integral of a velocity function over a time interval give?
- The acceleration
- The average velocity
- The displacement (change in position)
- The maximum speed
is increasing on . How does a left Riemann sum compare with the exact integral?
- It is too small
- It is too big
- It is exact
- It depends on only
A game updates position += velocity * dt every frame. Mathematically it is computing…
- a derivative
- a Riemann sum approximating an integral
- a limit at infinity
- a geometric series