Calculus & Analysis

Numerical Integration

Visualizing Riemann sums, Trapezoidal, and Simpson's rules to approximate definite integrals.

By Riano · Published · Updated · Report a correction

Numerical integration: agreement is not the same as accuracy

Two methods can give the same answer and still miss an integral. Use the quadratic f(x) = x2 + 1 on [−2, 2] to calculate five approximations, compare them with an antiderivative, and explain the direction of the error. These inputs are available in the actual Riano module.

Choose a rule and a sampling location

Divide [a, b] into N equal subintervals of width Δx = (b − a)/N. Left and right Riemann sums use the corresponding endpoint height for each rectangle. The midpoint rule samples halfway across each interval. The trapezoidal rule joins adjacent endpoint heights with a straight line.

Composite Simpson's rule fits quadratic pieces over pairs of equally spaced intervals. Its endpoint weights are 1, the odd-index interior weights are 4, and the even-index interior weights are 2; multiply their sum by Δx/3. This version requires an even number of intervals.

Worked example: five rules with four intervals

For f(x) = x2 + 1 on [−2, 2], N = 4 gives Δx = 1. The endpoint heights at −2, −1, 0, 1 and 2 are 5, 2, 1, 2 and 5.

  1. The left sum is 5 + 2 + 1 + 2 = 10. The right sum is 2 + 1 + 2 + 5 = 10.
  2. The midpoint heights at −1.5, −0.5, 0.5 and 1.5 are 3.25, 1.25, 1.25 and 3.25. Their sum is 9.
  3. The trapezoidal sum is 5/2 + 2 + 1 + 2 + 5/2 = 10. Simpson's sum is (5 + 4×2 + 2×1 + 4×2 + 5)/3 = 28/3.
  4. An independent antiderivative is x3/3 + x. Evaluating it at the endpoints gives 28/3, approximately 9.3333.
Expected readings, rounded to four decimal places
MethodN = 4N = 8
Left or right sum10.00009.5000
Midpoint9.00009.2500
Trapezoidal10.00009.5000
Simpson's9.33339.3333

The left and right sums agree because the endpoint heights match, not because either rule is exact. The quadratic is convex: trapezoids sit above its curve, while midpoint rectangles underestimate its integral. Simpson's rule is exact for this quadratic in exact arithmetic.

Compare the native methods

Open Numerical Integration. The Function selector supplies fixed functions and intervals; it is not a general-purpose expression editor.

  1. Select “f(x) = x^2 + 1” and set Intervals (N) to 4. Compare Left Riemann Sum and Right Riemann Sum with the value 10.0000.
  2. Select Midpoint Rule, then Trapezoidal Rule. Check 9.0000 and 10.0000 against the independent exact value 9.3333.
  3. Select Simpson's Rule and keep the displayed N at 4. Compare the result and reported absolute error, then repeat with N = 8.
  4. Return to Midpoint Rule and compare N = 4 with N = 8. Its error falls from 1/3 to 1/12, a factor of four for this example.

The interface uses an even interval count for Simpson's rule. If you switch methods after choosing an odd N, inspect the effective N printed beside the slider before comparing results.

An error display is not a universal error bound

Here, “Exact” is available because the supplied functions have coded antiderivatives. The displayed error is the difference from that reference, not an automatic guarantee for an arbitrary unknown function. A rounded error of 0.0000 need not mean zero error in exact arithmetic.

Accuracy depends on smoothness and sampling. A narrow peak or rapid oscillation can fall between sample points. Familiar error orders for midpoint, trapezoidal and Simpson's rules require derivative bounds; they do not apply unchanged across discontinuities or singularities. The module does not implement adaptive subdivision or accept improper integrals.

Explain the result before refining it

Why does doubling N reduce the midpoint error by a factor of four in the table? For this particular quadratic, the exact difference is 16/(3N2). Substituting N = 4 and N = 8 gives the two measured errors. This calculation explains the pattern without treating every function as a quadratic.

Source for the quadrature rules

OpenStax Calculus Volume 2, Numerical Integration explains composite midpoint, trapezoidal and Simpson's rules and their smoothness assumptions. The quadratic table above is independently calculated for Riano's implemented inputs.

Experience it interactively

Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Numerical Integration module.

Try Numerical Integration on Riano →

More in Calculus & Analysis