---
title: Sources and equivalents lab
heading: Fitting a Thevenin model to measurements, sweeping for maximum power, checking superposition, and a dependent-source amplifier stage, in Python
eyebrow: C1 · Lesson 19 · Sources and equivalents lab
xp: 50
---

::: hook
Lessons 14 to 17 replaced real sources and whole networks with two numbers, a Thevenin voltage and resistance, and used them to find load voltages, superposed contributions and the load that takes the most power. On the bench those two numbers come from measurements, which are noisy and more than two. In this lab you'll fit a Thevenin model to data by least squares, find the best load by sweeping, check superposition numerically, and analyze an emitter follower's dependent-source model, which leads into lesson 21.
:::

::: play
- A source's terminal voltage falls along the straight line $V = V_{th} - IR_{th}$; a resistive load forces $V = IR_L$. Where the lines cross is the operating point.
- The sliders set the source and the load. Make the load equal to $R_{th}$ and the operating point sits at half the open-circuit voltage and half the short-circuit current, where the power, the rectangle under the point, is largest.
- The readouts give the operating point and the load power.
:::

```sim plot
view: graph
x: I = 0..200 mA
y: V = 0..12 V
slider: v = 1..12 step 0.1 = 5 V
slider: r = 10..200 step 1 = 50 Ω
slider: L = 5..500 = 50 Ω log
plot: v - I/1000*r | label: source line | samples: 200
plot: I/1000*L | color: ch2 | label: load line | samples: 200
point: v/(r + L)*1000, v*L/(r + L) | label: operating point
readout: current, mA = v/(r + L)*1000 | digits: 4
readout: load voltage, V = v*L/(r + L) | digits: 4
readout: load power, mW = v^2*L/(r + L)^2*1000 | digits: 4
```

::: predict
## A bench supply's voltage is measured at four load currents: 12.10 V at 0 A, 12.00 V at 1 A, 11.90 V at 2 A and 11.60 V at 5 A. What's its internal resistance?

- [ ] 2.4 Ω, from 12 V at 5 A
- [x] 0.1 Ω
- [ ] 0.5 Ω
- [ ] It can't be found without a short-circuit test

::: reveal
0.1 Ω. The voltage falls 0.1 V for every amp, so $R_{th}$ = 0.1 Ω and $V_{th}$ = 12.10 V. A least-squares line through all four points finds the same values and averages out measurement noise, without ever shorting the supply.
:::
:::

::: nameit
## Fitting a Thevenin model

A linear source obeys $V = V_{th} - IR_{th}$. Given several measured (I, V) pairs, a least-squares straight-line fit returns the intercept $V_{th}$ and the slope $-R_{th}$. With noisy data, more points narrow the uncertainty; with a nonlinear source, the fit's residuals show where the linear model fails.

## Sweeping for maximum power

The power into a load is $P = \frac{V_{th}^2R_L}{(R_{th} + R_L)^2}$. Sweeping $R_L$ over a logarithmic grid and keeping the largest P finds the maximum at $R_L = R_{th}$, as lesson 17 proved. The peak is broad: within a factor of two either side, the power stays above 89% of the maximum.

## Superposition, numerically

For a node fed by several sources $V_i$ through resistors $R_i$, with $R_g$ to ground, $v = \frac{\sum V_i/R_i}{\sum 1/R_i + 1/R_g}$. Setting all but one source to zero and summing the results must give the same v, to rounding: a quick test of any linear-circuit code.

## A dependent-source stage

An emitter follower, modeled with a dependent current source β$i_b$, a source resistance $R_s$ and a load $R_L$, has

$$R_{in} = R_s + (\beta + 1)R_L, \qquad \frac{v_{out}}{v_{in}} = \frac{(\beta + 1)R_L}{R_s + (\beta + 1)R_L},$$

and seen from the load, the source resistance shrinks to $\frac{R_s}{\beta + 1}$. Solving the model's equations numerically, with a test source where needed, reproduces all three.
:::

::: example
## Fit a Thevenin model to the bench supply's readings: (0 A, 12.10 V), (1 A, 12.00 V), (2 A, 11.90 V), (5 A, 11.60 V).

::: solution
1. **Fit the line.** The points lie exactly on a line, with slope −0.10 V/A and intercept 12.10 V.
2. **Read off the model.** So $V_{th}$ = 12.10 V and $R_{th}$ = 0.10 Ω.
3. **Short-circuit current.**
   $$\begin{aligned} I_{sc} &= \frac{12.10}{0.10} \\ &= 121\ \text{A} \end{aligned}$$
   though the supply would current-limit long before.
4. **Noisy readings.** With noisy readings, a least-squares fit uses all of them; the uncertainty in $R_{th}$ shrinks as the spread of currents grows, which is why such tests use the widest current range the source safely allows.

**Answer:** the supply is 12.10 V behind 0.10 Ω.
:::
:::

::: example
## A 5 V source has 50 Ω inside. Sweep the load: what's the power at 25, 50 and 100 Ω?

::: solution
1. **Load power.** With 5 V behind 50 Ω,
   $$P = \frac{25R_L}{(50 + R_L)^2}$$
2. **At 25 Ω.**
   $$\begin{aligned} P &= \frac{25 \times 25}{75^2} \\ &= 111\ \text{mW} \end{aligned}$$
3. **At 50 Ω.**
   $$\begin{aligned} P &= \frac{25 \times 50}{100^2} \\ &= 125\ \text{mW} \end{aligned}$$
   the maximum.
4. **At 100 Ω.**
   $$\begin{aligned} P &= \frac{25 \times 100}{150^2} \\ &= 111\ \text{mW} \end{aligned}$$
5. **A flat peak.** Halving or doubling the load costs only 11%. That flat peak is why "matched" loads in practice are often off by tens of percent with little loss.
6. **The cost of a match.** A matched load wastes half the source's power: at 50 Ω the source itself dissipates 125 mW too.

**Answer:** the load gets 111 mW at 25 Ω, 125 mW at 50 Ω (the maximum) and 111 mW at 100 Ω.
:::
:::

::: example
## An emitter follower has β = 100, source resistance 1 kΩ and load 1 kΩ. Find its input resistance, voltage gain and output resistance.

::: solution
1. **Input resistance.**
   $$\begin{aligned} R_{in} &= 1 + 101 \times 1 \\ &= 102\ \text{k}\Omega \end{aligned}$$
2. **Voltage gain.**
   $$\begin{aligned} \frac{v_{out}}{v_{in}} &= \frac{101}{102} \\ &= 0.990 \end{aligned}$$
3. **Output resistance.** Looking back from the load, the 1 kΩ source resistance appears divided by β + 1:
   $$\frac{1000}{101} = 9.9\ \Omega$$
   or 9.8 Ω with the 1 kΩ load in parallel.
4. **What the stage is for.** It turns a 1 kΩ source into a 10 Ω one at nearly unity gain, which is its whole purpose: a buffer.

**Answer:** the input resistance is 102 kΩ, the voltage gain 0.990, and the output resistance 9.9 Ω (9.8 Ω with the load in parallel).
:::
:::

::: check
kind: python

## Step 1. Write `thevenin_fit(I, V)` returning `(Vth, Rth)` from a least-squares straight-line fit of measured voltages V against currents I.

```python starter
import numpy as np

def thevenin_fit(I, V):
    return float(V[0]), 0.0

print(thevenin_fit([0, 1, 2, 5], [12.10, 12.00, 11.90, 11.60]))
```

```python solution
import numpy as np

def thevenin_fit(I, V):
    slope, intercept = np.polyfit(np.asarray(I, float), np.asarray(V, float), 1)
    return float(intercept), float(-slope)

print(thevenin_fit([0, 1, 2, 5], [12.10, 12.00, 11.90, 11.60]))
```

```python tests
import numpy as np
v, r = thevenin_fit([0, 1, 2, 5], [12.10, 12.00, 11.90, 11.60])
assert abs(v - 12.10) < 1e-9 and abs(r - 0.10) < 1e-9, f"Expected 12.10 V and 0.10 Ω; got {v}, {r}"
rng = np.random.default_rng(5)
I = np.linspace(0, 0.2, 21)
V = 5 - 50 * I + rng.normal(0, 0.01, I.size)
v2, r2 = thevenin_fit(I, V)
assert abs(v2 - 5) < 0.02 and abs(r2 - 50) < 0.2, "Noisy data from 5 V behind 50 Ω should fit close to those values."
```

::: right
Right. Every measured point contributes, so noise averages down as the number of points grows.
:::
:::

::: check
kind: python

## Step 2. Write `best_load(Vth, Rth)` returning the load, among 2001 values spaced logarithmically from Rth/100 to 100 Rth, that takes the most power.

```python starter
def best_load(Vth, Rth):
    return 2 * Rth

print(best_load(5, 50))
```

```python solution
import numpy as np

def best_load(Vth, Rth):
    RL = Rth * np.logspace(-2, 2, 2001)
    P = Vth**2 * RL / (Rth + RL) ** 2
    return float(RL[np.argmax(P)])

print(best_load(5, 50))
```

```python tests
assert abs(best_load(5, 50) / 50 - 1) < 1e-9, f"The best load equals Rth; got {best_load(5, 50)}"
assert abs(best_load(9, 1.5) / 1.5 - 1) < 1e-9, "For any source."
```

::: right
Right. The sweep finds $R_L = R_{th}$ without any calculus.
:::
:::

::: check
kind: python

## Step 3. Write `node_voltage(sources, Rg)` for a node fed by a list of `(V, R)` sources with `Rg` to ground, and `superposed(sources, Rg)` returning the sum of each source's contribution with the others set to zero.

```python starter
def node_voltage(sources, Rg):
    return 0.0

def superposed(sources, Rg):
    return 0.0

s = [(10, 1), (5, 1)]
print(node_voltage(s, 1), superposed(s, 1))
```

```python solution
def node_voltage(sources, Rg):
    num = sum(V / R for V, R in sources)
    den = sum(1 / R for _, R in sources) + 1 / Rg
    return num / den

def superposed(sources, Rg):
    total = 0.0
    for k in range(len(sources)):
        alone = [(V if i == k else 0.0, R) for i, (V, R) in enumerate(sources)]
        total += node_voltage(alone, Rg)
    return total

s = [(10, 1), (5, 1)]
print(node_voltage(s, 1), superposed(s, 1))
```

```python tests
s = [(10, 1), (5, 1)]
assert abs(node_voltage(s, 1) - 5) < 1e-12, "10 V and 5 V through 1 kΩ each into 1 kΩ to ground give 5 V."
assert abs(superposed(s, 1) - node_voltage(s, 1)) < 1e-12, "Superposition must reproduce the full answer."
s3 = [(1, 10), (0.5, 10), (-0.2, 10)]
assert abs(node_voltage(s3, 10) - 0.325) < 1e-12, "The passive mixer gives 0.325 V."
assert abs(node_voltage([(10, 1)], 1) * 2 / 3 - 10 / 3) < 1e-12, "One source alone into 1 kΩ ∥ 1 kΩ checks the divider."
```

::: right
Right. Superposition is exact for linear circuits, so the two agree to rounding.
:::
:::

::: check
kind: python

## Step 4. Write `follower(beta, Rs, RL)` returning `(Rin, gain, Rout)` for the emitter-follower model: solve for the base current with a 1 V input, then find the output resistance by driving the output with a test current, input source shorted.

```python starter
def follower(beta, Rs, RL):
    return RL, 1.0, Rs

print(follower(100, 1.0, 1.0))
```

```python solution
def follower(beta, Rs, RL):
    ib = 1.0 / (Rs + (beta + 1) * RL)
    Rin = 1.0 / ib
    gain = (beta + 1) * ib * RL
    # Test current into the output node, input shorted: KCL gives
    # i_t = v/RL + v/Rs * (beta + 1), since ib = -v/Rs and the source adds beta*ib.
    v = 1.0 / (1 / RL + (beta + 1) / Rs)
    return Rin, gain, v

print(follower(100, 1.0, 1.0))
```

```python tests
Rin, g, Ro = follower(100, 1.0, 1.0)
assert abs(Rin - 102) < 1e-9, f"Rin = Rs + (β + 1)RL = 102 kΩ; got {Rin}"
assert abs(g - 101 / 102) < 1e-12, "The gain is 101/102."
assert abs(Ro * 1000 - 9.80) < 0.01, f"Seen from the output, with RL in parallel, about 9.8 Ω; got {Ro * 1000}"
```

::: right
Right. The dependent source multiplies the load's resistance seen from the input and divides the source's resistance seen from the output.
:::
:::

::: check
kind: python

## Step 5. Write `pulse_resistance(t, v, i_low, i_high, t_on, t_off)` estimating a battery's internal resistance from a voltage record during a current pulse: average v before `t_on`, average v between `t_on` and `t_off`, and divide the drop by the current step.

```python starter
def pulse_resistance(t, v, i_low, i_high, t_on, t_off):
    return 0.0

print(pulse_resistance([0, 1, 2, 3], [12.1, 12.1, 11.6, 11.6], 0, 5, 1.5, 4))
```

```python solution
import numpy as np

def pulse_resistance(t, v, i_low, i_high, t_on, t_off):
    t = np.asarray(t, float)
    v = np.asarray(v, float)
    before = v[t < t_on].mean()
    during = v[(t >= t_on) & (t < t_off)].mean()
    return float((before - during) / (i_high - i_low))

print(pulse_resistance([0, 1, 2, 3], [12.1, 12.1, 11.6, 11.6], 0, 5, 1.5, 4))
```

```python tests
import numpy as np
assert abs(pulse_resistance([0, 1, 2, 3], [12.1, 12.1, 11.6, 11.6], 0, 5, 1.5, 4) - 0.1) < 1e-12, "A 0.5 V drop for 5 A is 0.1 Ω."
rng = np.random.default_rng(2)
t = np.arange(0, 2, 0.001)
i = np.where((t >= 1) & (t < 1.5), 10.0, 1.0)
v = 3.7 - 0.05 * i + rng.normal(0, 0.002, t.size)
r = pulse_resistance(t, v, 1.0, 10.0, 1.0, 1.5)
assert abs(r - 0.05) < 0.001, f"A 50 mΩ cell should come out near 0.05 Ω; got {r}"
```

::: right
Right. Averaging many samples before and during the pulse beats a single reading, and the current step cancels the cell's EMF.
:::
:::

::: check
kind: parts
given: v = 12 | 12.6; i = 0.5 | 1; d = 0.2 | 0.5; j = 4 | 8

## A source reads {v} V at {i} A and {d} V less at {j} A.

::: part
answer: d/(j - i)
unit: Ω
tolerance: 0.5%

### What's its Thevenin resistance?

::: misses
- [any] Slope of the V–I line.
:::
:::

::: part
answer: v + i*d/(j - i)
unit: V
tolerance: 0.5%

### What's its Thevenin voltage?

::: misses
- [any] Extend the line to zero current.
:::
:::
:::

::: check
given: V = 1 | 5 | 12; R = 8 | 50 | 600
answer: V^2/(4*R)*1000
unit: mW
tolerance: 0.5%

## A source of {V} V behind {R} Ω drives a matched load. How much power does the load get?

::: right
Right. $\frac{V_{th}^2}{4R_{th}}$.
:::

::: misses
- [V^2/R*1000] The load gets only half the source voltage.
- [any] Half the voltage across $R_L = R_{th}$.
:::
:::

::: check
given: a = 5 | 10; b = 2 | -3; c = 1 | 4; R = 1 | 10; g = 1 | 2
answer: (a + b + c)/(3 + R/(g*R))
unit: V
tolerance: 0.5%

## Sources of {a}, {b} and {c} V each feed a node through {R} kΩ, and {g} × {R} kΩ connects it to ground. What's the node voltage?

::: right
Right. $\frac{\sum V_i/R}{3/R + 1/R_g}$.
:::

::: misses
- [(a + b + c)/3] The ground resistor also pulls on the node.
- [any] Sum the conductance-weighted sources.
:::
:::

::: check
kind: parts
given: b = 50 | 100 | 200; s = 5 | 10; L = 0.5 | 2

## An emitter follower has β = {b}, source resistance {s} kΩ and load {L} kΩ.

::: part
answer: s + (b + 1)*L
unit: kΩ
tolerance: 0.5%

### What's its input resistance?

::: misses
- [(b + 1)*L] Include the source resistance in series.
- [any] $R_s + (\beta + 1)R_L$.
:::
:::

::: part
answer: (b + 1)*L/(s + (b + 1)*L)
tolerance: 0.5%

### What's its voltage gain?

::: misses
- [any] $\frac{(\beta + 1)R_L}{R_s + (\beta + 1)R_L}$.
:::
:::
:::

::: explain
## Why does fitting a line to several load measurements give a better Thevenin model than using just two of them?

::: model
Each voltage reading carries some noise, from the meter and from small changes in the source. With only two points, any error in either one goes straight into the slope, which is $R_{th}$, and the error is magnified when the two currents are close together. A least-squares line through many points, spread over a wide range of currents, averages the noise down: random errors partly cancel, and the slope is pinned by the points far apart. The fit also exposes trouble a two-point calculation hides: if the points curve away from a straight line, the source isn't linear over that range, and a single Thevenin model doesn't describe it.
:::
:::

::: practice

::: check
given: V = 3.3 | 9; R = 4 | 75
answer: V^2/(4*R)*1000
unit: mW
tolerance: 0.5%

## Maximum load power from {V} V behind {R} Ω?

::: misses
- [any] $\frac{V^2}{4R}$.
:::
:::

::: check
given: b = 80 | 150; s = 2 | 5; L = 1 | 3
answer: s + (b + 1)*L
unit: kΩ
tolerance: 0.5%

## Follower input resistance: β = {b}, $R_s$ = {s} kΩ, $R_L$ = {L} kΩ?

::: misses
- [any] $R_s + (\beta + 1)R_L$.
:::
:::
:::
