---
title: Roots of polynomials
heading: Horner's rule, deflation, companion matrices and how sensitive roots are
eyebrow: A13 · Lesson 8 · Roots of polynomials
xp: 40
---

::: hook
Polynomials are everywhere in engineering: characteristic equations, filter denominators, the transfer functions of every linear circuit. Finding their roots looks like a special case of lessons 4 to 7, and the methods do carry over, with some twists. A polynomial can be evaluated together with its derivative in one cheap pass. Once a root is found it can be divided out. All the roots can be found at once as the eigenvalues of a matrix. And the roots of a polynomial written out in expanded form can be wildly sensitive to its coefficients, which is why filters are built from small sections instead of one big polynomial.
:::

::: play
- The crosses are the roots of $(s + 1)(s + 2)\cdots(s + 8) + \varepsilon s^7$: the polynomial with roots at −1 to −8, with ε added to its $s^7$ coefficient, which is 36.
- Raise ε a little. At ε = 0.0005, a change of 0.0014% in that coefficient, the roots at −6 and −7 have already met and split into a complex pair, and the root at −8 has moved to −8.16.
- The roots near −1 hardly move: their sensitivity grows with the size of the root raised to the power of the perturbed term.
:::

```sim plot
view: splane
x: sigma = -11..1
y: omega = -3..3
slider: e = 0..0.005 step 0.0001 = 0
den: (s + 1)*(s + 2)*(s + 3)*(s + 4)*(s + 5)*(s + 6)*(s + 7)*(s + 8)
num: s^7
locus: e = 0..0.005
readout: first-order shift of the root at −8 = -416*e | digits: 3
```

::: predict
## A polynomial's coefficients are rounded to 16 significant digits. Its roots are then computed exactly. How many digits of the roots are correct?

- [x] It depends on the polynomial: sometimes all 16, sometimes very few
- [ ] Always about 16
- [ ] Always about 8
- [ ] None, since rounding changes the polynomial

::: reveal
It depends entirely on the polynomial. A well-separated root of a small polynomial keeps nearly all its digits. Clustered or repeated roots, or roots of a high-degree polynomial in expanded form, can lose most of them: Wilkinson's famous example, $(x - 1)(x - 2)\cdots(x - 20)$, has roots that turn complex when one coefficient changes in its tenth digit. That's conditioning, a property of the problem, so no root-finder can fix it.
:::
:::

::: nameit
## Horner's rule

Evaluate $p(x) = a_nx^n + \cdots + a_0$ as $(\cdots((a_nx + a_{n-1})x + a_{n-2})x + \cdots)x + a_0$: n multiplications and n additions. Running a second Horner pass on the intermediate values gives $p'(x)$ in another 2n operations, so a Newton step costs about 4n.

## Deflation

The intermediate Horner values $b_k$ are the coefficients of the quotient: $p(x) = (x - r)q(x) + p(r)$. When r is a root, $q$ has the remaining roots, so you can find them one by one. Rounding errors build up as you go, so deflate out small roots first and then **polish** each root with a Newton step on the original p.

## All roots at once

- **Companion matrix.** The roots of $x^n + c_{n-1}x^{n-1} + \cdots + c_0$ are the eigenvalues of the matrix with first row $(-c_{n-1}, \ldots, -c_0)$ and ones just below the diagonal. `np.roots` works this way, with the QR algorithm.
- **Durand–Kerner and Aberth** iterate all n approximations together: $x_i \leftarrow x_i - \frac{p(x_i)}{\prod_{j \ne i}(x_i - x_j)}$ is Durand–Kerner. Aberth's refinement converges cubically and is what the platform's own s-plane view uses.

## Sensitivity

If coefficient $a_k$ changes by δ, a simple root r moves by about

$$\delta r \approx -\frac{\delta\,r^k}{p'(r)}.$$

Large roots, high powers and small $p'(r)$ (roots close together) all make it big. A root of multiplicity m moves by about $\delta^{1/m}$, far more than δ.
:::

::: example
## Evaluate $p(x) = 2x^3 - 6x^2 + 2x - 1$ and $p'(x)$ at x = 3 with Horner's rule.

::: solution
1. **First pass, for $p(3)$.**
   $$\begin{aligned} b_3 &= 2 \\ b_2 &= 2 \cdot 3 - 6 \\ &= 0 \\ b_1 &= 0 \cdot 3 + 2 \\ &= 2 \\ b_0 &= 2 \cdot 3 - 1 \\ &= 5 \end{aligned}$$
   so $p(3) = 5$.
2. **Second pass on $b_3, b_2, b_1$, for $p'(3)$.**
   $$\begin{aligned} c_2 &= 2 \\ c_1 &= 2 \cdot 3 + 0 \\ &= 6 \\ c_0 &= 6 \cdot 3 + 2 \\ &= 20 \end{aligned}$$
   so $p'(3) = 20$.
3. **Check.**
   $$\begin{aligned} p'(x) &= 6x^2 - 12x + 2 \\ p'(3) &= 54 - 36 + 2 \\ &= 20 \end{aligned}$$
4. **A Newton step from 3.**
   $$\begin{aligned} x_1 &= 3 - \frac{5}{20} \\ &= 2.75 \end{aligned}$$

**Answer:** $p(3) = 5$ and $p'(3) = 20$, so a Newton step from 3 goes to 2.75.
:::
:::

::: example
## $x = 1$ is a root of $x^3 - 6x^2 + 11x - 6$. Deflate it and find the other roots.

::: solution
1. **Horner at 1.**
   $$\begin{aligned} b_2 &= 1 \\ b_1 &= 1 - 6 \\ &= -5 \\ b_0 &= -5 + 11 \\ &= 6 \end{aligned}$$
   and the remainder is $6 - 6 = 0$.
2. **The quotient.** It is
   $$x^2 - 5x + 6$$
   whose roots are 2 and 3.
3. **With computed roots.** Roots that carry small errors leave those errors in the quotient. Polishing each root found from a quotient with one or two Newton steps on the original cubic removes that accumulated error.

**Answer:** the quotient is $x^2 - 5x + 6$, so the other roots are 2 and 3.
:::
:::

::: example
## The polynomial $(s + 1)(s + 2)\cdots(s + 8)$ has 36 as its $s^7$ coefficient. How far does the root at −8 move if that coefficient becomes 36.0005?

::: solution
1. **The derivative at the root.**
   $$\begin{aligned} p'(-8) &= (-7)(-6)\cdots(-1) \\ &= -5040 \end{aligned}$$
2. **The first-order estimate.** With $(-8)^7 = -2\,097\,152$:
   $$\begin{aligned} \delta r &\approx -\frac{0.0005 \times (-2\,097\,152)}{-5040} \\ &= -0.21 \end{aligned}$$
3. **The actual roots.** Computing the roots directly gives −8.16, and the roots at −6 and −7 have collided and left the real axis as $-6.50 \pm 0.45j$.
4. **How sensitive that is.** A relative change of $1.4 \times 10^{-5}$ in one coefficient moved the root at −8 by 2% and the roots at −6 and −7 by about 10%.
5. **What filter designers do.** That's why an eighth-order filter is built as four second-order sections, each with well-conditioned roots, instead of from its expanded polynomial.

**Answer:** the first-order estimate moves the root by −0.21; it actually moves to −8.16, and the roots at −6 and −7 become $-6.50 \pm 0.45j$.
:::
:::

::: check
given: a = 1 | 2; b = -3 | 4; c = 2 | -5; d = 1 | 7; x = 2 | -1.5
answer: ((a*x + b)*x + c)*x + d
tolerance: 0.5%

## Evaluate $p(x) = \given{a}x^3 + \given{b}x^2 + \given{c}x + \given{d}$ at x = {x} with Horner's rule.

::: right
Right. Three multiplications and three additions.
:::

::: misses
- [any] $((ax + b)x + c)x + d$.
:::
:::

::: check
given: a = 1 | 2; b = -3 | 4; c = 2 | -5; x = 2 | -1.5
answer: 3*a*x^2 + 2*b*x + c
tolerance: 0.5%

## What is $p'(x)$ for $p = \given{a}x^3 + \given{b}x^2 + \given{c}x + d$ at x = {x}, from the second Horner pass?

::: right
Right. The second pass on the first pass's intermediate values gives $p'$.
:::

::: misses
- [3*a*x^2 + 2*b*x] Include the linear coefficient.
- [any] $3ax^2 + 2bx + c$.
:::
:::

::: check
given: r = 1 | 2 | 3; e = 0.001 | 0.01
answer: -e/((r - 1)*(r - 2) + (r - 1)*(r - 3) + (r - 2)*(r - 3))
tolerance: 1%

## The constant term of $(x - 1)(x - 2)(x - 3)$ changes by {e}. About how far does the root at {r} move?

::: right
Right. $\delta r \approx -\frac{\delta}{p'(r)}$ for the constant term ($k = 0$).
:::

::: misses
- [e/((r - 1)*(r - 2) + (r - 1)*(r - 3) + (r - 2)*(r - 3))] Check the sign: $p(r) + \delta + p'(r)\delta r = 0$.
- [any] $p'(r)$ is the product of r minus the other two roots.
:::
:::

::: check
given: r1 = -1 | 2; r2 = -3 | 0.5; r3 = 4.5 | -2
answer: r1 + r2 + r3
tolerance: 0.5%

## The monic cubic with roots {r1}, {r2} and {r3} has a companion matrix. What is that matrix's trace?

::: right
Right. The trace is $-c_2$, the sum of the roots, as the sum of the eigenvalues must be.
:::

::: misses
- [-(r1 + r2 + r3)] The first row holds $-c_2$, which is the sum of the roots.
- [any] The trace is the sum of the eigenvalues.
:::
:::

::: check
given: m = 2 | 3 | 4; d = 0.0001 | 0.000001
answer: d^(1/m)
tolerance: 1%

## $(x - 1)^{\given{m}}$ has its constant term changed by {d}. About how far do its roots move from 1?

::: right
Right. $(x - 1)^m = \pm\delta$ puts the roots at distance $\delta^{1/m}$.
:::

::: misses
- [d] A multiple root is far more sensitive than a simple one.
- [any] Solve $(x - 1)^m = \delta$ for the size of $x - 1$.
:::
:::

::: check
kind: parts
given: m = 3..9 int

## Newton's method approaches a root of multiplicity {m}.

::: part
answer: (m - 1)/m
tolerance: 0.5%

### By what factor does each step shrink the error?

::: misses
- [1/m] The factor is $1 - \frac1m$.
- [any] For $f = (x - r)^m$, $x - \frac{f}{f'} - r = (x - r)\left(1 - \frac1m\right)$.
:::
:::

::: part
answer: m
tolerance: 0

### What multiple of the step $f/f'$ restores quadratic convergence?

::: misses
- [any] Modified Newton: $x - m\frac{f}{f'}$.
:::
:::
:::

::: check
kind: choice

## Why are high-order digital and analog filters built as cascades of second-order sections?

- [ ] Second-order sections run faster
- [x] The roots of a high-degree polynomial in expanded form are very sensitive to its coefficients, so rounding the coefficients would move the poles
- [ ] Polynomials above degree 4 have no root formula
- [ ] It halves the number of coefficients

::: right
Right. Each quadratic's roots depend only on its own three coefficients, so rounding them moves each pole a little instead of moving clustered poles a lot.
:::

::: misses
- [1] The operation count is about the same either way.
- [3] Numerical root-finders don't need formulas; the issue is sensitivity.
- [4] The count is about the same; what changes is how errors affect the poles.
:::
:::

::: check
kind: derivation
vars: a = -3..3, b = -3..3, c = -3..3, x = 0.5..3

## Newton's method on $p(x) = x^3 + ax^2 + bx + c$.

::: step
answer: ((x + a)*x + b)*x + c

### What is p(x), in Horner's nested form?
:::

::: step
answer: x - (((x + a)*x + b)*x + c)/(3*x^2 + 2*a*x + b)

### What is the Newton step's new x?
:::

::: right
Right. With Horner's second pass for $p'$, each step costs about 4n operations for degree n.
:::
:::

::: check
kind: python

## Find all three roots of $x^3 - 6x^2 + 11x - 6$ at once with the Durand–Kerner iteration $z_i \leftarrow z_i - \frac{p(z_i)}{\prod_{j\ne i}(z_i - z_j)}$, starting from $z_k = (0.4 + 0.9j)^k$, k = 0, 1, 2, for 100 sweeps (update all three from the old values each sweep). Set `roots` to their sorted real parts.

```python starter
import numpy as np

roots = [0.0, 0.0, 0.0]
print(roots)
```

```python solution
import numpy as np

p = np.poly1d([1, -6, 11, -6])
z = np.array([(0.4 + 0.9j)**k for k in range(3)])
for _ in range(100):
    new = z.copy()
    for i in range(3):
        d = np.prod([z[i] - z[j] for j in range(3) if j != i])
        new[i] = z[i] - p(z[i]) / d
    z = new
roots = sorted(z.real)
print(roots)
```

```python tests
assert all(abs(r - e) < 1e-12 for r, e in zip(roots, [1, 2, 3])), f"The roots are 1, 2 and 3; got {roots}."
```

::: right
Right. 1, 2 and 3, all found together. The complex starting points keep the iterates from getting stuck on the real axis.
:::
:::

::: explain
## Why does a root of multiplicity m move by about $\delta^{1/m}$ when a coefficient changes by δ?

::: model
Near a root r of multiplicity m, the polynomial behaves like $c(x - r)^m$: it's extremely flat, since its first m − 1 derivatives vanish there. Adding δ to a coefficient adds roughly a constant δ near r, so the new roots satisfy $c(x - r)^m \approx -\delta$, which puts them at distance $\lvert\delta/c\rvert^{1/m}$ from r, spread around a circle. For a simple root that distance is proportional to δ, but for a double root it's $\sqrt\delta$: a perturbation of $10^{-16}$ moves a double root by about $10^{-8}$. The flatter the polynomial at its root, the less information its values carry about where the root is.
:::
:::

::: practice

::: check
given: a = 3 | -1; b = 0 | 2; c = -4 | 1; d = 5 | -2; x = 1.5 | -2
answer: ((a*x + b)*x + c)*x + d
tolerance: 0.5%

## Horner: $\given{a}x^3 + \given{b}x^2 + \given{c}x + \given{d}$ at {x}?

::: misses
- [any] $((ax + b)x + c)x + d$.
:::
:::

::: check
given: m = 2 | 5; d = 0.0000000001 | 0.00000001
answer: d^(1/m)
tolerance: 1%

## Shift of an m = {m} root for δ = {d}?

::: misses
- [any] $\delta^{1/m}$.
:::
:::

::: check
given: r1 = 1 | -4; r2 = 2 | 3; r3 = -5 | 0.5
answer: r1 + r2 + r3
tolerance: 0.5%

## Companion matrix trace for roots {r1}, {r2}, {r3}?

::: misses
- [any] The sum of the roots.
:::
:::
:::
