A5 · Lesson 23 · Lab: tolerance analysis in code
Sensitivities, worst cases and a thousand random boards
A designer choosing between 1% and 5% resistors needs to know how far the circuit's output will wander. The sensitivity lesson gave two estimates: the worst case, where every part is at its limit in the harmful direction, and a root-sum-square estimate for random errors. In this lab you'll compute sensitivities numerically for any circuit formula, make both estimates, and then check them by building twenty thousand random boards in software.
- A divider of = 10 kΩ over = 4.7 kΩ, loaded by , has output fraction . The curves are the three parts' sensitivities against the load, in kΩ.
- Move the marked load lower. The load starts to matter: its sensitivity grows while that of shrinks.
- With a light load, barely matters even at 5% tolerance: its sensitivity is small.
Predict
Three parts have sensitivities 0.7, 0.64 and 0.064, with tolerances 1%, 1% and 5%. Twenty thousand random boards are built with each part uniformly distributed within its tolerance. About how big is the spread (standard deviation) of the output?
About 0.58%. A part spread uniformly over has a standard deviation of , not , so the root-sum-square of the tolerances, 1.0%, divided by gives the expected spread, 0.58%. The worst case, 1.7%, is reached only when every part sits at its limit in the harmful direction at once, which almost never happens.
Name it
Numerical sensitivity
For a formula , the sensitivity to is . Numerically: multiply by and by , and divide the change in by the change in . With , that's accurate to about 10 digits.
Two predictions
With tolerances (as fractions):
- worst case: ;
- root-sum-square: . If each part is uniform over , its standard deviation is , so the output's standard deviation is the root-sum-square divided by .
Monte Carlo
Draw each part's value at random within its tolerance, compute the output, and repeat many times. The spread of the results is the real answer for that model of the parts, including any nonlinearity the sensitivities miss. random.seed makes the run repeatable, and random.uniform(-t, t) draws one part's fractional error.
Worked example
A divider has = 10 kΩ, = 4.7 kΩ and a 47 kΩ load. What are its sensitivities?
Show the solution
- Combine and the load.
- Ratio.
- Sensitivities, found numerically. They are to , to and to .
- Check. They sum to zero, as they must: scaling every resistor by the same factor leaves a ratio unchanged.
Answer the sensitivities are to , to and to .
Worked example
With tolerances of 1%, 1% and 5% on those parts, what are the worst-case and root-sum-square errors?
Show the solution
- Worst case. Add each sensitivity times its tolerance:
- Root-sum-square.
- Why the load adds little. The load's 5% tolerance adds little, because its sensitivity is small.
Answer the worst-case error is 1.66% and the root-sum-square error is 1.00%.
Check
Three parts have sensitivities 0.7, 0.5 and 0.25, with tolerances 1%, 1% and 5%. What is the worst-case error, in percent?
Check
The root-sum-square of the sensitivity-weighted tolerances is 1%. If every part is uniform within its tolerance, what standard deviation do you expect for the output, in percent?
Check
Write sens(f, p, i, rel=1e-6) returning the sensitivity of f(p) to p[i], from the change in when p[i] is multiplied by rel. Test it on an RC corner, with p = [R, C].
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
For the loaded divider in the starter, set S to the list of sensitivities to R1, R2 and RL (use your sens), then wc and rss to the worst-case and root-sum-square errors as fractions, with tolerances of 1%, 1% and t3 = 5% on the load.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Build n = 8000 random boards with random.seed(2): each part is its nominal value times random.uniform(-t, t) with tolerances 1%, 1% and 5%. Set spread to the standard deviation of the output's fractional error , and worst to the largest absolute error seen.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Which part should get a tighter tolerance? For each part in turn, cut its tolerance to a tenth (1% to 0.1%, or 5% to 0.5%) and recompute the root-sum-square error. Set best to the index (0, 1 or 2) of the part whose tightening helps most, and gain to how much the root-sum-square falls, as a fraction.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
A designer finds that the worst-case error of a circuit is 3%, but a Monte Carlo run of 10,000 boards never shows more than 1.4%. Which number should go in the specification?
It depends on what the specification promises. The worst case is a guarantee: it covers every combination of parts within their tolerances, including the rare one where all errors line up in the harmful direction. If every unit shipped must meet the spec, for example in a safety or calibration circuit, the worst case is the honest number, perhaps with margin for temperature and aging. The Monte Carlo result describes typical boards: with independent random errors, combinations near the worst case are so unlikely that 10,000 samples rarely see one. If the spec is statistical, such as 99.7% of units within a limit, the simulated distribution (about three standard deviations) is the right basis and the worst case would over-design the circuit. Either way, the simulation is only as good as its model of the parts: if part errors are correlated, for instance resistors from one reel all running high, the true spread is larger than the independent model predicts.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.