---
title: "Response lab: stability, step metrics and linear models"
heading: A Routh-array stability counter, step-response metrics measured from data, numerical linearization by central differences, and the residues that decide which poles dominate
eyebrow: S9 · Lesson 12 · Response lab
xp: 60
---

::: hook
Lessons 3 to 10 gave closed forms: overshoot from ζ, settling from σ, stability from the Routh array, the residues that make some poles dominate. Real work runs these as code: a stability check inside a design loop, a script that reads overshoot and settling time off a measured response, a linearization of a model too messy to differentiate by hand. In this lab you'll write those tools and test each against the formulas it replaces.
:::

::: play
- The curve is the step response of the standard second-order system with $\omega_n$ = 10 rad/s, ζ set by the slider.
- At ζ = 0.5 it peaks at 0.363 s with 16.3% overshoot and stays within 2% after 0.81 s, close to the estimate $4/\zeta\omega_n$ = 0.8 s.
- Your step_metrics function must recover those numbers from samples alone.
:::

```sim plot
view: graph
x: t = 0..1.5 s
y: y = 0..1.8
slider: z = 0.1..0.95 = 0.5
plot: 1 - exp(-z*10*t)*(cos(10*sqrt(1 - z^2)*t) + z/sqrt(1 - z^2)*sin(10*sqrt(1 - z^2)*t)) | label: step response
plot: 1.02 + 0*t | dashed | label: +2%
plot: 0.98 + 0*t | dashed | label: −2%
```

::: predict
## The Routh array of $s^3 + 3s^2 + (K - 4)s + K$ is built for K = 5. How many sign changes appear in its first column?

- [ ] None
- [ ] One
- [x] Two
- [ ] Three

::: reveal
The first column is 1, 3, (3(K − 4) − K)/3 = (2K − 12)/3 = −2/3, and K = 5: signs +, +, −, +, two changes, so two roots in the right half-plane. The loop of lesson 9's example needs K > 6.
:::
:::

::: nameit
## What the lab computes

- **Routh array**: rows from the polynomial's alternating coefficients, each new entry $\frac{r_{1,0}r_{2,j+1} - r_{1,j+1}r_{2,0}}{r_{2,0}}$ from the two rows above; the number of sign changes in the first column counts right-half-plane roots (assume no zero appears in the first column).
- **Step metrics** from samples $(t_k, y_k)$ with final value $y_\infty$ taken as the last sample: overshoot $(\max y - y_\infty)/y_\infty$, peak time where the maximum occurs, 2% settling time as the last time $|y - y_\infty| > 0.02y_\infty$.
- **Linearization**: $A_{ij} = \partial f_i/\partial x_j \approx \frac{f_i(x + he_j) - f_i(x - he_j)}{2h}$, with error proportional to $h^2$.
- **Residues**: for $T(s) = \prod(-p_i)/\prod(s - p_i)$ with distinct real poles, the step response is $1 + \sum_k r_ke^{p_kt}$ with
$$r_k = \frac{\prod_j(-p_j)}{p_k\prod_{j \ne k}(p_k - p_j)}$$
:::

::: check
kind: python

## Write `routh(c)`: given polynomial coefficients from the highest power down, return the first column of the Routh array (a list) and the number of sign changes in it.

```python starter
def routh(c):
    return [1.0], 0
```

```python solution
def routh(c):
    n = len(c)
    r1 = [float(v) for v in c[0::2]]
    r2 = [float(v) for v in c[1::2]]
    w = len(r1)
    r2 += [0.0] * (w - len(r2))
    first = [r1[0], r2[0]]
    for _ in range(n - 2):
        new = [(r2[0] * r1[j + 1] - r1[0] * r2[j + 1]) / r2[0] for j in range(w - 1)] + [0.0]
        r1, r2 = r2, new
        first.append(r2[0])
    changes = sum(1 for a, b in zip(first, first[1:]) if a * b < 0)
    return first, changes
```

```python tests
f, n = routh([1, 2, 3, 4, 5])
assert n == 2, "s⁴ + 2s³ + 3s² + 4s + 5 has two right-half-plane roots."
assert routh([1, 3, 8 - 4, 8])[1] == 0 and routh([1, 3, 5 - 4, 5])[1] == 2, "Stable for K = 8, two RHP roots for K = 5."
assert routh([1, 6, 11, 6])[1] == 0, "(s + 1)(s + 2)(s + 3) is stable."
f, n = routh([1, 1, 2, 8])
assert n == 2 and abs(f[2] - (1 * 2 - 1 * 8) / 1) < 1e-12, "s³ + s² + 2s + 8: first column 1, 1, −6, 8."
```

::: right
Right. The array needs only arithmetic on the coefficients, which is why it predates any way of finding the roots themselves.
:::
:::

::: check
kind: python

## Write `step_metrics(t, y)`: from sampled step-response data, return (overshoot as a fraction, peak time, 2% settling time), taking the final value as the last sample.

```python starter
def step_metrics(t, y):
    return 0.0, 0.0, 0.0
```

```python solution
def step_metrics(t, y):
    yf = y[-1]
    k = max(range(len(y)), key=lambda i: y[i])
    over = (y[k] - yf) / yf
    ts = 0.0
    for i in range(len(y)):
        if abs(y[i] - yf) > 0.02 * abs(yf):
            ts = t[i]
    return over, t[k], ts
```

```python tests
import math
z, wn = 0.5, 10.0
wd = wn * math.sqrt(1 - z * z)
t = [k * 1e-4 for k in range(40001)]
y = [1 - math.exp(-z * wn * s) * (math.cos(wd * s) + z / math.sqrt(1 - z * z) * math.sin(wd * s)) for s in t]
o, tp, ts = step_metrics(t, y)
assert abs(o - math.exp(-math.pi * z / math.sqrt(1 - z * z))) < 1e-4, "Overshoot 16.3%."
assert abs(tp - math.pi / wd) < 2e-4, "Peak at π/ω_d = 0.363 s."
assert abs(ts - 0.8076) < 2e-3, "Settles (2%) at 0.808 s, near 4/(ζω_n) = 0.8 s."
```

::: right
Right. The settling estimate 4/ζω_n follows the envelope; the true settling time jumps in steps as successive peaks dip inside the band.
:::
:::

::: check
kind: python

## Write `jacobian(f, x0, h)`: the matrix of partial derivatives of a function f (taking and returning lists) at x0, by central differences with step h.

```python starter
def jacobian(f, x0, h):
    n = len(x0)
    return [[0.0] * n for _ in range(n)]
```

```python solution
def jacobian(f, x0, h):
    n = len(x0)
    m = len(f(x0))
    J = [[0.0] * n for _ in range(m)]
    for j in range(n):
        xp = list(x0); xp[j] += h
        xm = list(x0); xm[j] -= h
        fp, fm = f(xp), f(xm)
        for i in range(m):
            J[i][j] = (fp[i] - fm[i]) / (2 * h)
    return J
```

```python tests
import math
g, l, c = 9.81, 0.5, 0.3
pend = lambda x: [x[1], -g / l * math.sin(x[0]) - c * x[1]]
A = jacobian(pend, [math.pi, 0.0], 1e-5)
assert abs(A[0][1] - 1) < 1e-9 and abs(A[1][0] - g / l) < 1e-6 and abs(A[1][1] + c) < 1e-9, "At the top: A = [[0, 1], [g/l, −c]]."
maglev = lambda x: [x[1], 9.81 - 9.81 * (0.01 / x[0]) ** 2]
B = jacobian(maglev, [0.01, 0.0], 1e-7)
assert abs(B[1][0] - 2 * 9.81 / 0.01) < 1e-2, "Maglev at 10 mm: 2g/x₀ = 1962 s⁻²."
```

::: right
Right. Central differences cancel the second-order term, so halving h cuts the error fourfold until rounding takes over.
:::
:::

::: check
kind: python

## Write `step_residues(poles)`: for distinct real poles p (all negative) of $T(s) = \prod(-p_i)/\prod(s - p_i)$, return the coefficients $r_k$ of $e^{p_kt}$ in its step response.

```python starter
def step_residues(poles):
    return [0.0] * len(poles)
```

```python solution
def step_residues(poles):
    num = 1.0
    for p in poles:
        num *= -p
    out = []
    for k, pk in enumerate(poles):
        den = pk
        for j, pj in enumerate(poles):
            if j != k:
                den *= pk - pj
        out.append(num / den)
    return out
```

```python tests
r = step_residues([-1.0, -10.0])
assert abs(r[0] + 10 / 9) < 1e-12 and abs(r[1] - 1 / 9) < 1e-12, "Poles −1 and −10: −10/9 and 1/9."
r = step_residues([-1.0, -2.0, -20.0])
assert abs(sum(r) + 1) < 1e-12, "The response starts at zero: the residues sum to −1."
assert abs(r[2]) < 0.01, "A pole ten times farther out has a residue under 1%."
```

::: right
Right. A far pole's residue is small and its term dies fast: twice over, it barely shows in the response.
:::
:::

::: explain
## Why is it worth computing step-response metrics from data rather than reading them from the ζ and ω_n formulas?

::: model
The formulas for overshoot, peak time and settling time are exact only for a pure second-order system with no zeros. Real loops have extra poles, zeros, delays and nonlinearities that shift those numbers, sometimes a lot, as lesson 8's tables show. Measuring the metrics directly from a simulated or recorded response gives the true values for the actual system, and comparing them with the formulas shows how well the dominant-pole approximation holds. The same code works on measured data from the real plant, which is how specifications are finally verified.
:::
:::
