---
title: Gaussian quadrature
heading: Choosing where to sample, not just how to weight
eyebrow: A13 · Lesson 18 · Gaussian quadrature
xp: 40
---

::: hook
The trapezoid and Simpson rules take equally spaced samples and choose only the weights. If you can evaluate the function anywhere (a field integral in a solver, an expected value over noise, an element in a finite-element program), you can choose the sample points too. Gauss showed that the best n points make the rule exact for every polynomial up to degree $2n - 1$, twice what equal spacing achieves. Two well-placed samples integrate any cubic exactly; five beat Simpson's rule with dozens. The magic points are the roots of the Legendre polynomials from A12.
:::

::: play
- The curve is a quartic, $f(x) = 1 + bx + cx^2 + dx^3 + ex^4$, on [−1, 1]. The two dots are the 2-point Gauss nodes, $\pm1/\sqrt3$; the rule is $f(-1/\sqrt3) + f(1/\sqrt3)$.
- Move b, c and d: the Gauss estimate stays exact, because it's exact for every cubic. The 2-point trapezoid rule, $f(-1) + f(1)$, is off whenever c isn't zero.
- Only the $x^4$ term (slider e) can fool two Gauss points. Three points would be exact through degree 5.
:::
:::

```sim plot
view: graph
x: x = -1..1
y: y = -3..5
slider: b = -2..2 step 0.1 = 0.5
slider: c = -2..2 step 0.1 = 1
slider: d = -2..2 step 0.1 = -1
slider: e = -2..2 step 0.1 = 0
plot: 1 + b*x + c*x^2 + d*x^3 + e*x^4 | label: f(x)
point: -1/sqrt(3), 1 - b/sqrt(3) + c/3 - d/(3*sqrt(3)) + e/9 | label: Gauss node
point: 1/sqrt(3), 1 + b/sqrt(3) + c/3 + d/(3*sqrt(3)) + e/9
readout: exact integral = 2 + 2*c/3 + 2*e/5
readout: 2-point Gauss = 2 + 2*c/3 + 2*e/9
readout: 2-point trapezoid = 2 + 2*c + 2*e
```

::: predict
## How many function evaluations does Gaussian quadrature need to integrate any polynomial of degree 9 exactly?

- [ ] 10
- [ ] 9
- [x] 5
- [ ] 3

::: reveal
Five: an n-point Gauss rule is exact through degree $2n - 1$, and $2 \times 5 - 1 = 9$. An equally spaced rule with free weights would need 10 points (Newton–Cotes), and those weights turn negative and unstable at high order.
:::
:::

::: nameit
## Gauss–Legendre rules

$$\int_{-1}^{1}f(x)\,dx \approx \sum_{i=1}^{n}w_if(x_i)$$

with the nodes $x_i$ the roots of the Legendre polynomial $P_n$ and positive weights $w_i$:

| n | Nodes | Weights | Exact through degree |
|---|---|---|---|
| 1 | 0 | 2 | 1 |
| 2 | $\pm1/\sqrt3 = \pm0.57735$ | 1, 1 | 3 |
| 3 | 0, $\pm\sqrt{3/5} = \pm0.77460$ | $\frac89$, $\frac59$, $\frac59$ | 5 |
| 4 | ±0.33998, ±0.86114 | 0.65215, 0.34785 | 7 |
| 5 | 0, ±0.53847, ±0.90618 | 0.56889, 0.47863, 0.23693 | 9 |

## Why the Legendre roots

Divide any polynomial p of degree $\le 2n - 1$ by $P_n$: $p = qP_n + r$, with q and r of degree $\le n - 1$. The integral of $qP_n$ is zero by orthogonality, and $qP_n$ is also zero at the nodes, so the rule only has to integrate r exactly, which n points with the right weights can do.

## Other intervals and weights

For [a, b], map $x = \frac{b - a}{2}t + \frac{a + b}{2}$:

$$\int_a^bf(x)\,dx \approx \frac{b - a}{2}\sum w_if\left(\frac{b - a}{2}t_i + \frac{a + b}{2}\right)$$

Other weight functions have their own rules: **Gauss–Hermite** for $\int e^{-x^2}f(x)\,dx$, which gives expectations over Gaussian noise, and **Gauss–Laguerre** for $\int_0^\infty e^{-x}f(x)\,dx$. **Gauss–Kronrod** rules add points to a Gauss rule so the difference between the two estimates gives an error estimate for free.

## When not to use it

Gauss rules need f at irrational points, so they don't fit sampled data. For functions with a kink or singularity inside the interval, split the interval at the trouble spot first.
:::

::: example
## Integrate $e^x$ from −1 to 1 with 2- and 3-point Gauss rules, and compare with Simpson's rule on 3 points.

::: solution
1. **Exact value.**
   $$2\sinh 1 = 2.3504024$$
2. **Two-point Gauss.**
   $$\begin{aligned} &e^{-0.57735} + e^{0.57735} \\ &= 2.3426961 \end{aligned}$$
   error $7.7 \times 10^{-3}$.
3. **Three-point Gauss.**
   $$\begin{aligned} &\frac59(e^{-0.77460} + e^{0.77460}) \\ &\quad + \frac89 \\ &= 2.3503369 \end{aligned}$$
   error $6.5 \times 10^{-5}$.
4. **Simpson with 3 points.**
   $$\begin{aligned} &\frac13(e^{-1} + 4 + e) \\ &= 2.3620538 \end{aligned}$$
   error $1.2 \times 10^{-2}$.
5. **Compare.** Simpson uses the same number of evaluations as the three-point rule and has 180 times its error.

**Answer:** 2.3426961 with two Gauss points and 2.3503369 with three, against the exact 2.3504024; Simpson's 2.3620538 has 180 times the three-point error.
:::
:::

::: example
## Integrate $x^3$ from 0 to 2 with the 2-point rule.

::: solution
1. **Map the nodes.** On [0, 2] the nodes map to $1 \mp 0.57735$, that is 0.42265 and 1.57735.
2. **Apply the rule.**
   $$\begin{aligned} &\frac{2 - 0}{2}\left(0.42265^3 + 1.57735^3\right) \\ &= 0.07550 + 3.92450 \\ &= 4.0000 \end{aligned}$$
3. **Check.** That's exact, as it must be for a cubic.

**Answer:** 4.0000, the exact value.
:::
:::

::: example
## Evaluate $\int_{-1}^{1}\frac{x^2}{\sqrt{1 - x^2}}dx$ with the two-point Gauss–Chebyshev rule.

::: solution
1. **The rule.** For integrals with the weight $\frac{1}{\sqrt{1 - x^2}}$, the n-point Gauss–Chebyshev rule uses the nodes $\cos\frac{(2k - 1)\pi}{2n}$ and the equal weights $\frac\pi n$.
2. **Two points.** With n = 2 the nodes are $\pm0.7071$, so
   $$\frac\pi2(0.5 + 0.5) = \frac\pi2$$
3. **Why it's exact.** That's exact, though the integrand blows up at both ends: the weight takes care of the singularity, and the rule needs only to integrate the smooth factor $x^2$.
4. **Compare.** Gauss–Legendre on the same integrand would converge slowly.

**Answer:** $\frac\pi2$, exactly.
:::
:::

::: check
given: k = 4 | 6 | 8
answer: 2*(1/3)^(k/2)
tolerance: 0.1%

## What does 2-point Gauss quadrature give for $\int_{-1}^{1}x^{\given{k}}\,dx$?

::: right
Right. $2(1/\sqrt3)^k$ = {answer}, against the exact $\frac{2}{k + 1}$: degree {k} is beyond its reach.
:::

::: misses
- [2/(k + 1)] That's the exact value; two points are exact only through degree 3.
- [any] Not yet. $(-1/\sqrt3)^k + (1/\sqrt3)^k$.
:::
:::

::: check
given: c = 1 | 2 | 3
answer: 5/9*(exp(-c*sqrt(0.6)) + exp(c*sqrt(0.6))) + 8/9
tolerance: 0.001%

## What does the 3-point Gauss rule give for $\int_{-1}^{1}e^{\given{c}x}\,dx$?

::: right
Right. {answer}, against the exact $\frac{2\sinh c}{c}$.
:::

::: misses
- [2*sinh(c)/c] That's the exact value; the rule's answer differs slightly.
- [any] Not yet. $\frac59\left(e^{-c\sqrt{0.6}} + e^{c\sqrt{0.6}}\right) + \frac89 \cdot 1$.
:::
:::

::: check
given: n = 3 | 6 | 10
answer: 2*n - 1
tolerance: 0

## Through what polynomial degree is the {n}-point Gauss–Legendre rule exact?

::: right
Right. $2n - 1$ = {answer}.
:::

::: misses
- [n - 1] That's what n equally spaced points with free weights guarantee; Gauss also chooses the points.
- [any] Not yet. $2n - 1$.
:::
:::

::: check
given: b = 1 | 2 | 3
answer: (b/2)*((b/2*(1 - 1/sqrt(3)))^4 + (b/2*(1 + 1/sqrt(3)))^4)
tolerance: 0.01%

## What does the 2-point Gauss rule give for $\int_0^{\given{b}}x^4\,dx$?

::: right
Right. {answer}, against the exact $b^5/5$.
:::

::: misses
- [b^5/5] That's exact; two points can't integrate a quartic exactly.
- [any] Not yet. Map the nodes: $x = \frac b2(1 \pm 1/\sqrt3)$, and scale by $\frac b2$.
:::
:::

::: check
given: a = 0 | 1; b = 2 | 4
answer: (b - a)/2*(1/sqrt(3)) + (a + b)/2
tolerance: 0.01%

## Where does the positive 2-point node $t = 1/\sqrt3$ land on the interval [{a}, {b}]?

::: right
Right. $\frac{b - a}{2}t + \frac{a + b}{2}$ = {answer}.
:::

::: misses
- [any] Not yet. $x = \frac{b - a}{2}t + \frac{a + b}{2}$.
:::
:::

::: check
kind: choice

## Why can't Gaussian quadrature be used directly on an oscilloscope's sampled waveform?

- [ ] Its weights are negative
- [x] It needs the function at specific irrational points, and samples only exist at equal spacing
- [ ] It only works for polynomials
- [ ] It's less accurate than the trapezoid rule

::: misses
- [1] Gauss weights are all positive.
- [3] It works for any smooth function; polynomials are where it's exact.
- [4] For smooth functions it's far more accurate.
:::
:::

::: check
kind: parts
given: k = 6 | 8 | 10 | 12 | 14 | 16

## The 3-point Gauss–Legendre rule has nodes 0 and $\pm\sqrt{3/5}$, with weights $\frac89$ and $\frac59$.

::: part
answer: 2*(5/9)*(3/5)^(k/2)
tolerance: 0.5%

### What does it give for $\int_{-1}^{1}x^{\given{k}}\,dx$?

::: misses
- [2/(k + 1)] That's the exact value; the 3-point rule is exact only through degree 5.
- [any] $\frac59\left(\left(\sqrt{3/5}\right)^k \cdot 2\right)$.
:::
:::

::: part
answer: 2/(k + 1)
tolerance: 0.5%

### What is the exact value?

::: misses
- [any] $\frac{2}{k + 1}$.
:::
:::
:::

::: check
kind: derivation
vars: a = -2..0, b = 1..3

## Map the 2-point Gauss rule to $[a, b]$ with $x = \frac{a + b}{2} + \frac{b - a}{2}t$.

::: step
answer: (a + b)/2 + (b - a)/(2*sqrt(3))

### Where does the node $t = 1/\sqrt3$ land?
:::

::: step
answer: (b^3 - a^3)/3

### What does the rule give for $\int_a^bx^2\,dx$?
:::

::: right
Right. Two points integrate any cubic exactly, on any interval.
:::
:::

::: explain
## How can n sample points integrate every polynomial up to degree $2n - 1$ exactly?

::: model
A rule with n points has 2n free choices: n weights and n locations, and polynomials up to degree $2n - 1$ have 2n coefficients, so the count works out. Gauss's choice of locations is the roots of the Legendre polynomial $P_n$. Any polynomial p of degree up to $2n - 1$ can be split as $qP_n + r$ with q and r of degree below n. The $qP_n$ part integrates to zero, because $P_n$ is orthogonal to every lower-degree polynomial, and it's also zero at every node, so the rule correctly gives it zero. What's left, r, has degree below n, and n points with suitable weights integrate such polynomials exactly.
:::
:::

::: practice

::: check
given: k = 4 | 6 | 8 | 10 | 12 | 14
answer: 2*(1/3)^(k/2)
tolerance: 0.1%

## 2-point Gauss for $\int_{-1}^1x^{\given{k}}$?

::: misses
- [any] $2(1/3)^{k/2}$.
:::
:::

::: check
given: n = 2..30 int
answer: 2*n - 1
tolerance: 0

## Degree of exactness, {n}-point Gauss?

::: misses
- [any] $2n - 1$.
:::
:::

::: check
given: c = 0.5 | 1 | 1.5 | 2 | 3 | 4
answer: 5/9*(exp(-c*sqrt(0.6)) + exp(c*sqrt(0.6))) + 8/9
tolerance: 0.001%

## 3-point Gauss for $\int_{-1}^1e^{\given{c}x}$?

::: misses
- [any] $\frac59(e^{-c\sqrt{0.6}} + e^{c\sqrt{0.6}}) + \frac89$.
:::
:::
:::
