TExcelTAD
Home › Numerical Methods › Conditioning of Linear Systems

Conditioning of Linear Systems

How much can the solution of Ax=bA\mathbf{x} = \mathbf{b} change when b\mathbf{b} or AA change slightly, for example by measurement or rounding errors? The answer is governed by one number, the condition number κ(A)=∥A∥ ∥A−1∥\kappa(A) = \lVert A\rVert\,\lVert A^{-1}\rVert. It tells you how many digits of the solution to trust, before any algorithm is chosen.

The bound

If A(x+δx)=b+δbA(\mathbf{x} + \delta\mathbf{x}) = \mathbf{b} + \delta\mathbf{b}, then δx=A−1δb\delta\mathbf{x} = A^{-1}\delta\mathbf{b} and ∥b∥≤∥A∥ ∥x∥\lVert\mathbf{b}\rVert \le \lVert A\rVert\,\lVert\mathbf{x}\rVert, so

∥δx∥∥x∥≤κ(A) ∥δb∥∥b∥.\frac{\lVert\delta\mathbf{x}\rVert}{\lVert\mathbf{x}\rVert} \le \kappa(A)\,\frac{\lVert\delta\mathbf{b}\rVert}{\lVert\mathbf{b}\rVert}.

A similar bound holds for perturbations of AA: ∥δx∥∥x∥≲κ(A)∥δA∥∥A∥\frac{\lVert\delta\mathbf{x}\rVert}{\lVert\mathbf{x}\rVert} \lesssim \kappa(A)\frac{\lVert\delta A\rVert}{\lVert A\rVert}. In the 2-norm, κ2(A)=σmax⁡σmin⁡\kappa_2(A) = \frac{\sigma_{\max}}{\sigma_{\min}}; always κ≥1\kappa \ge 1, and κ=∞\kappa = \infty for a singular matrix.

Rule of thumb. A backward stable solver in double precision gives a relative error of about κ(A)⋅10−16\kappa(A) \cdot 10^{-16}: you lose log⁡10κ(A)\log_{10}\kappa(A) digits.

The Hilbert matrix

The Hilbert matrix Hij=1i+j−1H_{ij} = \frac{1}{i + j - 1} is a classic ill-conditioned example. Solve with a known solution of all ones and watch the digits disappear:

python · try it yourself
import numpy as np
from scipy.linalg import hilbert

for n in (4, 8, 12, 16):
    H = hilbert(n)
    x_true = np.ones(n)
    b = H @ x_true
    x = np.linalg.solve(H, b)
    err = np.linalg.norm(x - x_true) / np.linalg.norm(x_true)
    res = np.linalg.norm(b - H @ x) / np.linalg.norm(b)
    print(f"n = {n:2d}: cond = {np.linalg.cond(H):.1e}, relative error = {err:.1e}, relative residual = {res:.1e}")
n =  4: cond = 1.6e+04, relative error = 3.9e-13, relative residual = 4.1e-17
n =  8: cond = 1.5e+10, relative error = 2.3e-07, relative residual = 1.1e-16
n = 12: cond = 1.8e+16, relative error = 1.8e-01, relative residual = 1.5e-16
n = 16: cond = 4.3e+17, relative error = 2.1e+00, relative residual = 1.2e-16

The error follows κ⋅10−16\kappa \cdot 10^{-16} until no digits are left, yet the residual stays tiny: the solver is backward stable, and the computed x\mathbf{x} exactly solves a nearby system. A small residual does not mean a small error when κ\kappa is large.

Residual versus error

∥x−x~∥∥x∥≤κ(A)∥b−Ax~∥∥b∥.\frac{\lVert\mathbf{x} - \tilde{\mathbf{x}}\rVert}{\lVert\mathbf{x}\rVert} \le \kappa(A)\frac{\lVert\mathbf{b} - A\tilde{\mathbf{x}}\rVert}{\lVert\mathbf{b}\rVert}.

So a residual check certifies the solution only if κ(A)\kappa(A) is known to be moderate.

Estimating κ\kappa cheaply

Computing A−1A^{-1} or the SVD to get κ\kappa costs more than the solve itself. LAPACK estimates ∥A−1∥1\lVert A^{-1}\rVert_1 from the LU factors in O(n2)O(n^2) (Hager's / Higham's estimator):

python · try it yourself
import numpy as np
import scipy.linalg as sla
from scipy.linalg import hilbert

rng = np.random.default_rng(6)
for name, A in [("random 200x200", rng.normal(size=(200, 200))), ("Hilbert 10", hilbert(10))]:
    lu, piv = sla.lu_factor(A)
    rcond = sla.lapack.dgecon(lu, np.linalg.norm(A, 1), norm="1")[0]
    print(f"{name:15s}: estimate 1/rcond = {1 / rcond:.3e}, exact cond_1 = {np.linalg.cond(A, 1):.3e}")
random 200x200 : estimate 1/rcond = 1.478e+04, exact cond_1 = 1.478e+04
Hilbert 10     : estimate 1/rcond = 3.536e+13, exact cond_1 = 3.535e+13

The estimate is usually within a small factor, which is all that matters for counting digits.

Remedies for ill-conditioning

  • Rescale rows and columns (equilibration) when the trouble comes from units: κ\kappa of diag⁡(106,1)\operatorname{diag}(10^6, 1) is 10610^6 but trivially fixable.
  • Reformulate: avoid normal equations (Normal Equations), monomial bases (Polynomial Interpolation).
  • Regularize: replace AA by ATA+λIA^TA + \lambda I (ridge) or truncate small singular values.

In software

  • np.linalg.cond(A, p) computes the exact condition number (via SVD for p=2p = 2).
  • scipy.linalg.solve warns with LinAlgWarning: Ill-conditioned matrix (rcond=…) when the estimated reciprocal condition number is below machine precision.

Exercises

Exercise 1: Digits

κ(A)=3×107\kappa(A) = 3 \times 10^{7}. How many correct digits can you expect from np.linalg.solve?

Answer

About 8 to 9.

Solution

3×107⋅1.1×10−16≈3×10−93 \times 10^{7} \cdot 1.1 \times 10^{-16} \approx 3 \times 10^{-9}.

Exercise 2: Condition of a diagonal matrix

What is κ2\kappa_2 of diag⁡(4,0.5,2)\operatorname{diag}(4, 0.5, 2)?

Answer
Solution

The singular values are 4,2,0.54, 2, 0.5; κ=40.5=8\kappa = \frac{4}{0.5} = 8.

Exercise 3: Sensitivity experiment (code)

For A=(1111.0001)A = \begin{pmatrix} 1 & 1 \\ 1 & 1.0001\end{pmatrix} and b=(2,2.0001)\mathbf{b} = (2, 2.0001), perturb b2b_2 by 10−410^{-4} and compare the relative change in x\mathbf{x} with κ(A)\kappa(A) times the relative change in b\mathbf{b}.

Answer

The solution changes from (1,1)(1, 1) to (0,2)(0, 2): relative change 11, while κ⋅∥δb∥∥b∥≈1.4\kappa \cdot \frac{\lVert\delta\mathbf{b}\rVert}{\lVert\mathbf{b}\rVert} \approx 1.4.

Solution
python · try it yourself
import numpy as np
A = np.array([[1.0, 1.0], [1.0, 1.0001]])
b = np.array([2.0, 2.0001]); db = np.array([0.0, 1e-4])
x = np.linalg.solve(A, b); x2 = np.linalg.solve(A, b + db)
print(np.round(x, 6) + 0, np.round(x2, 6) + 0)
rel_x = np.linalg.norm(x2 - x) / np.linalg.norm(x)
bound = np.linalg.cond(A) * np.linalg.norm(db) / np.linalg.norm(b)
print(f"cond {np.linalg.cond(A):.4e}, relative change in x {rel_x:.4f}, bound {bound:.4f}")
[1. 1.] [0. 2.]
cond 4.0002e+04, relative change in x 1.0000, bound 1.4142

Test yourself

The condition number of AA in the 2-norm is…

  1. ∣det⁡A∣\lvert\det A\rvert
  2. σmax⁡/σmin⁡\sigma_{\max}/\sigma_{\min}
  3. ∥A∥2\lVert A\rVert_2
  4. the number of rows

A tiny residual ∥b−Ax~∥\lVert\mathbf{b} - A\tilde{\mathbf{x}}\rVert guarantees a small error when…

  1. always
  2. κ(A)\kappa(A) is moderate
  3. AA is large
  4. never

Is a small determinant a sign of ill-conditioning?

  1. Yes, always
  2. No; det⁡(0.1I20)=10−20\det(0.1 I_{20}) = 10^{-20} but κ=1\kappa = 1
  3. Only for symmetric matrices
  4. Only if negative