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

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 R1 = 10 kΩ over R2 = 4.7 kΩ, loaded by RL, has output fraction H=R2∥RLR1+R2∥RL. 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 R2 shrinks.
  • With a light load, RL 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?

Name it

Numerical sensitivity

For a formula y=f(p1,p2,…), the sensitivity to pi is ∂ln⁡y∂ln⁡pi. Numerically: multiply pi by 1+δ and by 1−δ, and divide the change in ln⁡y by the change in ln⁡pi. With δ=10−6, that's accurate to about 10 digits.

Two predictions

With tolerances ti (as fractions):

  • worst case: ∑i|Si|ti;
  • root-sum-square: ∑i(Siti)2. If each part is uniform over ±ti, its standard deviation is ti/3, so the output's standard deviation is the root-sum-square divided by 3.

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 R1 = 10 kΩ, R2 = 4.7 kΩ and a 47 kΩ load. What are its sensitivities?

Show the solution
  1. Combine R2 and the load. R2∥RL=4.27kΩ
  2. Ratio. H=4.2714.27=0.299
  3. Sensitivities, found numerically. They are −0.701 to R1, 0.637 to R2 and 0.064 to RL.
  4. Check. They sum to zero, as they must: scaling every resistor by the same factor leaves a ratio unchanged.

Answer the sensitivities are −0.701 to R1, 0.637 to R2 and 0.064 to RL.

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
  1. Worst case. Add each sensitivity times its tolerance: 0.701×1+0.637×1+0.064×5=1.66%
  2. Root-sum-square. 0.7012+0.6372+0.3182=1.00%
  3. 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 ln⁡f when p[i] is multiplied by 1± rel. Test it on an RC corner, f=1/(2πRC) 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 1+ random.uniform(-t, t) with tolerances 1%, 1% and 5%. Set spread to the standard deviation of the output's fractional error H/H0−1, 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?

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.