Numerical Integration Calculator
Approximate definite integrals using numerical methods like Simpson's rule.
Use Math.sin, Math.cos, Math.exp, Math.log (ln), Math.sqrt, Math.abs. ^ is power. Math.PI and Math.E are constants.
Even n for Simpson's rule.
About
Numerical integration — also called numerical quadrature — is the family of algorithms that approximate the definite integral ∫ₐᵇ f(x) dx when an analytical antiderivative is unavailable, too complex, or when the integrand is given only as a table of values. The integral represents the signed area between the curve y = f(x) and the x-axis from x = a to x = b, and numerical methods approximate that area by summing contributions from many small subintervals.
The trapezoidal rule divides [a, b] into n equal subintervals of width h = (b − a)/n and approximates the area on each subinterval with a trapezoid — that is, with the average of the function's values at the two endpoints multiplied by the width. The composite formula is T = h × [(f(a) + f(b))/2 + Σ_(i=1)^(n−1) f(a + i h)]. The trapezoidal rule is exact for linear functions (degree 1) and has error O(h²) for smooth functions. It is the simplest robust method and is widely used for tabulated data and quick estimates.
Simpson's rule fits a quadratic (parabola) through each triplet of consecutive sample points and integrates the parabola analytically. Composite Simpson's rule on n even subintervals is S = (h/3) × [f(x_0) + 4 f(x_1) + 2 f(x_2) + 4 f(x_3) + ... + 2 f(x_(n−2)) + 4 f(x_(n−1)) + f(x_n)] with the alternating 4-2-4-2 pattern. Simpson's rule is exact for polynomials up to degree 3 (one degree higher than the underlying quadratic fit suggests) and has error O(h⁴), making it about 100 times more accurate than the trapezoidal rule on the same number of subintervals for smooth functions.
Error and convergence are central concerns. The trapezoidal rule's error scales as O(h²) = O(1/n²); Simpson's scales as O(h⁴) = O(1/n⁴). Doubling n cuts the trapezoidal error by a factor of 4 and the Simpson error by a factor of 16. A practical error indicator is the difference between the trapezoidal and Simpson estimates: if they agree to k significant figures, the Simpson estimate is reliable to roughly k + 2 figures, because Simpson's error is two powers of h smaller.
All standard quadrature rules derive from polynomial interpolation. The trapezoidal rule fits linear (degree 1) polynomials; Simpson's rule fits quadratics; higher-order Newton-Cotes rules use cubics, quartics, and beyond. Gaussian quadrature places the sample points non-uniformly to maximise accuracy for a given number of points — achieving degree 2n−1 exactness with only n evaluations. Adaptive methods (recursive Simpson with subdivision at points where the error estimate is large) form the basis of production integrators in MATLAB's quad, SciPy's quad, and Numerical Recipes' qromb.
How to Use
- 1Type the integrand f(x)Enter the function as a JavaScript expression in x. Allowed tokens: numbers, +, -, *, /, ^ (power), parentheses, Math.* functions (Math.sin, Math.cos, Math.exp, Math.log, Math.sqrt, Math.abs, etc.) and constants Math.PI, Math.E.
- 2Enter the lower and upper limits a and bType the bounds of integration. The integral is taken from x = a to x = b, with the sign convention that ∫ₐᵇ = −∫ᵦₐ.
- 3Enter the number of subintervals nChoose n ≥ 2 (and even for Simpson's rule). Larger n gives smaller error but takes more steps. The default 100 is fine for most smooth functions; double it if you need more precision.
- 4Read the trapezoidal and Simpson's rule estimatesThe hero block shows Simpson's rule result (more accurate). Below it you can read the trapezoidal estimate, the difference between the two methods (a practical error indicator), the step size h, and the number of function evaluations.
- 5Sanity-check with the comparison tableThe comparison table at the bottom shows the exact integral alongside the trapezoidal and Simpson's estimates for several common integrals (∫ x², ∫ sin x, ∫ eˣ, ∫ 1/(1+x²)) so you can verify the calculator against known results.
Formula
Composite trapezoidal rule:
T = h × [(f(a) + f(b)) / 2 + Σ_(i=1)^(n−1) f(a + i h)]
h = (b − a) / n error = O(h²)
Composite Simpson's rule (n even):
S = (h/3) × [f(x_0) + 4 f(x_1) + 2 f(x_2) + 4 f(x_3) + ... + 4 f(x_(n−1)) + f(x_n)]
h = (b − a) / n error = O(h⁴)
Practical error indicator:
|S − T| is an empirical bound on the trapezoidal error.
Simpson's rule is roughly 100× more accurate than T for smooth f.
Fundamental theorem of calculus:
∫ₐᵇ f(x) dx = F(b) − F(a), where F′ = f
Worked Example
Example 1 — ∫₀^π sin x dx (exact = 2):
- With n = 10, h = π/10 ≈ 0.31416.
- Trapezoidal: T ≈ 2.0082481 (about 0.4% error).
- Simpson: S ≈ 2.0000067 (about 0.0003% error).
- Simpson is ~100× more accurate than the trapezoidal on the same n.
Example 2 — ∫₀¹ x² dx (exact = 1/3):
- With n = 4, h = 0.25. Trapezoidal: T = 0.343750 (≈ 3.1% error).
- Simpson: S = 0.333333 (exact, since Simpson is exact for polynomials up to degree 3).
Example 3 — ∫₀¹ eˣ dx (exact = e − 1 ≈ 1.7182818):
- With n = 100, h = 0.01. Trapezoidal: T ≈ 1.7182961 (5-digit accuracy).
- Simpson: S ≈ 1.7182818 (8-digit accuracy — Simpson's error of O(10⁻⁸) matches the expected O(h⁴) with h = 0.01).
Tips
- Always start with a modest n (say 100) and double it. If the result changes by less than your tolerance, you have converged. For Simpson's rule with smooth functions, n = 100 is usually enough for 6 significant digits.
- Compare the trapezoidal and Simpson estimates. If they agree to k significant figures, the Simpson estimate is reliable to roughly k + 2 figures because its error is two powers of h smaller.
- For non-smooth integrands (sharp corners, jumps, infinite values), subdivide the integral at the discontinuities and integrate each smooth piece separately. Then add the results. This is called "adaptive subdivision."
- For periodic integrands over a full period, the trapezoidal rule is exponentially convergent — far better than its O(h²) worst-case. This surprising fact underlies the spectral accuracy of the fast Fourier transform.
- If your integrand has a singularity at one endpoint (e.g. 1/√x at x = 0), apply a change of variable to remove the singularity before applying Simpson's rule. For 1/√x, substitute x = t² to get 2∫ f(t²) dt, which is smooth at t = 0.
Common Mistakes
- Using an odd n with Simpson's rule. The composite Simpson formula requires an even number of subintervals; the calculator rounds up to the nearest even n if needed, but it is best to enter an even n directly.
- Forgetting that the trapezoidal and Simpson rules assume a smooth integrand. Sharp corners, jumps, or infinite values invalidate the polynomial interpolation that underlies both rules; subdivide at discontinuities or singularities.
- Trusting the Simpson estimate without comparing it to the trapezoidal estimate. The difference |S − T| is the practical error indicator; if the two methods disagree, double n and try again.
- Mishandling singular integrands at the endpoints. ∫₀¹ 1/√x dx = 2 but applying Simpson's rule directly will fail because f(0) is infinite. Substitute x = t² to get 2∫₀¹ dt, which is smooth and trivial.
Frequently Asked Questions
- What is numerical integration?
- Numerical integration (or quadrature) is the use of numerical algorithms to approximate the definite integral of a function when an analytical antiderivative is unavailable, too complex, or when the integrand is given only as a table of values. The integral ∫ₐᵇ f(x) dx represents the signed area between the curve y = f(x) and the x-axis from x = a to x = b. Numerical methods approximate that area by summing contributions from many small subintervals, using polynomial interpolation between sample points.
- What is the trapezoidal rule?
- The trapezoidal rule approximates the integral by dividing [a, b] into n equal subintervals of width h = (b − a)/n and approximating the area under the curve on each subinterval with a trapezoid. The formula is T = h × [(f(a) + f(b))/2 + Σ_{i=1}^{n−1} f(x_i)], where x_i = a + i×h. The trapezoidal rule is exact for linear functions (degree 1) and has error O(h²) for smooth functions. It is the simplest robust method and is widely used for tabulated data.
- What is Simpson's rule?
- Simpson's rule approximates the integral by fitting a quadratic (parabola) through three consecutive sample points and integrating the parabola analytically. Composite Simpson's rule on n equal subintervals (n must be even) gives S = (h/3) × [f(x_0) + 4 f(x_1) + 2 f(x_2) + 4 f(x_3) + ... + 2 f(x_{n−2}) + 4 f(x_{n−1}) + f(x_n)], where h = (b − a)/n. Simpson's rule is exact for polynomials up to degree 3 and has error O(h⁴), making it about 100× more accurate than the trapezoidal rule on the same number of subintervals for smooth functions.
- When is Simpson's rule better than the trapezoidal rule?
- Simpson's rule is almost always better when the integrand is smooth (infinitely differentiable) and the number of subintervals n is even, because its error scales as O(h⁴) versus O(h²) for the trapezoid. The exception is when the integrand has sharp corners, jumps, or other non-smooth features — then the trapezoidal rule can be more reliable, or a higher-order method may fail entirely. For tabulated data with irregular spacing, the trapezoidal rule is the only practical choice.
- How do I choose the number of subintervals n?
- Start with a modest n (say 100) and double it; if the result changes by less than your tolerance, you have converged. For Simpson's rule with smooth functions, n = 100 to 1000 is usually enough for 6 significant digits. The error decreases as O(1/n⁴) for Simpson and O(1/n²) for trapezoid, so doubling n cuts the Simpson error by a factor of 16 and the trapezoid error by a factor of 4. For non-smooth integrands, n may need to be much larger; consider an adaptive method.
- How are numerical integration formulas derived?
- All standard quadrature rules derive from polynomial interpolation. The trapezoidal rule fits a linear (degree 1) polynomial through each pair of consecutive sample points and integrates the linear polynomial analytically. Simpson's rule fits a quadratic (degree 2) polynomial through each triplet of consecutive points and integrates the quadratic. Higher-order Newton-Cotes rules use cubics, quartics, and higher; Gaussian quadrature places the sample points non-uniformly to maximise accuracy for a given number of points. The error analysis comes from Taylor expansion of the integrand.
Definite integral
The definite integral ∫ₐᵇ f(x) dx represents the signed area between the curve y = f(x) and the x-axis from x = a to x = b. By the fundamental theorem of calculus, if F(x) is an antiderivative of f (so F′ = f), then ∫ₐᵇ f(x) dx = F(b) − F(a). When no closed-form antiderivative exists — or when the integrand is given only as sampled data — numerical methods approximate the integral by summing contributions from many small subintervals.
Trapezoidal rule
The trapezoidal rule divides [a, b] into n equal subintervals of width h = (b − a)/n and approximates the area on each subinterval with a trapezoid. The composite formula is T = h × [(f(a) + f(b))/2 + Σ_(i=1)^(n−1) f(a + i h)], which averages the endpoint values on each subinterval. The rule is exact for linear functions and has error O(h²) for smooth functions. It is the simplest robust method and the right choice for tabulated data.
Simpson's rule
Simpson's rule fits a quadratic through each triplet of consecutive sample points and integrates the quadratic analytically. Composite Simpson's rule on n even subintervals is S = (h/3) × [f(x_0) + 4 f(x_1) + 2 f(x_2) + ... + 4 f(x_(n−1)) + f(x_n)] with the alternating 4-2-4-2 pattern. The rule is exact for polynomials up to degree 3 and has error O(h⁴), making it about 100× more accurate than the trapezoidal rule on the same number of subintervals for smooth functions.
Error and convergence
The error of the trapezoidal rule scales as O(h²) = O(1/n²); Simpson's rule scales as O(h⁴) = O(1/n⁴). Doubling n cuts the trapezoidal error by a factor of 4 and the Simpson error by a factor of 16. A practical error indicator is the difference between the trapezoidal and Simpson estimates: if they agree to k significant figures, the Simpson estimate is reliable to roughly k + 2 figures. For sharp-cornered or discontinuous integrands, both rules converge slowly; consider an adaptive method or a transformation.
| Integral | f(x) | a | b | Exact | Simpson (n=100) | Trapezoid (n=100) |
|---|---|---|---|---|---|---|
| ∫₀¹ x² dx | x**2 | 0 | 1 | 0.333333 | 0.333333 | 0.333350 |
| ∫₀^π sin x dx | Math.sin(x) | 0 | Math.PI | 2.000000 | 2.000000 | 2.000000 |
| ∫₀¹ eˣ dx | Math.exp(x) | 0 | 1 | 1.718282 | 1.718281 | 1.718861 |
| ∫₀¹ 1/(1+x²) dx | 1/(1+x**2) | 0 | 1 | 0.785398 | 0.785398 | 0.785399 |
| ∫₀¹ √x dx | Math.sqrt(x) | 0 | 1 | 0.666667 | 0.666667 | 0.666667 |
| ∫₀^π/² cos x dx | Math.cos(x) | 0 | Math.PI/2 | 1.000000 | 1.000000 | 1.000000 |
| ∫₁² ln x dx | Math.log(x) | 1 | 2 | 0.386294 | 0.386294 | 0.386323 |
Simpson's rule is exact for polynomials up to degree 3, even though it fits a quadratic through three points — a surprising bonus that comes from the symmetric placement of the sample points. This is why Simpson is the default choice for smooth integrands; only the trapezoidal rule should be used for non-smooth or tabulated data.
For periodic integrands over a full period, the trapezoidal rule is exponentially convergent — far better than its worst-case O(h²). This surprising fact underlies the spectral accuracy of the fast Fourier transform and is exploited by the Clenshaw-Curtis and tanh-sinh quadrature rules.
The trapezoidal and Simpson rules assume a smooth integrand. If the function has a sharp corner, a jump, or an infinite singularity at one endpoint, the polynomial interpolation that underlies both rules breaks down. Subdivide the integral at the discontinuity and integrate each smooth piece separately, or apply a change of variable to remove the singularity.
Authoritative References
- Wikipedia — Numerical integration— wikipedia.orgComprehensive overview of quadrature methods, including the trapezoidal rule, Simpson's rule, Gaussian quadrature, adaptive methods, and error analysis.
- Wikipedia — Simpson's rule— wikipedia.orgDetailed entry on Simpson's rule, including the composite form, error analysis, and the relationship to Newton-Cotes and Romberg methods.
- LibreTexts — Numerical Integration— libretexts.orgOpen-access textbook chapter covering the trapezoidal rule, Simpson's rule, and Gaussian quadrature, with worked examples and error estimates.
- NIST DLMF — Numerical Integration— nist.govNIST Digital Library of Mathematical Functions section on quadrature, with formal error bounds and references for advanced methods.
Try our other free calculators
CalcGenius has 1,200+ free, ad-free, browser-based calculators for finance, health, math, science, and everyday life. No sign-up required.