Gauss Quadrature Simulator Back
Numerical Analysis Simulator

Gauss Quadrature Simulator — Convergence of Gauss-Legendre Integration

Visualize the sample points and weights of Gauss-Legendre quadrature. Change the point count, integration interval and integrand to learn why few points give high accuracy.

Parameters
Integrand f(x)
Number of points n
pts
Lower limit a
Upper limit b

The "exact value" is a reference computed with a finely subdivided composite Simpson rule.

Results
Gauss quadrature approximation
Exact value (reference integral)
Absolute error
Degree integrated exactly
Integrand and Sample Points

Cyan = integrand f(x) / shaded = the area being integrated / orange vertical line = sample point xᵢ (circle size = weight wᵢ)

Error Convergence versus Point Count

X axis = number of points n / Y axis = log₁₀|absolute error| (yellow dot = current n, lower is more accurate)

Theory & Key Formulas

Gauss-Legendre quadrature approximates an integral as a sum of function values at sample points times weights. What sets it apart from the equally spaced trapezoidal and Simpson rules is that it optimizes both the positions of the sample points and the weights.

The n-point Gauss quadrature rule. xᵢ are the sample points, wᵢ are the weights:

$$\int_a^b f(x)\,dx \approx \sum_{i=1}^{n} w_i\,f(x_i)$$

Linear transformation of the standard-interval [−1, 1] points ξᵢ and weights to an arbitrary interval [a, b]:

$$x_i = \frac{b-a}{2}\,\xi_i + \frac{a+b}{2}, \qquad w_i = \frac{b-a}{2}\,\hat{w}_i$$

The sample points ξᵢ are the roots of the n-th Legendre polynomial Pₙ(ξ), and the n-point rule integrates polynomials up to degree 2n−1 exactly.

Because it uses 2n free parameters (n positions plus n weights), it can satisfy 2n exactness conditions. This is why it beats equally spaced methods.

What is the Gauss Quadrature Simulator

🙋
Numerical integration is the thing where you chop the interval into thin strips and add up their areas, right? How is Gauss quadrature different from that?
🎓
The strip-adding trapezoidal and Simpson rules place the sample points at equal spacing. Gauss quadrature also gets to choose where to put the sample points. It optimizes both the positions and the weights. The orange vertical lines in the simulator above are the sample points — look closely and you'll see they cluster toward the ends, not evenly spaced. That's the key.
🙋
What do you gain by being able to choose the positions too?
🎓
You double the number of free parameters. With n points you have n positions and n weights, 2n in total. So you can satisfy "2n conditions", and an n-point rule integrates polynomials up to degree 2n−1 exactly. Look at the "degree integrated exactly" card — it goes up by 2 every time you add one point. The Simpson rule has a much lower degree for the same number of points.
🙋
How are the sample-point positions decided? Just optimized somehow?
🎓
There is actually a beautiful answer: the sample points are the roots of a Legendre polynomial. If you use polynomials orthogonal on [−1,1], the remainder term vanishes cleanly by orthogonality, which automatically guarantees exactness up to degree 2n−1. The simulator takes the standard-interval roots and linearly transforms them to your chosen interval [a,b]. So the "relative arrangement" of the sample points stays the same even when you change the interval.
🙋
When I switch the function to the "Runge function", the error graph barely drops even as I add points.
🎓
Good catch. For a smooth function like $e^{x/2}$, the error falls exponentially with the point count — the convergence graph plunges almost in a straight line. But the Runge function $1/(1+25x^2)$ has a singularity close by in the complex plane, so it converges much more slowly. "More points always means more accuracy" is simply not true. In that case you use composite quadrature, splitting the interval. There is no universal method — that's an important lesson in numerical analysis.

Frequently Asked Questions

Historically they were given as tables; today numerical libraries carry them as standard. The roots of the n-th Legendre polynomial can be found to high precision with methods such as Newton's method, and the corresponding weights are computed from the derivative value at each root. This simulator stores the standard-interval [−1,1] values for n=1 to 8 and linearly transforms them to the chosen interval.
For integrating a smooth function with few function evaluations, Gauss quadrature is overwhelmingly favorable. On the other hand, if the integrand is already given as a table at equal spacing, or if you want to add sample points later, the trapezoidal rule or the Gauss-Kronrod rule, which reuse points, are more practical. Once the assumption that you can freely choose the sample points breaks, the advantage of Gauss quadrature is lost.
In the finite element method, the stiffness matrix of each element is found by integrating products of shape functions over the element. These are polynomials (or close to them), so Gauss quadrature can evaluate them exactly or to high accuracy with few integration points. The extension to 2D and 3D elements is by tensor product. The choice of the number of integration points is an important design matter, affecting accuracy, computational cost and numerical instabilities such as hourglass modes.
Yes, by changing the family of orthogonal polynomials. Gauss-Chebyshev handles the weight function 1/√(1−x²), Gauss-Laguerre handles integrals with weight e^(−x) on a semi-infinite interval, and Gauss-Hermite handles integrals with weight e^(−x²) on the whole real line. This simulator deals with Gauss-Legendre, weight function 1 on a finite interval, but the idea generalizes directly to the other families.

Real-World Applications

Numerical integration in the finite element method (FEM): Inside every FEM solver — for structural, thermal or electromagnetic analysis — Gauss quadrature is used to compute element stiffness and mass matrices. Because the shape functions are polynomials, a small number of integration points gives high accuracy. "Reduced integration", which deliberately uses fewer integration points, speeds up the computation but invites hourglass modes, so it is used together with stabilization.

Computational physics and chemistry: In fields where analytically intractable integrals appear in large numbers — electron integrals in quantum chemistry, radiative transfer, partition functions in statistical mechanics — Gauss quadrature is a standard tool. For multidimensional integrals it is combined with tensor products or sparse grids to fight the curse of dimensionality.

Financial engineering and probabilistic computation: Pricing options requires the fast computation of expected values, which are integrals against a probability density. Gauss-Hermite quadrature is well matched to expectation calculations under a normal distribution and can compute smooth integrals accurately with fewer evaluation points than a Monte Carlo method.

Signal processing and control engineering: There are many situations where a design metric is evaluated by numerical integration — the response energy of a system, the integral of a cost function, frequency-domain integrals in filter design. As long as the integrand is smooth, Gauss quadrature returns a reliable value with little computation.

Common Misconceptions and Cautions

The most common misconception is to think that "adding more sample points keeps reducing the error for any function". This holds only when the integrand is sufficiently smooth. In the simulator, switch the function to the Runge function $1/(1+25x^2)$ and add points. The convergence graph does not fall the way it does for the smooth $e^{x/2}$. With discontinuities, cusps, or a singularity close by in the complex plane, even Gauss quadrature converges slowly. Such functions are handled with composite quadrature that splits the interval, or with a change of variable matched to the singularity.

The next most common error is to assume that "Gauss quadrature is always better than the trapezoidal rule". The strength of Gauss quadrature presupposes that you can place the sample points at freely chosen positions. If the integrand values are given as equally spaced sensor data, or if you want to raise accuracy without discarding already-computed points (adaptive integration), the trapezoidal rule or the Gauss-Kronrod rule, which can reuse sample points, are more practical. Which algorithm is superior depends on the context of how the data is available.

Finally, take care not to confuse the "exact up to degree 2n−1" of an n-point rule with the size of the error itself. "Exact" only means that if the integrand happens to be a polynomial up to that degree, the error is zero. A real integrand is not a polynomial, so components beyond the exactness degree always remain, and those become the error. In the simulator, select the polynomial $x^4-2x^2+1$ and set n=3: since 2n−1=5 ≥ 4, you can confirm the absolute error becomes essentially zero (rounding only). The exactness degree is an indicator of "how far you get correct for free"; the accuracy for other functions must be judged separately from the convergence behavior.