Lesson 12 of 18
Simpson's Rule
Simpson's Rule
Simpson's rule fits a parabola through each pair of subintervals (3 points: left, mid, right) instead of a line (trapezoid).
Formula
With subintervals ( must be even), :
Pattern of coefficients: 1, 4, 2, 4, 2, ..., 4, 1
Odd-indexed points get weight 4, even-indexed interior points get weight 2, endpoints get weight 1.
Why 1-4-2-4-1?
Each pair of subintervals uses the exact integral of the parabola through 3 points, which equals . Combining such pairs gives the pattern.
Accuracy
Error is — much better than trapezoid ():
Simpson's rule is exact for polynomials of degree .
Comparison for
| Trapezoid | Simpson's | |
|---|---|---|
| 2 | 0.375 | 0.3333 (exact!) |
| 4 | 0.344 | 0.3333 (exact!) |
Simpson's rule gives the exact answer for with just !
Your Task
Implement double simpson(double (*f)(double), double a, double b, int n). Assume is even.
TCC compiler loading...
Loading...
Click "Run" to execute your code.