A752 of 56
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A7 · Lesson 52 · Conditioning

When a correct algorithm gives a wrong answer

Fit a polynomial of degree 11 through 12 calibration points and solve for its coefficients with a perfectly good solver: the coefficients can come out wrong in every digit, even though plugging them back in reproduces the data almost exactly. Nothing is broken. Some problems are ill-conditioned: tiny changes in the data, even rounding at the sixteenth digit, produce large changes in the answer. A single number, the condition number, tells you in advance how many digits you can trust.

  • The two lines are x+y=2 and x+(1+e)y=2+δ. When e is small they're nearly parallel.
  • δ is a small error in the right-hand side, such as a measurement error. Watch where the crossing point goes as you change it.
  • Shrink e. The same small δ moves the solution much farther. The readout gives the condition number.

Predict

A matrix has condition number 1010. Solving with it in double precision (about 16 digits), roughly how many correct digits can you expect?

Name it

The condition number

For a square invertible matrix,

κ(A)=σmax⁡σmin⁡=‖A‖‖A−1‖

If 𝐛 changes by a relative amount ε, the solution of A𝐱=𝐛 can change by up to κε:

‖Δ𝐱‖‖𝐱‖≤κ(A)‖Δ𝐛‖‖𝐛‖

κ≥1 always; orthogonal matrices have κ=1, perfectly conditioned. Conditioning is a property of the problem, not of the algorithm: no method can recover digits the data doesn't determine.

Stability is a property of the algorithm

A stable algorithm gives the exact answer to a nearby problem. Elimination with partial pivoting, QR and the SVD are stable; elimination without pivoting is not (a tiny pivot creates huge multipliers, lesson 9). A stable algorithm on a well-conditioned problem gives an accurate answer. On an ill-conditioned problem even a stable algorithm loses about log10⁡κ digits.

Residual and error

The residual 𝐫=𝐛−A𝐱^ is easy to compute; the error 𝐱^−𝐱 is what you care about. A stable solver always makes the residual tiny. For an ill-conditioned A that says little about the error: nearly parallel lines meet almost anywhere along their length while staying very close to both.

Familiar trouble spots

  • Hilbert matrices, hij=1i+j−1: κ≈1013 at size 10.
  • Polynomial fitting in the basis 1,x,x2,… (Vandermonde matrices) on many points; orthogonal polynomials fix it.
  • Normal equations ATA: κ(ATA)=κ(A)2, which is why QR is preferred (lesson 28).
  • Circuits with resistances spanning many orders of magnitude, like 1 mΩ next to 1 GΩ.

Worked example

How sensitive is the system x+y=2, x+1.001y=2 to errors in 𝐛?

Show the solution
  1. The solution. It's (2,0).
  2. Nudge 𝐛. Change the second right side to 2.001 and the solution becomes (1,1): a change of 0.04% in 𝐛 moved the solution by about 70% of its size.
  3. Condition number. The condition number of [1111.001] is about 4000, so an amplification of about two thousand is within what it allows.

Answer very sensitive: a 0.04% change in 𝐛 moves the solution by about 70%, and the condition number is about 4000.

Worked example

Solve H𝐱=H𝟏 with the 10 × 10 Hilbert matrix. How accurate is the answer?

Show the solution
  1. Condition number. np.linalg.cond gives κ=1.6×1013.
  2. Residual and error. Solving gives a residual of about 4×10−16, as small as rounding allows, yet the largest error in 𝐱 is about 3×10−4.
  3. Digits lost. That's roughly 13 of the 16 digits gone, as log10⁡κ=13.2 predicts.
  4. At size 12. There κ≈2×1016 and the answer has no correct digits at all.

Answer only about 3 digits are right: the largest error is about 3×10−4, even though the residual is about 4×10−16.

Worked example

A solver works in double precision on a matrix with condition number 106. About how many digits of the answer can you trust?

Show the solution
  1. Precision. Double precision carries about 16 significant digits.
  2. Relative error. A stable solver's relative error is about the condition number times the rounding unit: 106×10−16=10−10 so roughly 10 digits are right.
  3. The residual. |A𝐱^−𝐛| will still look tiny, near 10−16 relative to |𝐛|. It measures how well the answer fits the equations, and the condition number turns that into how close the answer is.

Answer about 10 digits.

Check

A matrix has condition number 1 × 10¹⁰. In double precision (16 digits), about how many correct digits does solving with it leave?

Check

A matrix's largest singular value is 50 and its smallest is 0.5. What is its condition number?

Check

κ(A) = 100 and 𝐛 has a 0.1% error. What is the largest possible percent error in 𝐱?

Check

κ(A) = 100000. What is the condition number of ATA?

Check

Solve x+y=2, x+(1+0.01)y=2+0.02. What is y?

Check

Build the 8 × 8 Hilbert matrix H (hij=1i+j+1 with 0-based indices), set kappa to its condition number, solve H @ x = H @ ones with np.linalg.solve, and set err to the largest error in the solution and res to the largest entry of the residual.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Solve x+y=2, x+(1+0.01)y=2+0.001.

(a)

What is y?

(b)

What is x?

Check

A stable solver runs in double precision on a matrix with condition number k.

Step 1

With a rounding unit of 1.1×10−16, about how large is the relative error?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

A solver returns 𝐱^ with a residual of 10−16. Why might 𝐱^ still be badly wrong?

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.