---
title: Resistor networks lab
heading: Series and parallel in code, standard resistor values, choosing a divider, tolerance by Monte Carlo, and ladder networks, in Python
eyebrow: C1 · Lesson 12 · Resistor networks lab
xp: 50
---

::: hook
Lessons 5 to 11 reduced networks by hand: series and parallel, dividers, real resistors with tolerances, loading, nodal and mesh analysis, delta–wye. A designer does the same reductions in code, and adds the questions hand analysis avoids: which standard values come closest to a ratio, how far a batch of 5% resistors can push a divider, and what an endless ladder converges to. In this lab you'll write those small tools, and check each against the formulas from the lessons.
:::

::: play
- Each sample is the output of a 10 kΩ over 10 kΩ divider from 5 V, built from two resistors drawn at random within ±t%. The histogram collects 5000 of them.
- The curve is a Gaussian with the spread predicted from the tolerance, $\sigma = \frac{5\sqrt2}{4}\frac{t}{\sqrt3}$ for uniform errors.
- Halve the tolerance and the spread halves. Step 4 does the same Monte Carlo in Python, for any divider.
:::

```sim plot
view: random
x: v = 2.2..2.8 V
slider: t = 0.5..10 step 0.5 = 5 %
sample: 5/(1 + (1 + t/100*(2*rand(1) - 1))/(1 + t/100*(2*rand(1) - 1))) | sd: S
trials: 5000
bins: 50
density: exp(-(v - 2.5)^2/(2*(5*sqrt(2)/4*t/100/sqrt(3))^2))/(5*sqrt(2)/4*t/100/sqrt(3)*sqrt(2*pi))
readout: measured spread, V = S
readout: predicted spread, V = 5*sqrt(2)/4*t/100/sqrt(3)
```

::: predict
## A 5 V to 3.3 V divider must be built from standard E24 values (1.0, 1.1, 1.2, … 9.1 and their multiples of ten). How close can two resistors from the first two decades get to the ratio 0.66?

- [ ] Only within about 5%
- [ ] Within about 1%
- [x] Within about 0.1%
- [ ] Exactly

::: reveal
Within about 0.1%: 4.7 over 9.1 (or 4.7 kΩ and 9.1 kΩ) gives 0.6594, 0.09% short of 0.66. With 576 pairs to choose from, a near-perfect ratio usually exists, but finding it by hand is tedious. Step 3 searches all pairs.
:::
:::

::: nameit
## Series and parallel in code

Series resistances add; parallel ones add as conductances, $R = \left(\sum 1/R_i\right)^{-1}$. Written as small functions, they combine into any series–parallel network in one line, the way the lessons reduced them on paper.

## Standard values

Resistors come in E-series, with N values per decade spaced about $10^{1/N}$ apart: E12 for 10% parts, E24 for 5%, E96 for 1%. The nearest standard value to x is the one closest on a log scale, since the series is evenly spaced in log.

## Choosing a divider

For a target ratio $\frac{R_2}{R_1 + R_2}$, search every pair of standard values and keep the one with the smallest error. Two decades of E24 give 48 values each way, so 2304 pairs, which a computer checks instantly.

## Tolerance by Monte Carlo

A ±t% resistor can be anywhere in its band. Drawing each resistor at random within its tolerance, many times, gives the distribution of the output. For a divider of equal resistors with uniform errors, the output's standard deviation is $\frac{V}{4}\sqrt2\frac{t}{\sqrt3}$; Monte Carlo handles any network and any error distribution without new algebra.

## Ladders

A ladder of series R and shunt R, n sections long, has input resistance given by repeating $r \leftarrow R + R\,\|\,r$. As n grows it converges to the value that reproduces itself, $r = R + R\,\|\,r$, which is $\frac{1 + \sqrt5}{2}R$ = 1.618R.
:::

::: example
## Find the E24 pair, from 1.0 to 91, that makes the best 5 V to 3.3 V divider. What's its error, and what does it actually output?

::: solution
1. **Search all pairs.** The ratio $\frac{R_2}{R_1 + R_2}$ closest to 0.66 comes from $R_1$ = 4.7 and $R_2$ = 9.1 (in any unit, say kΩ):
   $$\frac{9.1}{4.7 + 9.1} = 0.65942$$
   an error of 0.00058.
2. **Output.** From exactly 5 V:
   $$5 \times 0.65942 = 3.297\ \text{V}$$
3. **An exact target.** For other targets the search can be exact: 1.8 V from 3.3 V needs 0.5455, which 1.0 over 1.2 hits exactly, since
   $$\frac{1.2}{2.2} = \frac{6}{11}$$
4. **Check the pair.** A designer would then check the pair's total resistance against the load and the power budget.

**Answer:** 4.7 over 9.1 (say kΩ) is the best pair, with a ratio of 0.65942, an error of 0.00058 and an output of 3.297 V.
:::
:::

::: example
## A 10 kΩ over 10 kΩ divider from 5 V uses ±5% resistors. What's the spread of its output, by Monte Carlo and by formula?

::: solution
1. **Monte Carlo.** Drawing both resistors uniformly within ±5% 200 000 times gives a mean of 2.500 V and a standard deviation of 0.0510 V.
2. **Each resistor's spread.** Each resistor's standard deviation is
   $$\frac{5\%}{\sqrt3} = 2.89\%$$
3. **Combine by formula.** The output depends on their ratio with sensitivity ¼ each, and the two add in quadrature:
   $$\begin{aligned} \sigma &= 5 \times \frac14 \times \sqrt2 \times 0.02887 \\ &= 0.0510\ \text{V} \end{aligned}$$
   The formula agrees with the Monte Carlo result.
4. **Worst case.** With both resistors at opposite extremes the output is off by ±0.125 V, nearly 2.5σ. Monte Carlo shows how rarely that corner occurs.

**Answer:** the output has a mean of 2.500 V and a standard deviation of 0.0510 V by both methods, and the worst case is ±0.125 V.
:::
:::

::: example
## A ladder alternates 1 kΩ in series and 1 kΩ to ground, section after section. What's its input resistance after one, two and many sections?

::: solution
1. **One section.**
   $$1 + 1 = 2\ \text{k}\Omega$$
2. **Two sections.**
   $$1 + 1\,\|\,2 = 1.667\ \text{k}\Omega$$
3. **Three sections.**
   $$1 + 1\,\|\,1.667 = 1.625\ \text{k}\Omega$$
4. **Many sections.** The values close in quickly on the golden ratio, since r must equal $1 + 1\,\|\,r$:
   $$\begin{aligned} r &= \frac{1 + \sqrt5}{2}\ \text{k}\Omega \\ &= 1.618\ \text{k}\Omega \end{aligned}$$
5. **Transmission lines.** A long transmission line, modeled as such a ladder, presents its characteristic impedance in the same way, which C10 takes up.

**Answer:** the input resistance is 2 kΩ after one section, 1.667 kΩ after two, 1.625 kΩ after three, and 1.618 kΩ (the golden ratio) after many.
:::
:::

::: check
kind: python

## Step 1. Write `series(*r)` and `parallel(*r)` returning the combined resistance of any number of resistors.

```python starter
def series(*r):
    return 0.0

def parallel(*r):
    return 0.0

print(series(1, 2, 3), parallel(1, 2, 3))
```

```python solution
def series(*r):
    return float(sum(r))

def parallel(*r):
    return 1.0 / sum(1.0 / x for x in r)

print(series(1, 2, 3), parallel(1, 2, 3))
```

```python tests
assert abs(series(1, 2, 3) - 6) < 1e-12, "Series resistances add."
assert abs(parallel(1, 2, 3) - 6 / 11) < 1e-12, "1 ∥ 2 ∥ 3 = 6/11."
assert abs(parallel(10, 10) - 5) < 1e-12, "Two equal resistors in parallel halve."
assert abs(series(1, parallel(2, 2)) - 2) < 1e-12, "1 + 2 ∥ 2 = 2."
```

::: right
Right. Nesting these two calls reduces any series–parallel network.
:::
:::

::: check
kind: python

## Step 2. Write `nearest_e24(x)` returning the E24 value nearest to x on a log scale, in the same decade or the next (so 9.6 rounds to 10).

```python starter
def nearest_e24(x):
    return x

print(nearest_e24(4.9), nearest_e24(7.0))
```

```python solution
import math

E24 = [1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0,
       3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1]

def nearest_e24(x):
    d = math.floor(math.log10(x))
    m = x / 10**d
    best = min(E24 + [10.0], key=lambda v: abs(math.log(v / m)))
    return round(best * 10**d, 10)

print(nearest_e24(4.9), nearest_e24(7.0))
```

```python tests
assert nearest_e24(4.9) == 5.1, "4.9 is nearer 5.1 than 4.7 on a log scale."
assert nearest_e24(7.0) == 6.8, "7.0 rounds to 6.8."
assert nearest_e24(2.35) == 2.4, "2.35 rounds to 2.4."
assert nearest_e24(470) == 470 and nearest_e24(9.6) == 10, "Exact values stay; 9.6 rounds up to 10."
```

::: right
Right. Comparing logarithms treats a 10% step the same at 1 kΩ and at 100 kΩ.
:::
:::

::: check
kind: python

## Step 3. Write `best_divider(ratio)` returning `(R1, R2)` from E24 values between 1.0 and 91 that make $\frac{R_2}{R_1 + R_2}$ closest to `ratio`.

```python starter
def best_divider(ratio):
    return 1.0, 1.0

print(best_divider(0.66))
```

```python solution
E24 = [1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0,
       3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1]
VALUES = [round(v * 10**d, 6) for d in (0, 1) for v in E24]

def best_divider(ratio):
    best = None
    for r1 in VALUES:
        for r2 in VALUES:
            err = abs(r2 / (r1 + r2) - ratio)
            if best is None or err < best[0] - 1e-15:
                best = (err, r1, r2)
    return best[1], best[2]

print(best_divider(0.66))
```

```python tests
r1, r2 = best_divider(0.66)
assert (r1, r2) == (4.7, 9.1), f"The best 5 V to 3.3 V pair is 4.7 over 9.1; got {(r1, r2)}"
a, b = best_divider(1.8 / 3.3)
assert abs(b / (a + b) - 1.8 / 3.3) < 1e-12, "1.8 V from 3.3 V can be hit exactly."
c, d = best_divider(0.24)
assert abs(d / (c + d) - 0.24) < 0.0013, "0.24 is reachable within about 0.1%."
```

::: right
Right. A brute-force search over a few thousand pairs beats any clever rule.
:::
:::

::: check
kind: python

## Step 4. Write `divider_mc(R1, R2, tol, n, seed)` returning `(mean, std)` of a divider's output from 5 V when each resistor is drawn uniformly within ±`tol` (a fraction) of its value, over n trials.

```python starter
def divider_mc(R1, R2, tol, n, seed):
    return 5 * R2 / (R1 + R2), 0.0

print(divider_mc(10, 10, 0.05, 200000, 1))
```

```python solution
import numpy as np

def divider_mc(R1, R2, tol, n, seed):
    rng = np.random.default_rng(seed)
    a = R1 * (1 + tol * rng.uniform(-1, 1, n))
    b = R2 * (1 + tol * rng.uniform(-1, 1, n))
    v = 5 * b / (a + b)
    return float(v.mean()), float(v.std())

print(divider_mc(10, 10, 0.05, 200000, 1))
```

```python tests
import math
m, s = divider_mc(10, 10, 0.05, 200000, 1)
assert abs(m - 2.5) < 1e-3, "The mean stays at 2.5 V."
assert abs(s / (5 / 4 * math.sqrt(2) * 0.05 / math.sqrt(3)) - 1) < 0.02, f"The spread should be about 0.0510 V; got {s}"
m2, s2 = divider_mc(10, 10, 0.01, 200000, 2)
assert abs(s2 / s - 0.2) < 0.01, "1% parts give a fifth of the spread."
```

::: right
Right. The same few lines handle any network: replace the formula for v.
:::
:::

::: check
kind: python

## Step 5. Write `ladder(n, R)` returning the input resistance of an n-section ladder of series R and shunt R with its far end open: one section is 2R, and each added section gives $r \leftarrow R + R\,\|\,r$.

```python starter
def ladder(n, R):
    return 2 * R

print(ladder(1, 1.0), ladder(30, 1.0))
```

```python solution
def ladder(n, R):
    r = 2 * R
    for _ in range(n - 1):
        r = R + R * r / (R + r)
    return r

print(ladder(1, 1.0), ladder(30, 1.0))
```

```python tests
import math
assert abs(ladder(1, 1.0) - 2) < 1e-12, "One section is series R plus shunt R: 2R."
assert abs(ladder(2, 1.0) - 5 / 3) < 1e-12, "Two sections: 1 + 1 ∥ 2 = 1.667."
assert abs(ladder(30, 1.0) - (1 + math.sqrt(5)) / 2) < 1e-12, "A long ladder converges to the golden ratio."
assert abs(ladder(30, 4.7) / ladder(30, 1.0) - 4.7) < 1e-12, "It scales with R."
```

::: right
Right. The fixed point of $r = R + R\,\|\,r$ is the golden ratio times R, reached after a handful of sections.
:::
:::

::: check
kind: parts
given: a = 1 | 4.7 | 10; b = 2.2 | 6.8; c = 3.3 | 22

## Resistors of {a}, {b} and {c} kΩ.

::: part
answer: a + b*c/(b + c)
unit: kΩ
tolerance: 0.5%

### What's {a} kΩ in series with the parallel pair of the other two?

::: misses
- [any] $R_a + R_b\,\|\,R_c$.
:::
:::

::: part
answer: 1/(1/a + 1/b + 1/c)
unit: kΩ
tolerance: 0.5%

### What are all three in parallel?

::: misses
- [any] Add the conductances.
:::
:::
:::

::: check
given: a = 4.7 | 5.1; b = 9.1 | 10; t = 0.66 | 0.67
answer: (b/(a + b) - t)/t*100
unit: %
tolerance: 1%

## A divider uses $R_1$ = {a} and $R_2$ = {b} for a target ratio of {t}. What's its ratio error, in percent of the target?

::: right
Right. $\frac{R_2/(R_1 + R_2) - r}{r}$.
:::

::: misses
- [any] Compare the actual ratio with the target.
:::
:::

::: check
given: V = 3.3 | 5 | 12; t = 1 | 5
answer: V/4*sqrt(2)*t/100/sqrt(3)*1000
unit: mV
tolerance: 0.5%

## An equal-resistor divider from {V} V uses ±{t}% parts with uniform errors. What's the standard deviation of its output?

::: right
Right. $\frac{V}{4}\sqrt2\frac{t}{\sqrt3}$.
:::

::: misses
- [V/4*sqrt(2)*t/100*1000] A uniform spread of ±t has standard deviation $t/\sqrt3$.
- [any] Two independent errors, each with sensitivity ¼, in quadrature.
:::
:::

::: check
given: R = 1 | 4.7 | 10
answer: (1 + sqrt(5))/2*R
unit: kΩ
tolerance: 0.5%

## An endless ladder alternates {R} kΩ in series and {R} kΩ to ground. What's its input resistance?

::: right
Right. $r = R + R\,\|\,r$ gives $\frac{1 + \sqrt5}{2}R$.
:::

::: misses
- [2*R] That's one section; the next sections load the first.
- [any] Solve $r = R + \frac{Rr}{R + r}$.
:::
:::

::: explain
## Why does Monte Carlo usually give a smaller spread than a worst-case tolerance analysis?

::: model
A worst-case analysis puts every resistor at the edge of its tolerance band in whichever direction hurts most, all at once. Real resistors' errors are independent and spread across the band, so their effects partly cancel: one resistor high is as likely to pair with another high as with another low. Monte Carlo draws many combinations from the actual distributions and reports how the output is really spread, which for independent errors grows only as the square root of the sum of their squares. The worst case is still possible, but it sits far out in the tail, and Monte Carlo shows how rare it is, which is what a production yield depends on.
:::
:::

::: practice

::: check
given: R = 1 | 2.2 | 3.3 | 4.7 | 10 | 15 | 22 | 47
answer: (1 + sqrt(5))/2*R
unit: kΩ
tolerance: 0.5%

## Endless R/R ladder input resistance for R = {R} kΩ?

::: misses
- [any] $\frac{1 + \sqrt5}{2}R$.
:::
:::

::: check
given: V = 2.5 | 10; t = 2 | 10
answer: V/4*sqrt(2)*t/100/sqrt(3)*1000
unit: mV
tolerance: 0.5%

## Output spread of an equal divider from {V} V with ±{t}% parts?

::: misses
- [any] $\frac{V}{4}\sqrt2\frac{t}{\sqrt3}$.
:::
:::
:::
