DC Sweeps, Sensitivity and Tolerance Analysis
A divider that gives exactly on paper gives on one board and on the next, because real resistors are parts and the supply is only good to half a percent. If that voltage is an ADC reference, every reading inherits the error. The questions an engineer must answer are: how does the output depend on each part (sensitivity), how far can it go in the worst case, and how far does it go on a typical board (statistics). This lesson answers all three with ngspice sweeps and a few lines of NumPy, and links them back to the nodal solution you already know how to write.
The circuit
The output is a plain divider (Voltage and Current Dividers):
DC sweeps
A DC sweep repeats the operating-point solution while one source or component steps through a range. In ngspice it is the dc command: dc V1 0 5 1 steps from 0 to 5 V in 1 V steps; dc R2 14.85k 15.15k 0.15k steps a resistor. The sweep shows linearity at a glance and is the first tool for "what if this part changes".
cat > sweep.cir <<'EOF'
* ADC reference divider: sweep the supply, then R2 over its 1 % band
V1 in 0 DC 5
R1 in out 10k
R2 out 0 15k
.control
dc V1 0 5 1
print v(out)
dc R2 14.85k 15.15k 0.15k
print v(out)
.endc
.end
EOF
ngspice -b sweep.cir 2>&1 | awk '/^Index/ || /^[0-9]+\t/ {print $1, $2, $3}'Index v-sweep v(out)
0 0.000000e+00 0.000000e+00
1 1.000000e+00 6.000000e-01
2 2.000000e+00 1.200000e+00
3 3.000000e+00 1.800000e+00
4 4.000000e+00 2.400000e+00
5 5.000000e+00 3.000000e+00
Index res-sweep v(out)
0 1.485000e+04 2.987928e+00
1 1.500000e+04 3.000000e+00
2 1.515000e+04 3.011928e+00
The supply sweep is a straight line of slope : a linear circuit. The resistor sweep moves the output by about for in , so 's tolerance alone is worth about of the output.
Sensitivity
The normalised sensitivity of an output to a parameter is the percentage change of per percentage change of :
For the divider, differentiating :
So in moves the output by , in agreement with the sweep (). Sensitivities tell you where to spend money: here a better supply matters more than better resistors. Note that : scaling both resistors together changes nothing, which is why matched resistor networks (one package, tracking temperature coefficients) make excellent dividers.
Worst case and statistics
Worst-case analysis sets every parameter to the end of its tolerance band that pushes the output the same way. To first order the relative error is
Statistical analysis assumes the errors are independent. If each part's error has standard deviation (a common assumption for a tolerance band is ), the output's relative standard deviation is the root-sum-square
and covers about 99.7 % of boards, versus for the worst case. Monte Carlo analysis checks this by simulating many random boards; it needs no linearisation, so it also works when sensitivities change across the band.
Worked example
Given: , , .
Find: the worst-case range of exactly, and compare with the linear estimate.
Solve: the output is highest for high , low , high :
Check: and , the linear estimate to three figures. The ngspice corner run below confirms both extremes.
cat > corners.cir <<'EOF'
* Worst-case corners: V1 +-0.5 %, R1 and R2 +-1 %
V1 in 0 DC 5
R1 in out 10k
R2 out 0 15k
.control
foreach v 4.975 5.025
foreach a 9.9k 10.1k
foreach b 14.85k 15.15k
alter V1 dc = $v
alter R1 = $a
alter R2 = $b
op
echo "V1=$v R1=$a R2=$b v(out)=amp;v(out)"
end
end
end
.endc
.end
EOF
ngspice -b corners.cir 2>&1 | grep '^V1='V1=4.975 R1=9.9k R2=14.85k v(out)=2.985
V1=4.975 R1=9.9k R2=15.15k v(out)=3.00883
V1=4.975 R1=10.1k R2=14.85k v(out)=2.96107
V1=4.975 R1=10.1k R2=15.15k v(out)=2.985
V1=5.025 R1=9.9k R2=14.85k v(out)=3.015
V1=5.025 R1=9.9k R2=15.15k v(out)=3.03907
V1=5.025 R1=10.1k R2=14.85k v(out)=2.99083
V1=5.025 R1=10.1k R2=15.15k v(out)=3.015
alter changes a component between runs and foreach loops over values; the eight corners are every combination of the three tolerance ends. In a circuit where the sensitivities can change sign inside the band, the extremes need not be at the corners, and Monte Carlo is the safer check.
Sensitivity and Monte Carlo in Python
import numpy as np
import matplotlib.pyplot as plt
def vout(V1, R1, R2):
return V1 * R2 / (R1 + R2)
nom = {"V1": 5.0, "R1": 10e3, "R2": 15e3}
tol = {"V1": 0.005, "R1": 0.01, "R2": 0.01}
y0 = vout(**nom)
# normalised sensitivities by a central difference of +-0.1 %
S = {}
for k in nom:
up, dn = dict(nom), dict(nom)
up[k] *= 1.001; dn[k] *= 0.999
S[k] = (vout(**up) - vout(**dn)) / y0 / 0.002
print(f"S_{k} = {S[k]:+.4f}")
wc = sum(abs(S[k]) * tol[k] for k in nom)
rss = np.sqrt(sum((S[k] * tol[k] / 3) ** 2 for k in nom))
print(f"linear worst case: +-{100 * wc:.3f} %, sigma (t/3 rule): {100 * rss:.3f} %")
rng = np.random.default_rng(2026)
n = 100_000
draw = {k: nom[k] * (1 + tol[k] / 3 * rng.standard_normal(n)) for k in nom}
y = vout(**draw)
print(f"Monte Carlo: mean {y.mean():.4f} V, sigma {1000 * y.std():.2f} mV ({100 * y.std() / y0:.3f} %)")
print(f"boards outside +-1 %: {100 * np.mean(np.abs(y / y0 - 1) > 0.01):.3f} %")
plt.hist(y, bins=80)
plt.axvline(y0 * (1 - wc), linestyle="--"); plt.axvline(y0 * (1 + wc), linestyle="--")
plt.xlabel("V_out (V)"); plt.ylabel("boards"); plt.title("100 000 random dividers; dashed: worst case")
plt.show()S_V1 = +1.0000
S_R1 = -0.4000
S_R2 = +0.4000
linear worst case: +-1.300 %, sigma (t/3 rule): 0.252 %
Monte Carlo: mean 3.0000 V, sigma 7.55 mV (0.252 %)
boards outside +-1 %: 0.004 %
The histogram is a narrow bell well inside the worst-case lines. The Monte Carlo puts 4 boards in 100 000 outside . Whether that is good enough depends on how the parts are really distributed: tolerance bands are a manufacturer's guarantee, not a Gaussian, and parts sorted from a batch can sit near the band edges. Use the worst case for safety-relevant limits, statistics for yield estimates.
Interactive: the divider with a load
The ADC reference input is not infinite resistance. Add its input resistance as in the netlist lab and sweep it: the loaded output drops, a systematic error that tolerance analysis of and alone never shows.
With the lab shows , already low, almost half the worst-case budget; at it falls to . A buffer or a stiffer divider fixes it.
Guided lab: measure a batch of dividers
Hand calculation. Nominal ; worst case to ; expected spread if the parts behave like the model.
Simulation. The two ngspice sessions and the Python Monte Carlo above.
What you would measure. Build five dividers from one bag of and 1 % resistors, all fed from one supply (20 mA limit). Measure at the board and each with a 5½-digit or a good 4½-digit DMM ( or so on the 10 V range). Report rather than , which removes the supply error: expect within (the resistor-only worst case), and usually within about . Also measure each resistor out of circuit and check that the computed ratio matches the in-circuit one.
Hazards and safe setup. 5 V, current-limited, in the resistors. Measure resistors out of circuit only with the supply off.
Pitfalls
- Adding percentages as if errors were correlated gives the worst case, adding them in quadrature gives a statistical spread; say which one you quote.
- Ignoring loading: the meter, the ADC and any next stage are resistors too.
- Forgetting temperature: a 100 ppm/K resistor drifts over , more than its tolerance at would suggest; matched networks track.
- Dates in SPICE output: ngspice prints a date and run statistics; filter them out (
awk,grep) when you compare runs. - Monte Carlo without a seed is not reproducible; fix the seed in reports.
Practice
- Redesign the divider with and . What are the sensitivities now?
- Replace the 1 % resistors by 0.1 % parts. What is the linear worst case?
- A divider has . What does that say about compared with , and is such a divider sensitive to its resistors?
Answer
- Unchanged: , , . Only the ratio matters (the loading by gets worse with larger values).
- : the supply dominates.
- , so : a large division ratio (). Each 1 % in a resistor moves the output 0.9 %, so high-ratio dividers need tighter resistors.
Test yourself
The normalised sensitivity means…
- a 1 % increase of raises by about 0.4 %
- changes by 0.4 V per volt of
- has 0.4 % tolerance
- is 40 % of
Three independent errors contribute 0.5 %, 0.4 % and 0.4 % to an output. What are the worst-case and the root-sum-square values?
- 1.3 % and about 0.76 %
- 1.3 % and 1.3 %
- 0.5 % and 0.4 %
- 0.76 % and 1.3 %
Which ngspice command steps a resistor through a range of values and re-solves the operating point at each?
tran R2 …dc R2 14.85k 15.15k 0.15kac R2 …op R2
Why do and of a divider add to zero?
- because the supply is ideal
- because the output depends only on the ratio
- because both resistors have the same tolerance
- because the divider is unloaded