Tuesday, April 7, 2026

Numerical integration - Trapezoidal vs Simpson - Reversing the wheel of learning - High end computer science is plain Maths

1. The Core Idea

We want to approximate:

abf(x)dx\int_a^b f(x)\,dx

by sampling the function at discrete points.

2. Trapezoidal Rule

Instead of rectangles, we use trapezoids.

abf(x)dxh2[f(x0)+2i=1n1f(xi)+f(xn)]

Key properties:

  • Error ~ O(h²)
  • Assumes linear variation between points

👉 Think: “connect the dots with straight lines”

3. Simpson’s Rule

Now we approximate using parabolas (quadratic fit).

abf(x)dxh3[f(x0)+4oddf(xi)+2evenf(xi)+f(xn)]\int_a^b f(x)\,dx \approx \frac{h}{3}\left[f(x_0) + 4\sum_{\text{odd}} f(x_i) + 2\sum_{\text{even}} f(x_i) + f(x_n)\right]

Key properties:

  • Error ~ O(h⁴) (much faster convergence)
  • Requires even number of intervals

Think: “fit smooth curves instead of straight lines”

4. The Real Question: Which is Better?

MethodAccuracyCostWhen it works best
TrapezoidalMediumLowRough/oscillatory data
SimpsonHighMediumSmooth functions

5. A Perfect Test Function

Use something smooth but non-trivial:

f(x)=sin(x),0πsin(x)dx=2f(x) = \sin(x), \quad \int_0^\pi \sin(x)\,dx = 2

No comments: