---
title: Feedback lab
heading: Loop gain, phase margin, step response and the nulling resistor, in Python
eyebrow: S2 · Lesson 18 · Feedback lab
xp: 60
---

::: hook
Stability analysis is where hand estimates and simulators most often disagree, because the estimates hide their approximations: "90° minus the arctangent" assumes the dominant pole is far below crossover, and "overshoot from phase margin" assumes a pure two-pole loop. This lab computes the exact numbers instead: it finds a two-pole loop's crossover and phase margin by bisection, simulates its closed-loop step response with Runge–Kutta to measure the real overshoot, and checks the right-half-plane zero that a nulling resistor moves.
:::

::: play
- The curve is the closed-loop step response of a follower built from a two-pole amplifier (dominant pole far below, second pole at k times the gain-bandwidth), $\zeta = \tfrac12\sqrt k$.
- At k = 2 the overshoot is 4.3%; at k = 1 it's 16%; at k = 4 the response is critically damped.
- The lab's simulation reproduces these values numerically from the loop's poles.
:::

```sim plot
view: graph
x: t = 0..4 µs
y: y = 0..1.5
slider: k = 0.5..3.9 step 0.1 = 2
plot: 1 - exp(-0.5*sqrt(k)*sqrt(k)*2*pi*10*t)*(cos(sqrt(1 - 0.25*k)*sqrt(k)*2*pi*10*t) + 0.5*sqrt(k)/sqrt(1 - 0.25*k)*sin(sqrt(1 - 0.25*k)*sqrt(k)*2*pi*10*t)) | label: follower output (GBW = 10 MHz)
readout: overshoot (%) = 100*exp(-pi*0.5*sqrt(k)/sqrt(1 - 0.25*k)) | digits: 3
```

::: predict
## A follower uses an amplifier with $A_0 = 10^4$, a dominant pole at 1 kHz and a second pole at 20 MHz. What phase margin does it have?

- [ ] Exactly 63.4°, from 90° − arctan(10/20)
- [x] About 65.5°: the loop crosses 1 slightly below 10 MHz because the second pole already lowers the gain there
- [ ] About 45°
- [ ] About 90°

::: reveal
The exact crossover is where $|L| = 1$: about 9.1 MHz, not 10, since the second pole has already cut the gain by $1/\sqrt{1 + (f/p_2)^2}$. The phase there is $-90° - \arctan(9.1/20) = -114.5°$, a margin of 65.5°. The rule of thumb is pessimistic by 2°, and the gap grows as the poles approach.
:::
:::

::: nameit
## The tools

- **Loop gain:** $L(f) = A_0\beta/((1 + jf/p_1)(1 + jf/p_2))$. The crossover $f_c$ solves $|L| = 1$ (bisection in log f), and the phase margin is $180° + \angle L(f_c)$.
- **Closed loop:** with poles $\omega_1, \omega_2$ the follower obeys $y'' + (\omega_1 + \omega_2)y' + \omega_1\omega_2(1 + A_0\beta)y = \omega_1\omega_2A_0x$. Integrating it with RK4 gives the exact step response.
- **Nulling resistor:** in a Miller-compensated two-stage op amp, $R_z$ in series with $C_c$ moves the zero from $g_{m2}/C_c$ to $1/(C_c(1/g_{m2} - R_z))$: infinity at $R_z = 1/g_{m2}$, and into the left half plane beyond.
:::

::: check
kind: python

## Write `phase_margin(A0, p1, p2, beta)`: return (phase margin in degrees, crossover frequency in Hz) for the two-pole loop, bisecting in log f between 1 Hz and $10^{12}$ Hz.

```python starter
import math

def phase_margin(A0, p1, p2, beta):
    return (90.0, 1e6)
```

```python solution
import math

def phase_margin(A0, p1, p2, beta):
    def L(f):
        s = 1j * f
        return A0 * beta / ((1 + s / p1) * (1 + s / p2))
    lo, hi = 0.0, 12.0
    for _ in range(200):
        mid = (lo + hi) / 2
        if abs(L(10 ** mid)) > 1:
            lo = mid
        else:
            hi = mid
    v = L(10 ** mid)
    return (180 + math.degrees(math.atan2(v.imag, v.real)), 10 ** mid)
```

```python tests
pm, fc = phase_margin(1e4, 1e3, 2e7, 1.0)
assert abs(pm - 65.54) < 0.02 and abs(fc - 9.10e6) < 0.01e6, "The predict: 65.5° at 9.1 MHz."
pm2, _ = phase_margin(1e4, 1e3, 2e7, 0.1)
assert pm2 > 85, "A gain-of-ten loop (β = 0.1) crosses a decade lower and has far more margin."
```

::: right
Right. Bisection on the magnitude is robust; the phase then comes straight from the complex value.
:::
:::

::: check
kind: python

## Write `step_overshoot(A0, p1, p2, beta, n=200000)`: integrate the closed-loop equation from rest with a unit step input by RK4 over 20 natural periods ($T = 20/\sqrt{b}$ with $b = \omega_1\omega_2(1 + A_0\beta)$) in n steps, and return the overshoot in percent over the final value.

```python starter
import math

def step_overshoot(A0, p1, p2, beta, n=200000):
    return 0.0
```

```python solution
import math

def step_overshoot(A0, p1, p2, beta, n=200000):
    w1, w2 = 2 * math.pi * p1, 2 * math.pi * p2
    a, b, c = w1 + w2, w1 * w2 * (1 + A0 * beta), w1 * w2 * A0
    final = c / b
    h = 20 / math.sqrt(b) / n
    y = v = peak = 0.0
    def f(y, v):
        return v, c - a * v - b * y
    for _ in range(n):
        k1 = f(y, v)
        k2 = f(y + h / 2 * k1[0], v + h / 2 * k1[1])
        k3 = f(y + h / 2 * k2[0], v + h / 2 * k2[1])
        k4 = f(y + h * k3[0], v + h * k3[1])
        y += h / 6 * (k1[0] + 2 * k2[0] + 2 * k3[0] + k4[0])
        v += h / 6 * (k1[1] + 2 * k2[1] + 2 * k3[1] + k4[1])
        peak = max(peak, y)
    return (peak / final - 1) * 100
```

```python tests
os = step_overshoot(1e4, 1e3, 2e7, 1.0)
assert abs(os - 4.321) < 0.005, "Second pole at twice the GBW: 4.32%, matching ζ = 0.707."
assert step_overshoot(1e4, 1e3, 1e7, 1.0) > 15, "Second pole at the GBW: about 16%."
```

::: right
Right. The simulation confirms the two-pole formula exactly here, and it keeps working when extra poles or zeros make the formula invalid.
:::
:::

::: check
kind: python

## Write `rhp_zero(gm2, Cc, Rz)`: the compensation zero $1/(C_c(1/g_{m2} - R_z))$ in rad/s (positive means right half plane), returning `math.inf` when $R_z = 1/g_{m2}$ exactly; and `rz_for_cancel(gm2, Cc, p2)`: the $R_z$ that puts the zero on a left-half-plane pole at −p2 (rad/s).

```python starter
import math

def rhp_zero(gm2, Cc, Rz):
    return gm2 / Cc

def rz_for_cancel(gm2, Cc, p2):
    return 1 / gm2
```

```python solution
import math

def rhp_zero(gm2, Cc, Rz):
    d = 1 / gm2 - Rz
    if d == 0:
        return math.inf
    return 1 / (Cc * d)

def rz_for_cancel(gm2, Cc, p2):
    return 1 / gm2 + 1 / (p2 * Cc)
```

```python tests
assert abs(rhp_zero(1e-3, 1e-12, 0) - 1e9) < 1, "No resistor: a right-half-plane zero at gm2/Cc = 1e9 rad/s."
assert rhp_zero(1e-3, 1e-12, 1e3) == math.inf, "Rz = 1/gm2 pushes it to infinity."
r = rz_for_cancel(1e-3, 1e-12, 2e9)
assert abs(rhp_zero(1e-3, 1e-12, r) + 2e9) < 1, "That Rz puts the zero at −2e9 rad/s, on the pole."
```

::: right
Right. Slightly more than $1/g_{m2}$ moves the zero into the left half plane, where it can cancel the output pole and add phase instead of removing it.
:::
:::

::: check
kind: python

## Write `pm_rule(p2_over_ft)`: the rule-of-thumb phase margin $90° - \arctan(1/k)$ for a second pole at k times the unity-gain frequency, and `pm_error(A0, p1, k)`: the exact margin (from `phase_margin` with β = 1 and $p_2 = kA_0p_1$) minus the rule's.

```python starter
import math

def pm_rule(p2_over_ft):
    return 90.0

def pm_error(A0, p1, k):
    return 0.0
```

```python solution
import math

def pm_rule(p2_over_ft):
    return 90 - math.degrees(math.atan(1 / p2_over_ft))

def pm_error(A0, p1, k):
    def L(f):
        s = 1j * f
        return A0 / ((1 + s / p1) * (1 + s / (k * A0 * p1)))
    lo, hi = 0.0, 12.0
    for _ in range(200):
        mid = (lo + hi) / 2
        if abs(L(10 ** mid)) > 1:
            lo = mid
        else:
            hi = mid
    v = L(10 ** mid)
    return 180 + math.degrees(math.atan2(v.imag, v.real)) - pm_rule(k)
```

```python tests
assert abs(pm_rule(2) - 63.43) < 0.01, "Rule: 63.4° at k = 2."
e2, e1 = pm_error(1e4, 1e3, 2), pm_error(1e4, 1e3, 1)
assert 1.5 < e2 < 3 and e1 > e2, "The rule is pessimistic by about 2° at k = 2, and more as the poles approach."
```

::: right
Right. The rule assumes the crossover stays at the gain-bandwidth; the second pole pulls it lower, which helps.
:::
:::

::: explain
## Why is the phase-margin rule of thumb "90° minus arctan of the unity-gain frequency over the second pole" slightly pessimistic for a two-pole amplifier?

::: model
The rule assumes the loop gain crosses one at the gain-bandwidth frequency, as it would with only the dominant pole. But the second pole is already reducing the magnitude at that frequency, by a factor of $1/\sqrt{1 + (f/p_2)^2}$, so the real crossover happens somewhat lower. At the lower frequency the second pole contributes less phase lag, so the actual margin is a little larger than the rule predicts. The difference is a couple of degrees when the second pole is twice the gain-bandwidth, and grows as the poles move closer together.
:::
:::
