› Riemann Sum Numerical Integration Visualizer Back
Mathematics / Calculus

Riemann Sum Numerical Integration Visualizer

Compare five numerical integration methods — left, right, midpoint, trapezoidal, and Simpson's rule — intuitively. Increase partition count n and observe how errors converge across three visual tabs.

Settings

Presets

Results
Numerical result
—
Exact value (analytic)
—
Absolute error
—
Relative error [%]
—
Cvrect

The blue rectangles (or trapezoids) show each subinterval approximation. The gap from the function curve is the error.

Error

Vertical axis (log): absolute error. Watch convergence as n increases; for the trapezoidal rule, error decreases roughly as 1/n².

Allmethods

Compare method values at the same n. Distance from the exact value (red line) shows the accuracy difference.

Theory & Key Formulas

Left/right Riemann sums: error $O(h)$, $h=\Delta x$

Midpoint rule: error $O(h^2)$

Trapezoidal rule: $\dfrac{h}{2}\bigl[f(x_0)+2\sum_{i=1}^{n-1}f(x_i)+f(x_n)\bigr]$, error $O(h^2)$

Simpson's rule: $\dfrac{h}{3}\bigl[f_0+4f_1+2f_2+\cdots+f_n\bigr]$, error $O(h^4)$

🙋 Is integration just 'cut it up and add it up'? Is it really that simple?

🙋
When I learned about definite integrals, I was taught 'add up the areas of tiny rectangles.' Is that really enough to calculate it? Does a computer do the same thing?
🎓
Basically, yes. But the accuracy changes depending on 'at which point you measure the height.' Left and right endpoints have an error of O(h) and converge slowly. The midpoint rule is much better at O(h²). The trapezoidal rule is also O(h²); it just looks like trapezoids instead of rectangles, but it's as good as the midpoint rule. Simpson's rule converges even faster at O(h⁴). If you look at the 'Error vs n' tab in this simulator, you can see the difference in convergence speed for each method at a glance on a log graph.
🙋
I've heard the name 'Simpson's rule,' but how is it different from the trapezoidal rule?
🎓
The trapezoidal rule connects each interval with a straight line (1st-order polynomial interpolation). Simpson's rule approximates with a parabola (2nd-order polynomial) passing through three points. Since a 2nd-order polynomial follows the curve better, the accuracy is higher—the error decreases inversely with n⁴, so doubling n reduces the error by a factor of 16. Try selecting Simpson's rule with the '∫₀^π sin(x)dx = 2' preset and compare the errors for n=10 and n=20; you should see roughly a 16-fold difference.
🙋
When I select the 'sin(x²)' function, the graph looks jagged. Does this have any special meaning?
🎓
sin(x²) is a function related to the 'Fresnel integral,' which appears in light diffraction calculations. Since there is no closed-form analytic solution, it must be computed numerically. It oscillates strongly and converges slowly—this is a typical example of a 'difficult function for numerical integration,' and in practice, adaptive numerical integration (adaptive Simpson's method) or variable transformation is required. In CAE, similar issues arise when integrating temperature-dependent material constants or nonlinear characteristics.
🙋
I've heard that the finite element method (FEM) also uses numerical integration. What kind of calculations is it used for?
🎓
Numerical integration is needed to compute element stiffness matrix components K_ij = ∫_Ω B_i^T D B_j dΩ. Since analytic integration is often impossible, a method called Gauss-Legendre integration is used to evaluate values at a finite number of points (integration points) within the element and take a weighted sum. Typically, for quadrilateral elements, 2×2=4 point integration is standard, and for hexahedral elements, 2×2×2=8 point integration is standard. In terms of accuracy assurance, the higher-order extension of Simpson's rule lies at the core of CAE.

Frequently Asked Questions

A definite integral is defined as the limit of a Riemann sum: ∫ₐᵇ f(x)dx = lim_{n→∞} Σ f(x_i*) Δx. Here, x_i* is any point within each subinterval. If f is continuous on [a,b], the limit converges to the same value regardless of which point is chosen. This is the condition for Riemann integrability.
Truncation error of the trapezoidal rule: |E_T| ≤ (b-a)³/(12n²) × max|f''|. Simpson's rule: |E_S| ≤ (b-a)⁵/(180n⁴) × max|f⁽⁴⁾|. Since Simpson's rule is inversely proportional to n⁴, doubling the number of partitions reduces the error to 1/16 (trapezoidal rule gives 1/4). However, this assumes f is a smooth function that is four times differentiable.
It is a quadrature method that uses optimal evaluation points and weights to maximize accuracy, rather than equally spaced points. An n-point Gauss quadrature can exactly integrate polynomials up to degree 2n-1. It is important for element integration in the finite element method and in university numerical analysis. The basic form is Gauss-Legendre quadrature on the interval [-1,1], which can be extended to any interval via variable transformation.
① Rapidly oscillating functions (sin(x²), Bessel functions): Fine partitions are needed due to high-frequency components. ② Functions with singularities at endpoints (e.g., 1/√x): Trapezoidal and Simpson's rules lose accuracy. ③ Wide-range integrals (from -∞ to ∞): Interval transformation is required. For these, adaptive quadrature methods (e.g., QUADPACK), variable transformation, and special acceleration techniques (Richardson extrapolation, Romberg's method) are used.
In the finite element method, the element stiffness matrix K_e = ∫_Ω B^T D B dΩ is evaluated by numerical integration. Gauss-Legendre quadrature is commonly adopted, with standard choices: 2-3 points in 1D, 2×2=4 points for 2D quadrilateral elements, and 2×2×2=8 points for 3D hexahedral elements. Too few integration points cause 'hourglass modes' (zero-energy modes), while too many cause 'shear locking' (over-stiffness), so selecting the appropriate integration rule is important.

How to Use

  1. Define the integration bounds: enter lower limit (a) and upper limit (b) in the respective fields
  2. Set the number of subintervals (n) using the slider; higher values increase computational accuracy but reduce visual clarity
  3. Select a method from the dropdown: Left Riemann, Right Riemann, Midpoint, Trapezoidal, or Simpson's Rule
  4. Input your function expression (e.g., x^2 + 3*x - 1) in the function field
  5. Click Compute to generate the visualization and numerical result

Worked Example

Integrate f(x) = x^2 from a=0 to b=4 with n=8 subintervals. Using Trapezoidal Rule: each subinterval width Δx = 0.5. The approximation yields ≈21.5 square units, compared to the exact value of 21.333 (analytical result: [x^3/3] from 0 to 4 = 64/3). Simpson's Rule with the same n=8 produces ≈21.334, demonstrating superior accuracy for polynomial integrands. Typical engineering application: calculating shear force diagrams in beam analysis where load distributions follow quadratic profiles.

Practical Notes