Integrals & Numerical Integration

Calculus & Optimization

Chapter 9 · Integrals & Numerical Integration

Chapters 3-8 built one half of calculus: given a function, find its rate of change. This chapter builds the other half — given a rate of change, find the accumulated total — and lands on the specific technique real physics engines and simulations run every single frame.

The Integral as Accumulated Area

The Fundamental Theorem of Calculus
∫ f(x) dx from a to b is the signed area between f's curve and the x-axis. If F is an antiderivative of f (meaning F'(x)=f(x)), then that area equals F(b)−F(a) — integration and differentiation are inverse operations.
Verified directly — the antiderivative matches numerical area exactly
∫x² dx from 0 to 3: the antiderivative F(x)=x³/3 gives F(3)−F(0)=9. A direct numerical approximation of the actual area (midpoint rule, n=1000 thin rectangles) gives 8.999998 — matching to within 0.000002.

Numerical Integration: Four Methods, Compared

Real functions in code (or genuinely unknown functions from sensor data) don't come with a symbolic antiderivative. Numerical integration approximates the area directly, by summing many small pieces.

MethodIdea
Left Riemann sumRectangles, height from the left edge of each interval
Right Riemann sumRectangles, height from the right edge
Midpoint ruleRectangles, height from the interval's own midpoint
Trapezoidal ruleTrapezoids — the average of left and right heights
Verified directly — convergence rates genuinely differ
Approximating ∫x²dx from 0 to 3 (true value 9): at n=100 rectangles, left/right Riemann error is ≈0.135, while midpoint and trapezoidal error is ≈0.0002−0.0005 — roughly 270-600× more accurate at the identical n. Left/right error shrinks linearly with 1/n; midpoint and trapezoidal error shrinks with 1/n² — the exact same forward-vs-central-difference pattern Chapter 4 found for derivatives, now showing up in integration.

Euler's Method: Numerical Integration for Physics Simulation

If acceleration is known, integrating once gives velocity; integrating again gives position. A physics engine does this numerically, one small time step dt at a time — Euler's method: v_new = v + a·dt, x_new = x + v·dt.

Verified directly against the exact analytical solution
Simulating a falling object (x0=100, v0=0, a=−9.8 m/s²) with dt=0.01, for 300 steps (t=3s): Euler's method gives x=56.047, v=−29.400. The exact formula, x(t)=x0+v0t+½at²: x=55.900, v=−29.400. Velocity matches exactly; position is off by 0.147 — a small, real, honest numerical error, not a bug.
Verified directly — the error is genuinely first-order
Position error at t=3: dt=0.1 → error=1.470, dt=0.01 → error=0.147, dt=0.001 → error=0.0147, dt=0.0001 → error=0.00147 — the error shrinks by exactly a factor of 10 every time dt does. Euler's method is only first-order accurate, the physics-simulation equivalent of Chapter 4's own forward difference — real game engines use smaller time steps or more sophisticated integrators (Runge-Kutta methods, out of this course's own scope) specifically to control this exact error.

Numerical Integration in Code

def midpoint_rule(f, a, b, n): dx = (b - a) / n return sum(f(a + (i+0.5)*dx) for i in range(n)) * dx def euler_step(x, v, a, dt): return x + v*dt, v + a*dt # position uses the OLD velocity # simulate a falling object x, v, a, dt = 100, 0, -9.8, 0.01 for _ in range(300): x, v = euler_step(x, v, a, dt) print(x, v) # 56.047, -29.400 -- close to the exact 55.900, -29.400

Hands-On Exercises

Exercise 1

Using the Fundamental Theorem of Calculus, find ∫x³ dx from 0 to 2 using the antiderivative F(x)=x⁴/4. Then approximate the same area using the trapezoidal rule with n=4 intervals, and compare.

📄 View solution
Exercise 2

Using Euler's method, simulate one second of a ball thrown straight up with v0=15 m/s, x0=0, a=−9.8 m/s², using dt=0.5 (2 steps). Show each step's position and velocity, then compare your final position to the exact formula x(t)=x0+v0t+½at² at t=1.

📄 View solution
Exercise 3

A colleague says "Euler's method computed velocity exactly right in this chapter's own falling-object example, so it must be a perfectly accurate method for physics simulation." Using this chapter's own verified findings, explain what's wrong with generalizing from the velocity result to the method as a whole.

📄 View solution

Chapter 9 Quick Reference

  • Integral: accumulated (signed) area under a curve; the Fundamental Theorem connects it directly to antiderivatives, F(b)−F(a)
  • Verified: the antiderivative and a fine-grained numerical approximation agree to within 0.000002
  • Midpoint and trapezoidal rules converge with 1/n²; left/right Riemann sums only converge with 1/n — verified directly, mirroring Chapter 4's central-vs-forward-difference finding
  • Euler's method: v_new=v+a·dt, x_new=x+v·dt — the numerical integration real physics engines run every frame
  • Verified against an exact analytical solution: velocity exact, position off by a small, genuinely first-order error that shrinks linearly with dt
  • Next chapter: Capstone — optimizing a function from scratch