Gaussian Quadrature Calculator

Evaluate integrals with Gauss-Legendre nodes and weights, 2 to 5 points, error included.

Gaussian Quadrature Calculator

What Is Gaussian Quadrature?

Gaussian quadrature approximates a definite integral as a weighted sum of function values at specially chosen points:

ab f(x) dx ≈ (b−a)/2 · Σ wᵢ f(xᵢ)

Rules like the trapezoid or Simpson's fix their sample points on a uniform grid and adjust only the weights, buying exactness for polynomials up to roughly the number of points. Carl Friedrich Gauss's insight was to free the points too: with n nodes and n weights, 2n parameters in all, one rule can integrate every polynomial of degree ≤ 2n − 1 exactly. The magic locations are the roots of the Legendre polynomials, and the resulting Gauss-Legendre rules are astonishingly accurate: three samples integrate any quintic exactly, and five samples typically deliver eight or more digits for smooth integrands. This calculator maps the standard nodes and weights from [−1, 1] onto your interval, assembles the weighted sum term by term, and reports the true error against a high-resolution reference.

How to Use the Gaussian Quadrature Calculator

Enter the integrand, finite limits, and the number of nodes (2 through 5). The steps display the exactness degree 2n − 1, the change-of-interval substitution x = (b−a)t/2 + (a+b)/2, and one line per node: the standard abscissa tᵢ, its mapped location xᵢ, the weight wᵢ, and the sampled value f(xᵢ). The final lines assemble the weighted sum, scale by (b−a)/2, and compare with the reference integral so the rule's economy is quantified, watch five function evaluations land within 10⁻⁹ on the cosine chip. The plot marks the nodes on the curve; note how they cluster toward the interval's ends, Legendre roots are denser near ±1, which is exactly what optimal sampling requires.

Worked Example

The degree-5 chip integrates x⁵ − 2x³ + x over [−1, 1] with the 3-point rule: nodes ±√(3/5) and 0, weights 5/9, 8/9, 5/9. The rule's guarantee covers degree 2(3) − 1 = 5, so the answer must be exact, and it is: the weighted sum returns 0, the true value (an odd function over a symmetric interval). For the Gaussian integrand e−x² on [0, 1]:

3 nodes give 0.746814… against the truth 0.746824…, error ≈ 10⁻⁵ from three evaluations

Doubling to five nodes drives the error below 10⁻⁸. Compare Simpson's rule, which needs dozens of evaluations for similar accuracy: per sample, Gauss is the most accurate polynomial-based rule that exists, a provable optimality, not a heuristic.

Where the Nodes and Weights Come From

The Legendre polynomials Pₙ are the family orthogonal on [−1, 1]: ∫PₜPₙ dx = 0 for m ≠ n. Choosing the nodes as the n roots of Pₙ makes every polynomial of degree ≤ 2n − 1 integrate exactly, by a two-line argument: write such a polynomial as q·Pₙ + r with deg q, r ≤ n − 1; the q·Pₙ part integrates to zero by orthogonality and samples to zero at the roots, so only r matters, and n points always integrate a degree-(n−1) polynomial exactly with interpolatory weights. The weights, computable from the nodes, turn out always positive, which keeps the rules numerically stable at every order (high-order Newton-Cotes rules, by contrast, develop negative weights and catastrophic cancellation). The framework generalizes gracefully: swap the weight function and interval and orthogonality delivers Gauss-Chebyshev, Gauss-Laguerre (for ∫₀e−xf), and Gauss-Hermite (for ∫e−x²f over the real line), the standard artillery for improper and weighted integrals.

Common Mistakes to Avoid

  • Forgetting the interval scaling. The tabulated nodes and weights live on [−1, 1]; the mapped sum must be multiplied by (b−a)/2. Omitting the factor is the classic error, and the steps display it explicitly to guard against it.
  • Expecting exactness beyond degree 2n − 1. A 3-point rule nails quintics but not x⁶; the leftover error involves f⁽²ⁿ⁾. Exactness is a sharp boundary, not a vague promise.
  • Applying the rule across a singularity or kink. Gauss's accuracy assumes smoothness; a discontinuity in a low derivative reduces convergence to ordinary rates. Split the interval at the trouble spot or use a weighted Gauss variant designed for it.
  • Using one high-order rule on a long, wiggly interval. Five points cannot resolve many oscillations. Composite practice, subdividing and applying Gauss per panel, is how libraries deploy it; this page's single-panel form is the building block.
  • Reusing nodes when refining. Gauss nodes of different orders do not nest, so raising n discards previous evaluations. When function calls are precious and refinement likely, Gauss-Kronrod extensions or Romberg's nested scheme are the efficient alternatives.

Real-World Applications

Gaussian quadrature is the default integrator wherever accuracy per function evaluation is money. The finite element method, the backbone of structural, thermal, and electromagnetic simulation, evaluates every element's stiffness integrals by low-order Gauss rules millions of times per analysis; the 2×2 Gauss rule on quadrilaterals is arguably the most-executed numerical algorithm in engineering. Computational finance prices options by Gauss-Hermite integration against Gaussian densities; physics evaluates matrix elements and scattering amplitudes with Gauss-Laguerre for radial integrals; probability and statistics compute expectations E[f(X)] by matching the quadrature weight to the distribution's density. Computer graphics integrates lighting over hemispheres with quadrature-inspired sampling. The adaptive integrators in scientific libraries (QUADPACK's routines, and their descendants in modern languages) are built on Gauss-Kronrod pairs, Gauss rules with error estimates. Related tools on this site: Simpson's rule for the uniform-grid comparison and the definite integral calculator for symbolic evaluation.

Frequently Asked Questions

Why can n points integrate polynomials of degree 2n − 1?

Because both the locations and the weights are free: 2n parameters match the 2n conditions of integrating 1, x, …, x²ⁿ⁻¹ exactly. Orthogonal-polynomial roots are precisely the locations that make the system solvable, with the division-by-Pₙ argument turning the count into a proof.

What are the actual nodes and weights for small n?

n = 2: t = ±1/√3, w = 1, 1. n = 3: t = 0, ±√(3/5), w = 8/9, 5/9, 5/9. n = 4 and 5 involve nested radicals tabulated to machine precision inside this tool. All are symmetric about zero with positive weights summing to 2, the length of [−1, 1].

Why do the nodes crowd toward the endpoints?

Polynomial interpolation with equally spaced points suffers endpoint oscillations (the Runge phenomenon); clustering samples near the ends like the Legendre roots do (density ~ 1/√(1−t²)) tames it. Optimal integration inherits the same geometry, equal spacing is simply not where the information is.

How does the error behave for non-polynomial integrands?

The n-point error is proportional to the 2n-th derivative of f at some interior point, divided by huge factorials: for smooth functions, adding one node multiplies accuracy dramatically (spectral convergence). For functions with limited smoothness the derivative bound explodes and the advantage fades, matching the rule-of-thumb: Gauss loves analytic integrands.

What is the change-of-interval formula doing?

The substitution x = (b−a)t/2 + (a+b)/2 stretches [−1, 1] onto [a, b]; its constant Jacobian (b−a)/2 multiplies the sum. Nodes map affinely, weights are unchanged. Every Gauss table in existence is stated on [−1, 1] for exactly this reason, one table serves all intervals.

Why are positive weights such a big deal?

A weighted sum with positive weights is an average: no cancellation, errors bounded by the largest function error, and stability at any order. Newton-Cotes weights turn negative from 9 points on, amplifying rounding and integrand noise; Gauss-Legendre weights are provably positive for every n, which is why order can be raised fearlessly.

What are Gauss-Kronrod rules and why do libraries prefer them?

A Kronrod extension adds n + 1 cleverly chosen points to an n-point Gauss rule so that all old evaluations are reused, yielding a higher-order estimate plus, by differencing the two, a practically free error estimate. Adaptive integrators (like the classic 7-15 pair) subdivide wherever that estimate is large.

How do Gauss-Laguerre and Gauss-Hermite differ from Gauss-Legendre?

They bake a weight function into the rule: Laguerre integrates ∫₀ e−xf(x) dx, Hermite ∫−∞ e−x²f(x) dx, with nodes at roots of the corresponding orthogonal polynomials. Improper integrals and Gaussian expectations thereby become finite weighted sums with no truncation of the domain.

Is Gaussian quadrature always better than Simpson's rule?

Per evaluation on smooth single panels, decisively. But Simpson's uniform grid nests under refinement, tolerates modest smoothness, and is easier to reason about on tabulated data (where you cannot choose sample locations, Gauss is simply unavailable). The rules serve different constraints; Gauss wins when you control where to sample.

Why does this calculator stop at 5 nodes?

Two to five nodes cover the pedagogy, exactness degrees 3 through 9, and nearly all textbook exercises, while keeping every node and weight visible as an explicit step. Production codes use the same construction at higher orders via computed tables; nothing conceptually new appears beyond what five nodes already demonstrate.