---
title: Conditioning and stability
heading: Is the problem sensitive, or is the method careless?
eyebrow: A13 · Lesson 3 · Conditioning and stability
xp: 40
---

::: hook
When a computed answer comes out wrong, there are two very different possible culprits. The problem itself may be **ill-conditioned**: so sensitive that a change in the 16th digit of the input changes the 5th digit of the answer, and no algorithm can do better. Or the problem is fine and the **algorithm is unstable**: it amplifies rounding errors that a better method wouldn't. The distinction tells you what to fix. An ill-conditioned problem needs better data or a reformulation; an unstable algorithm needs replacing. Most of this course is about choosing stable algorithms and knowing the condition of the problems they solve.
:::

::: play
- The curves are the **condition numbers** $\kappa(x) = \left\lvert\frac{xf'(x)}{f(x)}\right\rvert$ of four simple functions: how many times a relative change in x is magnified in f(x).
- $\sqrt x$ halves relative errors everywhere. $e^x$ magnifies them by x, so it's sensitive only for large arguments.
- $\ln x$ and $x - 1$ blow up near x = 1: there the answer is near zero, so any absolute error in x is a huge relative error in the answer. The readouts give the digits lost at the marked x.
:::
:::

```sim plot
view: graph
x: x = 0.05..3
y: k = 0.1..10000 log
slider: x0 = 0.1..2.9 step 0.01 = 1.05
plot: 0.5 | label: √x
plot: x | label: eˣ
plot: 1/abs(ln(x)) | samples: 800 | label: ln x
plot: abs(x/(x - 1)) | samples: 800 | dashed | label: x − 1
marker: x = x0
readout: κ for ln x at x0 = 1/abs(ln(x0))
readout: decimal digits lost = log(1/abs(ln(x0)))
```

::: predict
## Summing the Taylor series $\sum(-20)^k/k!$ in double gives $e^{-20}$ as $6.1 \times 10^{-9}$; the truth is $2.06 \times 10^{-9}$. Whose fault is it?

- [ ] The problem's: $e^{-20}$ is ill-conditioned
- [x] The algorithm's: the series is unstable for negative arguments
- [ ] The computer's: double precision isn't enough
- [ ] Nobody's: $6.1 \times 10^{-9}$ is within rounding

::: reveal
The algorithm's. Evaluating $e^x$ has condition number $\lvert x\rvert = 20$, so about 1 digit at risk. But the series adds and subtracts terms as large as $20^{20}/20! \approx 4.3 \times 10^7$ to reach $2 \times 10^{-9}$; each carries an error near $4.3 \times 10^7 \times 10^{-16} \approx 5 \times 10^{-9}$, larger than the answer. Computing $1/e^{20}$, a series of all-positive terms, gives $2.061153622438558 \times 10^{-9}$ to full precision.
:::
:::

::: nameit
## Conditioning: a property of the problem

The relative **condition number** of evaluating f at x is

$$\kappa = \left\lvert\frac{\text{relative change in }f}{\text{relative change in }x}\right\rvert \approx \left\lvert\frac{xf'(x)}{f(x)}\right\rvert$$

For a linear system $Ax = b$ it's $\kappa(A) = \lVert A\rVert\lVert A^{-1}\rVert$ (A7). Roughly, **an answer loses $\log_{10}\kappa$ decimal digits** to the unavoidable rounding of its inputs, whatever the algorithm.

| Problem | κ |
|---|---|
| $\sqrt x$ | ½ |
| $e^x$ | $\lvert x\rvert$ |
| $\ln x$ | $1/\lvert\ln x\rvert$ |
| $x - a$ | $\lvert x/(x - a)\rvert$ |
| roots of a polynomial | can be enormous: Wilkinson's $(x - 1)(x - 2)\cdots(x - 20)$, expanded, loses its roots near 16 and 17 to complex values when one coefficient changes by $2^{-23}$ |

## Stability: a property of the algorithm

An algorithm is **backward stable** if its computed answer is the exact answer to a slightly perturbed problem, with the perturbation a few units of roundoff. Then

$$\text{relative error} \lesssim \kappa \times \text{(backward error)} \approx \kappa\,u$$

which is the best any method can promise. An unstable algorithm adds errors of its own: cancellation (lesson 2), growth through repeated steps (A12's explicit scheme past $r = \frac12$), or large intermediate values like the alternating series above.

## Checking which one you have

Perturb the input by a few ulps and rerun: if the answer moves a lot, the problem is ill-conditioned. Compare with a different, trusted method on the same input: if they disagree while the problem is well-conditioned, an algorithm is unstable.
:::

::: example
## A sensor's readings are $10^9$ plus 4, 7, 13 and 16. Compute their variance as the mean of squares minus the square of the mean, and as the mean squared deviation.

::: solution
1. **The one-pass formula.** It needs $\overline{x^2} \approx 10^{18}$, where doubles are 128 apart, and subtracts $\bar x^2$ from it.
2. **What double gives.** Summed left to right in double, the result is −128: a negative variance. (Python 3.12's built-in `sum` rounds more carefully and gets 0, which is still wrong.)
3. **The two-pass formula.** Subtract the mean first, which leaves the deviations $(-6, -3, 3, 6)$:
   $$\begin{aligned} \sigma^2 &= \frac{36 + 9 + 9 + 36}{4} \\ &= 22.5 \end{aligned}$$
   exactly.
4. **Conditioning and stability.** The problem is well-conditioned (the deviations are known to full precision); the one-pass algorithm is unstable.

**Answer:** the one-pass formula gives −128 (0 with Python 3.12's `sum`); the two-pass formula gives the correct variance, 22.5.
:::
:::

::: example
## A circuit's nodal matrix has $\kappa(A) \approx 10^{11}$. How many digits can you trust in its node voltages?

::: solution
1. **Digits left.** Double carries about 16 digits, and a condition number of $10^{11}$ can cost 11 of them:
   $$16 - 11 = 5$$
2. **The solver can't help.** A backward-stable solver (LU with pivoting) can't do better.
3. **Where a large κ comes from.** It usually means a nearly floating node or wildly different conductances (a 1 Ω part next to a 100 GΩ one). Rescaling or a better model is the fix.

**Answer:** about 5 digits.
:::
:::

::: example
## $x^2 - 2x + 1 - \varepsilon = 0$ has a double root at 1 when ε = 0. How far do the roots move when ε = $10^{-10}$?

::: solution
1. **The perturbed roots.**
   $$\begin{aligned} x &= 1 \pm \sqrt\varepsilon \\ &= 1 \pm 10^{-5} \end{aligned}$$
2. **Compare the sizes.** A change of $10^{-10}$ in a coefficient moved the roots by $10^{-5}$, a hundred thousand times more.
3. **No method avoids it.** This is a conditioning problem, so any method, however careful, faces it: rounding the coefficients to doubles already perturbs them by about $10^{-16}$, which moves a double root by about
   $$\sqrt{10^{-16}} = 10^{-8}$$
4. **In circuits.** Near-double roots, like the poles of a lightly damped resonance, come out with only half the digits.

**Answer:** the roots move to $1 \pm 10^{-5}$, a hundred thousand times more than the coefficient changed.
:::
:::

::: check
given: x = 1.001 | 1.1 | 2 | 10
answer: 1/abs(ln(x))
tolerance: 0.5%

## What's the condition number of computing $\ln x$ at x = {x}?

::: right
Right. $1/\lvert\ln x\rvert$ = {answer}.
:::

::: misses
- [1/abs(log(x))] Use the natural logarithm: $f' = 1/x$, so $xf'/f = 1/\ln x$.
- [any] Not yet. $\kappa = \lvert xf'(x)/f(x)\rvert$.
:::
:::

::: check
given: x = 0.5 | 20 | -50
answer: abs(x)
tolerance: 0.5%

## What's the condition number of computing $e^x$ at x = {x}?

::: right
Right. $\lvert x\rvert$ = {answer}.
:::

::: misses
- [exp(x)] That's the value; κ is $\lvert x\cdot e^x/e^x\rvert$.
- [any] Not yet. $xf'/f$ with $f' = f$.
:::
:::

::: check
given: k = 3 | 10 | 13
answer: 15.95 - k
tolerance: 1%

## A linear system has $\kappa(A) = 10^{\given{k}}$. About how many correct decimal digits can a backward-stable solver give in double?

::: right
Right. About $15.95 - \log_{10}\kappa$ = {answer}.
:::

::: misses
- [k] That's how many are lost.
- [any] Not yet. Double carries about 15.95 digits; subtract $\log_{10}\kappa$.
:::
:::

::: check
given: r = 1.01 | 1.1 | 3
answer: r/(r - 1)
tolerance: 0.5%

## What's the condition number of computing $x - a$ when x is {r} times a?

::: right
Right. $\left\lvert\frac{x}{x - a}\right\rvert = \frac{r}{r - 1}$ = {answer}.
:::

::: misses
- [1/(r - 1)] That's close, but κ compares relative changes: $\frac{x}{x - a}$, not $\frac{a}{x - a}$.
- [any] Not yet. $\kappa = \left\lvert\frac{x}{x - a}\right\rvert$.
:::
:::

::: check
given: x = 10 | 20 | 30
answer: x^x/x!
tolerance: 1%

## How large is the largest term of the series for $e^{-\given{x}}$, $\frac{x^x}{x!}$?

::: right
Right. {answer}: the size the errors are measured against, far above $e^{-x}$.
:::

::: misses
- [any] Not yet. The terms $x^k/k!$ peak at k = x.
:::
:::

::: check
kind: choice

## Two well-written solvers give answers that agree in only 4 digits on the same input, and perturbing the input in the 16th digit changes each answer in its 5th. What's the likely cause?

- [ ] One solver is unstable
- [x] The problem is ill-conditioned, with κ about $10^{11}$ or $10^{12}$
- [ ] Double precision is broken
- [ ] The answers are both exact

::: misses
- [1] If one were unstable, the other would still match the perturbed runs; here the problem itself is that sensitive.
- [3] It's working as designed.
- [4] They can't both be exact if they differ.
:::
:::

::: check
kind: parts
given: x = 1.5 | 1.55 | 1.56

## Evaluate $\tan x$ near its pole at $\pi/2$.

::: part
answer: 2*x/abs(sin(2*x))
tolerance: 0.5%

### What is its relative condition number at x = {x}?

::: misses
- [x/abs(tan(x))] $\kappa = \left\lvert\frac{xf'}{f}\right\rvert$ with $f' = \sec^2x$.
- [any] $\frac{x\sec^2x}{\tan x} = \frac{2x}{\sin 2x}$.
:::
:::

::: part
answer: log(2*x/abs(sin(2*x)))
tolerance: 1%

### About how many digits does that cost?

::: misses
- [any] $\log_{10}\kappa$.
:::
:::
:::

::: check
kind: derivation
vars: x = 0.1..0.9

## Find relative condition numbers $\kappa = \left\lvert\frac{xf'(x)}{f(x)}\right\rvert$.

::: step
answer: x/((1 + x)*ln(1 + x))

### For $f = \ln(1 + x)$?
:::

::: step
answer: 1/(1 - x)

### For $f = \frac{x}{1 - x}$?
:::

::: right
Right. The second blows up as x approaches 1, where the denominator cancels.
:::
:::

::: explain
## What's the difference between an ill-conditioned problem and an unstable algorithm?

::: model
Conditioning belongs to the problem: it says how much the exact answer changes when the input changes slightly, regardless of how the answer is computed. A problem with condition number κ loses about $\log_{10}\kappa$ digits to the unavoidable rounding of its input, and no method can recover them. Stability belongs to the algorithm: a stable one gives the exact answer to a problem only slightly different from the one posed, so its error is about κ times the roundoff, as good as possible. An unstable algorithm does worse than that, because it amplifies rounding errors in its own intermediate steps, as summing an alternating series or subtracting nearly equal partial results does. An ill-conditioned problem needs better data or a reformulation; an unstable algorithm needs a better algorithm.
:::
:::

::: practice

::: check
given: x = 1.01 | 1.1 | 2 | 5 | 10 | 100
answer: 1/abs(ln(x))
tolerance: 0.5%

## κ of $\ln x$ at {x}?

::: misses
- [any] $1/\lvert\ln x\rvert$.
:::
:::

::: check
given: k = 2..15 int
answer: 15.95 - k
tolerance: 1%

## Digits left with $\kappa = 10^{\given{k}}$?

::: misses
- [any] $15.95 - k$.
:::
:::

::: check
given: x = 2 | 3 | 10 | 20 | 50 | 100
answer: abs(x)
tolerance: 0.5%

## κ of $e^x$ at {x}?

::: misses
- [any] $\lvert x\rvert$.
:::
:::
:::
