Numerical Integration
Approximate definite integrals using quadrature rules — visualize rectangles, trapezoids, and parabolas, compare convergence rates, and compute subdivisions needed for a given tolerance.
The Numerical Integration Problem
Given a function \(f\) on \([a,b]\), numerical quadrature approximates the definite integral \(\int_a^b f(x)\,dx\) using a weighted sum of function values. Every quadrature rule has the form:
where \(x_i\) are the nodes (sample points) and \(w_i\) are the weights. The choice of nodes and weights determines the degree of exactness — the highest-degree polynomial that the rule integrates exactly.
Newton–Cotes Rules (Closed)
These rules use equispaced nodes including the endpoints \(a\) and \(b\).
- Left Riemann Sum: \(\displaystyle\int_a^b f(x)\,dx \approx h\sum_{i=0}^{n-1}f(x_i)\). Error: \(O(h)\). Exact for constants.
- Right Riemann Sum: \(\displaystyle\int_a^b f(x)\,dx \approx h\sum_{i=1}^{n}f(x_i)\). Error: \(O(h)\). Exact for constants.
- Midpoint Rule: \(\displaystyle\int_a^b f(x)\,dx \approx h\sum_{i=0}^{n-1}f\!\left(\dfrac{x_i+x_{i+1}}{2}\right)\). Error: \(O(h^2)\). Exact for linears. This is an open rule (does not use endpoints).
- Trapezoidal Rule: \(\displaystyle\int_a^b f(x)\,dx \approx \dfrac{h}{2}\left[f(a)+2\sum_{i=1}^{n-1}f(x_i)+f(b)\right]\). Error: \(O(h^2)\). Exact for linears.
- Simpson’s Rule: \(\displaystyle\int_a^b f(x)\,dx \approx \dfrac{h}{3}\left[f(x_0)+4f(x_1)+2f(x_2)+4f(x_3)+\cdots+f(x_{2m})\right]\). Requires an even number of subintervals \(n=2m\). Error: \(O(h^4)\). Exact for cubics.
Gauss–Legendre Quadrature
Rather than using equispaced nodes, Gaussian quadrature chooses nodes as the roots of Legendre polynomials on \([-1,1]\), with optimally-computed weights. An \(m\)-point Gauss rule is exact for all polynomials of degree \(\le 2m-1\) — the highest possible degree of exactness for \(m\) nodes.
For composite Gauss–Legendre, the interval \([a,b]\) is divided into \(n\) subintervals and the \(m\)-point rule is applied on each, giving \(nm\) total function evaluations with error \(O(h^{2m})\).
Error Estimates
For the composite rules on \([a,b]\) with step size \(h=(b-a)/n\):
- Left / Right: \(|E| \le \dfrac{(b-a)^2}{2n}\,\max|f'|\) — \(O(h)\)
- Midpoint: \(|E| \le \dfrac{(b-a)^3}{24n^2}\,\max|f''|\) — \(O(h^2)\)
- Trapezoidal: \(|E| \le \dfrac{(b-a)^3}{12n^2}\,\max|f''|\) — \(O(h^2)\)
- Simpson: \(|E| \le \dfrac{(b-a)^5}{180n^4}\,\max|f^{(4)}|\) — \(O(h^4)\)
This Tool
- Select a quadrature rule from the eight buttons above.
- Choose a built-in example or enter a custom integrand, interval, and number of subintervals.
- Click Compute to see the quadrature formula, the numerical approximation, exact value (when known), absolute error, and the integration graph.
- Convergence plot: A log-log plot of absolute error vs. number of subintervals, confirming the theoretical convergence rate.
- Tolerance calculator: Enter a desired accuracy \(\varepsilon\) and the tool finds the smallest number of subintervals \(n\) for which the measured error \(|Q_n - I|\) drops below \(\varepsilon\), via doubling-then-bisection.