Lesson 10 of 18

Midpoint Rule

Midpoint Rule

Use the midpoint of each subinterval:

i=0n1f ⁣(a+(i+0.5)h)h\sum_{i=0}^{n-1} f\!\left(a + \left(i + 0.5\right) h\right) \cdot h

a   a+h  a+2h        b
|----|----|----...----|
 ^    ^    ^
 midpoints used

Why Midpoints Are Better

The midpoint rule has O(h2)O(h^2) error — the same order as the trapezoidal rule, and better than left/right (O(h)O(h)). Intuitively, the midpoint is the "best representative" of a subinterval because it cancels out the linear error terms from both sides.

Comparison for 01x2dx=13\int_0^1 x^2\, dx = \frac{1}{3}

nnLeftMidpointTrapezoidRight
40.2190.3280.3440.469

The midpoint rule is more accurate than both left and right for the same nn.

Exact for Linear Functions

The midpoint rule integrates linear functions exactly, regardless of nn. For f(x)=2x+1f(x) = 2x + 1 on [0,3][0, 3]:

  • 03(2x+1)dx=[x2+x]03=12\int_0^3 (2x+1)\, dx = \left[x^2+x\right]_0^3 = 12
  • Midpoint rule with any nn: f(a+(i+0.5)h)h=12\sum f(a + (i+0.5)h) \cdot h = 12

Your Task

Implement double riemann_mid(double (*f)(double), double a, double b, int n).

TCC compiler loading...
Loading...
Click "Run" to execute your code.