Numerical Integration, Explained

This guide shows you how to compute a definite integral when no clean antiderivative exists, how adaptive quadrature, Simpson's rule and the trapezoidal rule differ, and how to read the number the calculator returns with confidence.

What numerical integration does

A definite integral \int_a^b f(x)\,dx is the signed area between the curve y = f(x) and the x-axis from a to b. Sometimes you can find an antiderivative and evaluate it exactly. Often you cannot. The function exp(-x^2) is the classic case: it has no elementary antiderivative, so there is no formula to plug numbers into.

Numerical integration sidesteps the antiderivative entirely. It samples f at chosen points, fits simple shapes (straight lines, parabolas) to those samples, and adds up the areas of those shapes. With enough well-placed samples the sum lands very close to the true value.

As a hook, consider \int_0^1 e^{-x^2}\,dx. The true value to ten digits is 0.7468241328. Adaptive quadrature reproduces that to full machine precision in a fraction of a millisecond, without you knowing any special functions.

When to use it, and when not

Reach for numerical integration when the integrand has no elementary antiderivative, when the antiderivative is available but painful to write out, or when f comes from a formula you just want a number from. The bounds must be finite and f must be defined and finite across [a, b] (with mild exceptions handled by the adaptive method).

Do not use it when you actually need a symbolic answer. If you want the antiderivative as a formula, or an exact value like \frac{\pi}{4}, use the Derivative & Integral Calculator instead. If your integrand blows up inside the interval, has an infinite bound, or oscillates wildly, a plain fixed-grid rule can be badly wrong; the adaptive method copes better but still deserves a sanity check.

Numerical methods return a number even when that number is meaningless. If f has a vertical asymptote inside [a, b], for example 1/x on [-1, 1], the true integral does not converge, but a grid rule will happily add up finite samples and hand you a value. Always look at the plot.

The three rules and the intuition behind them

The trapezoidal rule splits [a, b] into n equal pieces of width h = (b-a)/n and joins consecutive sample points with straight lines. Each piece is a trapezoid.

\int_a^b f(x)\,dx \approx \frac{h}{2}\left[f(x_0) + 2\sum_{i=1}^{n-1} f(x_i) + f(x_n)\right]

Here x_i = a + i\,h are the grid points, and the interior samples are counted twice because each is shared by two trapezoids. The error shrinks like h^2: halve h and the error drops by about a factor of 4.

Simpson's rule fits a parabola through each pair of adjacent intervals, so it needs n even. Parabolas match curved functions much better than straight lines.

\int_a^b f(x)\,dx \approx \frac{h}{3}\left[f(x_0) + 4\sum_{\text{odd } i} f(x_i) + 2\sum_{\text{even } i} f(x_i) + f(x_n)\right]

The odd-indexed samples get weight 4, the even interior samples weight 2. Simpson's error shrinks like h^4: halve h and the error drops by about a factor of 16. That is why Simpson beats the trapezoid rule so decisively on smooth functions.

Adaptive quadrature does not use a fixed grid. It estimates the integral on a piece, estimates it again with more points, compares the two, and if they disagree by more than a tolerance it splits the piece and repeats. It spends samples where the function is hard and few where it is easy. This is the recommended choice: accurate, and it ignores your Subintervals value entirely.

Worked example: the demo integral

Integrating exp(-x^2) from 0 to 1

Load the demo: expr = exp(-x^2), a = 0, b = 1, method quad. The true value is 0.74682413. To see how the fixed rules approach it, follow the trapezoidal and Simpson steps with a coarse grid of n = 4, so h = 0.25.

  1. Sample f at the five grid points: f(0) = 1, f(0.25) = 0.939413, f(0.5) = 0.778801, f(0.75) = 0.569783, f(1) = 0.367879.
  2. Trapezoid: \frac{0.25}{2}\left[1 + 2(0.939413 + 0.778801 + 0.569783) + 0.367879\right] which gives 0.742984. The error is about 0.0038.
  3. Simpson: \frac{0.25}{3}\left[1 + 4(0.939413 + 0.569783) + 2(0.778801) + 0.367879\right] which gives 0.746855. The error is about 0.000031, roughly 120 times smaller than the trapezoid error at the same 5 samples.
  4. Switch the method back to quad. Adaptive quadrature returns 0.7468241328, correct to every digit shown.

The chart below shows the integrand with the area from 0 to 1 shaded. That shaded region is exactly what every method is trying to measure.

The curve falls from 1 at x = 0 toward 0. The shaded strip from x = 0 to x = 1 has area 0.746824.

Watch convergence as the grid refines

The single most instructive thing to feel is how fast Simpson's rule closes in compared to the trapezoid rule. The widget lets you raise n and watch both errors shrink at their different rates.

For \int_0^1 e^{-x^2}\,dx with true value 0.746824, the trapezoid error at n = 2 is about 0.01526, at n = 4 about 0.00384, at n = 8 about 0.00096: each doubling of n cuts it near fourfold. Simpson's error at n = 2 is about 0.000287, at n = 4 about 0.0000305, at n = 8 about 0.00000193: each doubling cuts it near sixteenfold.

Reading the result

The calculator returns a single number: the signed area. Signed matters. Where f is negative the contribution is negative, so an integral can be zero even when the function is far from zero. For example \int_0^{2\pi} \sin x\,dx = 0 because the area above the axis cancels the area below.

Compare methods to gauge trust. If adaptive quadrature, Simpson at n = 1000 and trapezoid at n = 1000 all agree to six digits, the answer is solid. If they disagree, the integrand is doing something awkward: a sharp spike, a near-singularity, or rapid oscillation. In that case increase n, or better, use the adaptive method and inspect the plot.

Quadrature
Any rule that approximates an integral by a weighted sum of function values.
Order of accuracy
The power of h that the error scales with. Trapezoid is order 2, Simpson is order 4.
Tolerance
The maximum error the adaptive method will accept before it stops splitting intervals.

Common mistakes

Reversed bounds flip the sign. If you enter a = 1 and b = 0, the result is the negative of the 0 to 1 integral, so -0.746824 instead of 0.746824. That is the correct mathematical convention, not a bug.

Odd n with Simpson's rule is a setup error, because Simpson pairs intervals. The calculator handles this for you, but if you compute by hand, use an even count.

Trusting a huge n blindly wastes effort. Going from n = 1000 to n = 100000 on a smooth function changes nothing you can see, and near a singularity it does not fix the underlying problem. More points cannot rescue an integral that does not converge.

Finally, check your expression syntax. Write exp(-x^2), not e^-x^2, and use * for multiplication where needed, as in 2*x.

Related tools

To see the exact antiderivative or an exact definite integral, use the Derivative & Integral Calculator. To watch left, right and midpoint sums build up toward the true area, the Riemann Sum Calculator is the companion to this page. If your integrand is a series expansion, the Taylor & Maclaurin Series tool gives the coefficients, and the Function Analyzer & Grapher helps you spot the zeros and asymptotes that decide whether an integral is safe to compute.

Frequently asked questions

Why does exp(-x^2) not have an antiderivative I can write down?

Its antiderivative is the error function, \frac{\sqrt{\pi}}{2}\,\text{erf}(x), which is not built from powers, exponentials, logs and trig functions. So there is no elementary formula, and numerical methods are the practical route to a number.

Which method should I pick?

Use adaptive quadrature unless you have a reason not to. It is accurate on smooth functions and tolerant of mild trouble. Pick Simpson or trapezoid only when you want to study how a fixed grid behaves.

How large should n be for the fixed rules?

For a smooth function, Simpson at n = 100 is usually accurate to eight or more digits. The default n = 1000 is more than enough for almost any well-behaved integrand. Doubling beyond that rarely changes a visible digit.

Can I integrate to infinity?

Not directly here; the bounds must be finite. For an integral like \int_0^\infty e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2} \approx 0.886227, you can approximate by choosing a large finite upper bound such as b = 6, where the tail contributes less than 10^{-15}.

Why is my answer negative when the graph looks positive?

Check whether you entered the bounds in reverse. If a > b, the sign flips. Also confirm the function is truly positive across the whole interval; a dip below the axis subtracts area.