---
title: "Dispatch lab: economic dispatch, losses and prices"
heading: Lambda iteration with generator limits, dispatch with transmission losses and penalty factors, a congested two-bus market with its prices and congestion rent, and penalty factors from a full B matrix
eyebrow: S10 · Lesson 34 · Dispatch lab
xp: 60
---

::: hook
Lessons 27 to 29 dispatched generators by equal incremental cost, charged remote plants for their losses, and priced electricity bus by bus. Market operators run these calculations every five minutes over thousands of units. In this lab you'll write the core of them: a lambda iteration that respects unit limits, the coordination equation with losses, a congested two-bus market, and penalty factors from a loss-coefficient matrix.
:::

::: play
- The curve is total generation from three units against the system's incremental cost λ, each unit running at $(\lambda - b_i)/(2c_i)$ between its limits (lesson 27's units, with the first capped at 300 MW).
- Where a unit hits its limit the curve's slope drops: λ rises faster for the same added load.
- Lambda iteration searches this monotone curve for the λ that meets the demand.
:::

```sim plot
view: graph
x: l = 7..14
y: P = 0..1400
plot: step(l - 8)*(l - 8)/0.01*step(11 - l) + step(l - 11)*300 + step(l - 9)*(l - 9)/0.008 + step(l - 7)*(l - 7)/0.016 | label: total output, MW
```

::: predict
## Three units share 900 MW at λ = 11.35 dollars per MWh. If the first unit is limited to 300 MW instead of the 334.8 it would produce, what happens to λ?

- [ ] It falls
- [ ] It stays at 11.35
- [x] It rises, as the other two must cover the missing 34.8 MW
- [ ] It doubles

::: reveal
With the first unit pinned at 300 MW, the other two share 600 MW: $(\lambda - 9)/0.008 + (\lambda - 7)/0.016 = 600$ gives λ = 11.53 dollars per MWh. A limit takes a unit out of the balancing, so the remaining ones must climb their cost curves further.
:::
:::

::: nameit
## What the lab computes

- **Lambda iteration**: for a trial λ, each unit runs at $P_i = (\lambda - b_i)/(2c_i)$ clamped to $[P_{\min,i}, P_{\max,i}]$; bisect on λ until the outputs sum to the demand.
- **Losses**: with a remote unit whose losses are $BP_2^2$, the coordination equation $(b_2 + 2c_2P_2) = \lambda(1 - 2BP_2)$ gives $P_2 = (\lambda - b_2)/(2c_2 + 2B\lambda)$, and the balance is $P_1 + P_2 = D + BP_2^2$.
- **Two-bus market**: a cheap unit at A and a dear one at B serve load at B through a line of limited capacity. If the line binds, B's price is set by the dear unit, A's by the cheap one, and the congestion rent is the flow times the price difference.
- **Penalty factors** from $P_L = P^TBP$: $\partial P_L/\partial P_i = 2\sum_jB_{ij}P_j$ and $L_i = 1/(1 - \partial P_L/\partial P_i)$.
:::

::: check
kind: python

## Write `dispatch(units, D)`: units are (b, c, Pmin, Pmax) with incremental cost b + 2cP dollars per MWh; return (λ, list of outputs) meeting demand D, by bisecting λ between 0 and 1000.

```python starter
def dispatch(units, D):
    return 0.0, [D / len(units)] * len(units)
```

```python solution
def dispatch(units, D):
    def outputs(lam):
        return [min(pmax, max(pmin, (lam - b) / (2 * c))) for b, c, pmin, pmax in units]
    lo, hi = 0.0, 1000.0
    for _ in range(200):
        lam = (lo + hi) / 2
        if sum(outputs(lam)) > D:
            hi = lam
        else:
            lo = lam
    return lam, outputs(lam)
```

```python tests
units = [(8, 0.005, 0, 1000), (9, 0.004, 0, 1000), (7, 0.008, 0, 1000)]
lam, P = dispatch(units, 900)
assert abs(lam - 11.348) < 1e-3 and abs(P[0] - 334.78) < 0.01, "Lesson 27's three units: λ = 11.35."
lam, P = dispatch([(8, 0.005, 0, 300)] + units[1:], 900)
assert abs(P[0] - 300) < 1e-9 and abs(lam - 11.533) < 1e-3, "With the first unit capped at 300 MW: λ = 11.53."
assert abs(sum(P) - 900) < 1e-6, "The outputs meet the demand."
```

::: right
Right. Bisection works because total output never decreases as λ rises, even with units pinned at their limits.
:::
:::

::: check
kind: python

## Write `dispatch_losses(b1, c1, b2, c2, B, D)`: unit 1 local, unit 2 remote with losses $BP_2^2$; return (λ, P1, P2, losses).

```python starter
def dispatch_losses(b1, c1, b2, c2, B, D):
    return 0.0, D / 2, D / 2, 0.0
```

```python solution
def dispatch_losses(b1, c1, b2, c2, B, D):
    lo, hi = max(b1, b2), 1000.0
    for _ in range(200):
        lam = (lo + hi) / 2
        P1 = (lam - b1) / (2 * c1)
        P2 = (lam - b2) / (2 * c2 + 2 * B * lam)
        if P1 + P2 - B * P2 * P2 > D:
            hi = lam
        else:
            lo = lam
    return lam, P1, P2, B * P2 * P2
```

```python tests
lam, P1, P2, L = dispatch_losses(8, 0.005, 7.5, 0.005, 0.0002, 500)
assert abs(lam - 10.801) < 1e-3 and abs(P1 - 280.11) < 0.01 and abs(P2 - 230.52) < 0.01, "λ = 10.80, P1 = 280.1, P2 = 230.5 MW."
assert abs(P1 + P2 - L - 500) < 1e-6, "Generation covers the load plus 10.6 MW of losses."
assert abs((7.5 + 0.01 * P2) / (1 - 2 * 0.0002 * P2) - lam) < 1e-9, "The remote unit's penalized cost equals λ."
```

::: right
Right. The remote unit is cheaper to run but pays for its losses, so it ends up producing less than equal incremental costs alone would give it.
:::
:::

::: check
kind: python

## Write `two_bus_market(cA, cB, D, limit)`: a constant-cost unit at A (cA dollars per MWh) and one at B (cB > cA) serve D MW at B through a line limited to `limit` MW; return (flow, price at A, price at B, congestion rent per hour).

```python starter
def two_bus_market(cA, cB, D, limit):
    return D, cA, cA, 0.0
```

```python solution
def two_bus_market(cA, cB, D, limit):
    if D <= limit:
        return D, cA, cA, 0.0
    return limit, cA, cB, limit * (cB - cA)
```

```python tests
assert two_bus_market(20, 50, 200, 250) == (200, 20, 20, 0.0), "Uncongested: one price."
f, pa, pb, rent = two_bus_market(20, 50, 400, 250)
assert f == 250 and pa == 20 and pb == 50 and rent == 7500, "Congested: B pays 50, rent 7500 dollars per hour."
```

::: right
Right. The rent is what loads at B pay above what generators at A receive for the power the line carries.
:::
:::

::: check
kind: python

## Write `penalty_factors(Bm, P)`: for a loss matrix Bm (list of lists, per MW) and outputs P, return the list of penalty factors $1/(1 - 2\sum_jB_{ij}P_j)$.

```python starter
def penalty_factors(Bm, P):
    return [1.0] * len(P)
```

```python solution
def penalty_factors(Bm, P):
    n = len(P)
    return [1 / (1 - 2 * sum(Bm[i][j] * P[j] for j in range(n))) for i in range(n)]
```

```python tests
L = penalty_factors([[0, 0], [0, 0.0002]], [280.1, 230.5])
assert abs(L[0] - 1) < 1e-12 and abs(L[1] - 1 / (1 - 0.0922)) < 1e-12, "A local unit pays nothing; the remote one 1.10."
L = penalty_factors([[1e-4, 2e-5], [2e-5, 1.5e-4]], [200, 300])
assert abs(L[0] - 1 / (1 - 2 * (0.02 + 0.006))) < 1e-12 and L[1] > L[0], "Off-diagonal terms couple the units' losses."
```

::: right
Right. With a full B matrix each unit's penalty depends on everyone's output, so dispatch and penalty factors are iterated together.
:::
:::

::: explain
## Why does a congested line create two different prices at its ends, and who collects the difference?

::: model
When a line can't carry more, the next megawatt of load on the receiving side must come from a local generator that costs more, so the cost of serving one more megawatt there, the locational price, rises to that generator's cost. On the sending side, more load can still be met by the cheap generators, so the price stays low. Loads on the receiving side pay the high price for all their energy, while the cheap generators are paid the low price for what they send over the line, and the difference on that flow is the congestion rent, collected by the system operator and usually returned through financial transmission rights.
:::
:::
