Numerical integration

Estimating ∫ab f(x) dx with n subintervals
try
scroll or pinch to zoom · drag to pan
4
ruleestimateerror

error versus n (log–log)

what is this?

Each rule cuts [a, b] into n subintervals of width Δx = (b − a)/n, replaces f on each by something whose area is easy, and adds up. Left, right and midpoint use rectangles whose height is f at the left end, right end or middle. The trapezoid rule joins the two endpoint values by a straight line, so Tn = (Ln + Rn)/2. Simpson’s rule takes the subintervals two at a time and fits a parabola through the three points, so it needs n even; it is also the weighted average Sn = (2Mn/2 + Tn/2)/3, which is how Active Calculus §5.6 writes it.

Shaded regions are what each rule counts, positive above the axis and negative below. The table compares all five rules at the current n; click a row to switch rules. The “error” is estimate minus a reference value computed by adaptive quadrature to about twelve digits, so positive means over-estimate. For a concave-up function the trapezoid rule over-estimates and the midpoint rule under-estimates, and the midpoint error is about half the trapezoid error with the opposite sign.

The lower plot shows |error| against n on log–log axes for n = 2, 4, 8, …, 256. A rule whose error shrinks like 1/nk makes a line of slope −k: about −1 for left and right, −2 for midpoint and trapezoid, −4 for Simpson, for smooth f. Errors below 10−14 are at the limit of the arithmetic and drawn on the floor; Simpson is exact for every cubic, so try x³ − 3x.

Functions: type x, numbers, + − * / ^, parentheses, implicit multiplication (2x, 3sin(x)), pi and e, and sqrt, cbrt, exp, ln (or log), log10, sin, cos, tan, sec, csc, cot, asin, acos, atan, sinh, cosh, tanh, abs. The endpoints accept expressions too (pi/2). Hover over the picture to see one subinterval’s contribution. Scroll (or pinch) to zoom about the cursor, with Shift to zoom only horizontally and Alt only vertically; drag to pan; double-click, press 0, or use reset view to fit [a, b] again. Changing f, a or b also resets the view. The address bar keeps the current settings, so a link reopens the same picture.