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 and . When 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 . The same small moves the solution much farther. The readout gives the condition number.
Predict
A matrix has condition number . Solving with it in double precision (about 16 digits), roughly how many correct digits can you expect?
About 6. Rounding errors of relative size can be amplified by up to , giving relative errors near . A useful rule: you lose about digits.
Name it
The condition number
For a square invertible matrix,
If changes by a relative amount , the solution of can change by up to :
always; orthogonal matrices have , 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 digits.
Residual and error
The residual is easy to compute; the error is what you care about. A stable solver always makes the residual tiny. For an ill-conditioned 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, : at size 10.
- Polynomial fitting in the basis (Vandermonde matrices) on many points; orthogonal polynomials fix it.
- Normal equations : , 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 , to errors in ?
Show the solution
- The solution. It's .
- Nudge . Change the second right side to and the solution becomes : a change of 0.04% in moved the solution by about 70% of its size.
- Condition number. The condition number of 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 with the 10 × 10 Hilbert matrix. How accurate is the answer?
Show the solution
- Condition number.
np.linalg.condgives . - Residual and error. Solving gives a residual of about , as small as rounding allows, yet the largest error in is about .
- Digits lost. That's roughly 13 of the 16 digits gone, as predicts.
- At size 12. There and the answer has no correct digits at all.
Answer only about 3 digits are right: the largest error is about , even though the residual is about .
Worked example
A solver works in double precision on a matrix with condition number . About how many digits of the answer can you trust?
Show the solution
- Precision. Double precision carries about 16 significant digits.
- Relative error. A stable solver's relative error is about the condition number times the rounding unit: so roughly 10 digits are right.
- The residual. will still look tiny, near 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
= 100 and has a 0.1% error. What is the largest possible percent error in ?
Check
= 100000. What is the condition number of ?
Check
Solve , . What is ?
Check
Build the 8 × 8 Hilbert matrix H ( 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 , .
(a)
What is ?
(b)
What is ?
Check
A stable solver runs in double precision on a matrix with condition number .
Step 1
With a rounding unit of , 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°.
Step 2
About how many correct digits remain?
Explain why
A solver returns with a residual of . Why might still be badly wrong?
A tiny residual means is very close to , so exactly solves a slightly perturbed problem. If is ill-conditioned, very different vectors give almost the same : for nearly parallel lines, points far apart along the lines are all close to both. So a small residual can coexist with a large error, up to about times the relative residual. Only a well-conditioned matrix turns a small residual into a small error.
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.