Numerical Integration

Unbloated Reveal.js Example

The Midpoint Rule

Approximate an integral by sampling the midpoint of each subinterval.

Mn(f)=h∑k=0n−1f(a+(k+12)h),h=b−an. M_n(f) = h \sum_{k=0}^{n-1} f\left(a + \left(k+\frac{1}{2}\right)h\right), \qquad h = \frac{b-a}{n}.

This is the same example used in the Unbloated technical book.

Reference Implementation

def midpoint(f, a, b, n):
    if n < 1:
        raise ValueError("n must be positive")
    h = (b - a) / n
    return h * sum(f(a + (k + 0.5) * h) for k in range(n))

Checking the Result

For the integral of x2x^2 on [0,1][0,1]:

nn Estimate Absolute error
1 0.250000 0.083333
2 0.312500 0.020833
4 0.328125 0.005208

Note

These values check one integrand. They do not establish an error bound for arbitrary continuous functions.

Continue Reading

Use the arrow keys or the slide controls to move through this presentation.