---
title: "Lab: second-order responses in code"
heading: Damping, overshoot and settling, computed and measured
eyebrow: A8 · Lesson 23 · Lab: second-order responses
xp: 60
---

::: hook
Lessons 16 to 21 gave formulas for a second-order system's response: $\zeta$ and $\omega_0$ from the parts, the overshoot from $\zeta$, the ringing from the roots. In this lab you'll compute those from component values, simulate step responses and measure them the way a scope would, estimate damping from the decay of a ringing waveform, and find out how good the familiar settling-time rule of thumb really is.
:::

::: play
- The curve is the step response of $\ddot y + 2\zeta\omega_0\dot y + \omega_0^2y = \omega_0^2u$ with $\omega_0$ = 1 rad/s.
- Lower $\zeta$: the overshoot grows and the ringing lasts longer. The readouts give the formula values your code will check.
- At $\zeta$ = 0.7 the overshoot is under 5%, a common design choice.
:::

```sim plot
view: graph
x: t = 0..30 s
y: y = 0..1.9
slider: z = 0.05..0.95 step 0.05 = 0.2
plot: 1 - exp(-z*t)*(cos(sqrt(1 - z^2)*t) + z/sqrt(1 - z^2)*sin(sqrt(1 - z^2)*t)) | label: step response | samples: 800
plot: 1 | dashed | color: ch2 | label: final value
readout: overshoot = exp(-pi*z/sqrt(1 - z^2)) | digits: 4
readout: rule-of-thumb settling time (2%), s = 4/z | digits: 4
```

::: predict
## Two successive peaks of a ringing waveform are 1.0 and 0.53. About what is $\zeta$?

- [x] About 0.1
- [ ] About 0.5
- [ ] About 0.01
- [ ] It can't be found from two peaks

::: reveal
About 0.1. The **logarithmic decrement** is $\delta = \ln\frac{1.0}{0.53} = 0.63$, and $\zeta = \frac{\delta}{\sqrt{4\pi^2 + \delta^2}} = 0.10$. Each cycle multiplies the amplitude by $e^{-2\pi\zeta/\sqrt{1 - \zeta^2}}$, so the ratio of any two successive peaks gives $\zeta$, without knowing the starting amplitude or the frequency.
:::
:::

::: nameit
## From parts to parameters

| Circuit | $\omega_0$ | $\zeta$ |
|---|---|---|
| Series RLC | $\frac{1}{\sqrt{LC}}$ | $\frac R2\sqrt{\frac CL}$ |
| Parallel RLC | $\frac{1}{\sqrt{LC}}$ | $\frac{1}{2R}\sqrt{\frac LC}$ |

## Measuring a response

- **Overshoot:** the peak's excess over the final value, as a fraction; for an underdamped step response it's $e^{-\pi\zeta/\sqrt{1 - \zeta^2}}$.
- **Logarithmic decrement:** $\delta = \ln\frac{x_k}{x_{k+1}}$ for successive peaks of a free oscillation; $\zeta = \frac{\delta}{\sqrt{4\pi^2 + \delta^2}}$.
- **Settling time:** the last time the response is outside a band (often 2%) around the final value. The rule of thumb $t_s \approx \frac{4}{\zeta\omega_0}$ comes from the envelope $e^{-\zeta\omega_0t}$ reaching 2%; the actual crossing depends on where in a cycle the response passes the band, so it's usually a little shorter.

## Simulating

Write the equation in state form, $\dot y = v$, $\dot v = \omega_0^2(u - y) - 2\zeta\omega_0v$, and step it with RK4. With 1000 steps per second of a 1 rad/s system, RK4 is accurate to many digits.
:::

::: example
## A series RLC has $R$ = 40 Ω, $L$ = 10 mH and $C$ = 1 µF. Find $\omega_0$, $\zeta$ and the step overshoot.

::: solution
1. **Natural frequency.**
   $$\begin{aligned} \omega_0 &= \frac{1}{\sqrt{10^{-8}}} \\ &= 10^4\ \text{rad/s} \end{aligned}$$
2. **Damping ratio.**
   $$\begin{aligned} \zeta &= \frac{40}{2}\sqrt{\frac{10^{-6}}{10^{-2}}} \\ &= 20 \times 0.01 \\ &= 0.2 \end{aligned}$$
3. **Overshoot.**
   $$\begin{aligned} &e^{-0.2\pi/\sqrt{0.96}} \\ &= e^{-0.641} \\ &= 0.527 \end{aligned}$$
   The capacitor voltage swings 53% past its final value before settling.

**Answer:** $\omega_0 = 10^4$ rad/s, $\zeta = 0.2$, and the step response overshoots by 53%.
:::
:::

::: example
## A system with $\zeta$ = 0.2 and $\omega_0$ = 1 rad/s. Compare the settling-time rule of thumb with the simulated value.

::: solution
1. **The rule of thumb.**
   $$\begin{aligned} t_s &= \frac{4}{0.2 \times 1}\ \text{s} \\ &= 20\ \text{s} \end{aligned}$$
2. **The simulation.** Simulating with RK4 and finding the last time $|y - 1| > 0.02$ gives 19.6 s.
3. **The envelope.** The error's envelope, $\frac{e^{-\zeta t}}{\sqrt{1 - \zeta^2}}$, reaches 2% at
   $$\begin{aligned} t &= \frac{\ln 51.0}{0.2}\ \text{s} \\ &= 19.7\ \text{s} \end{aligned}$$
   and the response, being inside its envelope, leaves the band a little earlier.
4. **Compare.** The rule is a slightly conservative estimate.

**Answer:** the rule gives 20 s and the simulation 19.6 s, so the rule is slightly conservative.
:::
:::

::: check
given: R = 20 | 50; L = 10 | 22; C = 0.47 | 1
answer: R/2*sqrt(C*1e-6/(L*1e-3))
tolerance: 0.5%

## A series RLC has $R$ = {R} Ω, $L$ = {L} mH and $C$ = {C} µF. What is $\zeta$?

::: right
Right. $\zeta = \frac R2\sqrt{C/L}$ = {answer}.
:::

::: misses
- [1/(2*R)*sqrt(L*1e-3/(C*1e-6))] That's the parallel RLC's formula.
- [any] Not yet. $\zeta = \frac R2\sqrt{C/L}$.
:::
:::

::: check
given: r = 0.5 | 0.7 | 0.8
answer: ln(1/r)/sqrt(4*pi^2 + ln(1/r)^2)
tolerance: 0.5%

## Successive peaks of a free oscillation have the ratio {r}. What is $\zeta$?

::: right
Right. $\delta = \ln(1/r)$ and $\zeta = \frac{\delta}{\sqrt{4\pi^2 + \delta^2}}$ = {answer}.
:::

::: misses
- [-ln(1/r)/sqrt(4*pi^2 + ln(1/r)^2)] Damping is positive: $\delta = \ln\frac{x_k}{x_{k+1}}$, the larger peak over the smaller.
- [any] Not yet. Start from the logarithmic decrement.
:::
:::

::: check
kind: python

## Write `params(R, L, C)` returning `(zeta, w0)` for a series RLC. Set `z, w0 = params(40, 10e-3, 1e-6)`.

```python starter
import math

def params(R, L, C):
    return 1.0, 1.0

z, w0 = params(40, 10e-3, 1e-6)
print(z, w0)
```

```python solution
import math

def params(R, L, C):
    return R / 2 * math.sqrt(C / L), 1 / math.sqrt(L * C)

z, w0 = params(40, 10e-3, 1e-6)
print(z, w0)
```

```python tests
assert abs(z - 0.2) < 1e-12 and abs(w0 - 1e4) < 1e-6, f"40 ohms, 10 mH and 1 uF give zeta = 0.2 and w0 = 10^4 rad/s; got {z}, {w0}."
assert abs(params(100, 1e-3, 1e-6)[0] - 1.5811388) < 1e-6, "100 ohms, 1 mH, 1 uF is overdamped: zeta = 1.581."
```

::: right
Right. Two lines of code that every RLC design starts from.
:::
:::

::: check
kind: python
given: z = 0.1 | 0.2 | 0.4

## Simulate the step response of $\ddot y + 2\zeta\dot y + y = 1$ ($\omega_0$ = 1, $\zeta$ = {z}) from rest with RK4, $h$ = 0.001, out to 40 s. Set `overshoot` to the peak minus 1 and `formula` to $e^{-\pi\zeta/\sqrt{1 - \zeta^2}}$.

```python starter
import math

overshoot = formula = 0.0
print(overshoot, formula)
```

```python solution
import math

def f(y, v):
    return v, 1 - 2 * z * v - y

y, v, h, peak = 0.0, 0.0, 0.001, 0.0
for _ in range(40000):
    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)
overshoot = peak - 1
formula = math.exp(-math.pi * z / math.sqrt(1 - z * z))
print(overshoot, formula)
```

```python tests
import math
assert abs(formula - math.exp(-math.pi * z / math.sqrt(1 - z * z))) < 1e-12, "formula should be exp(-pi zeta / sqrt(1 - zeta^2))."
assert abs(overshoot - formula) < 1e-5, f"The simulated overshoot should match {formula:.6f}; got {overshoot:.6f}."
```

::: right
Right. Simulation and formula agree to five digits; the remaining difference is the step size sampling the peak.
:::
:::

::: check
kind: python

## A ringing record's successive positive peaks are in the starter. Estimate $\zeta$ from each pair of neighbors by the logarithmic decrement, and set `z_est` to their average.

```python starter
import math

z_true = 0.1
r = math.exp(-2 * math.pi * z_true / math.sqrt(1 - z_true**2))
peaks = [2.0 * r**k for k in range(6)]
z_est = 0.0
print(z_est)
```

```python solution
import math

z_true = 0.1
r = math.exp(-2 * math.pi * z_true / math.sqrt(1 - z_true**2))
peaks = [2.0 * r**k for k in range(6)]
ests = []
for a, b in zip(peaks, peaks[1:]):
    d = math.log(a / b)
    ests.append(d / math.sqrt(4 * math.pi**2 + d * d))
z_est = sum(ests) / len(ests)
print(z_est)
```

```python tests
assert abs(z_est - 0.1) < 1e-9, f"The decrement method should recover zeta = 0.1 exactly from clean peaks; got {z_est}."
```

::: right
Right. On real data each pair gives a slightly different answer, and averaging over many cycles reduces the noise.
:::
:::

::: check
kind: python
given: z = 0.1 | 0.15 | 0.2 | 0.25 | 0.3 | 0.4

## Simulate the step response for $\zeta$ = {z} ($\omega_0$ = 1) with RK4, $h$ = 0.001, out to 60 s, and set `ts` to the last time $|y - 1| > 0.02$ (plus one step) and `rule` to $4/\zeta$.

```python starter
ts = rule = 0.0
print(ts, rule)
```

```python solution
def f(y, v):
    return v, 1 - 2 * z * v - y

y, v, h, ts = 0.0, 0.0, 0.001, 0.0
for n in range(1, 60001):
    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])
    if abs(y - 1) > 0.02:
        ts = (n + 1) * h
rule = 4 / z
print(ts, rule)
```

```python tests
import math
assert abs(rule - 4 / z) < 1e-12, "rule should be 4 / zeta."
env = math.log(50 / math.sqrt(1 - z * z)) / z
assert 0.5 * rule < ts <= env + 0.01, f"The measured settling time should be at most the envelope's {env:.2f} s and near the rule's {rule:.1f} s; got {ts:.3f}."
```

::: right
Right. The rule of thumb is close and slightly conservative: the response leaves the 2% band no later than its envelope does.
:::
:::

::: check
kind: python

## Design for 5% overshoot: invert $e^{-\pi\zeta/\sqrt{1 - \zeta^2}} = 0.05$ for $\zeta$, then set `R` to the series resistance that gives it with $L$ = 10 mH and $C$ = 1 µF.

```python starter
import math

zeta = R = 0.0
print(zeta, R)
```

```python solution
import math

d = math.log(0.05)
zeta = -d / math.sqrt(math.pi**2 + d * d)
L, C = 10e-3, 1e-6
R = 2 * zeta * math.sqrt(L / C)
print(zeta, R)
```

```python tests
import math
assert abs(math.exp(-math.pi * zeta / math.sqrt(1 - zeta**2)) - 0.05) < 1e-9, f"zeta should give exactly 5% overshoot; got {zeta}."
assert abs(R - 138.02) < 0.05, f"R = 2 zeta sqrt(L/C) = 138 ohms; got {R}."
```

::: right
Right. $\zeta = 0.690$, and 138 Ω: the overshoot formula inverts in closed form.
:::
:::

::: explain
## Why can the logarithmic decrement measure damping without knowing the starting amplitude or the circuit's parts?

::: model
A lightly damped free response is a sinusoid inside an exponential envelope, $Ae^{-\zeta\omega_0t}$. Successive peaks are one period apart, $T_d = \frac{2\pi}{\omega_0\sqrt{1 - \zeta^2}}$, so their ratio is $e^{-\zeta\omega_0T_d} = e^{-2\pi\zeta/\sqrt{1 - \zeta^2}}$. The amplitude $A$ cancels in the ratio, and so does $\omega_0$: the decay per cycle depends on $\zeta$ alone. That's why a scope trace of any ringing system, a filter, a mechanical mount or a power supply's output, gives its damping from two peak heights, and the frequency between them then gives $\omega_0$.
:::
:::

::: practice

::: check
given: r = 0.1..0.95
answer: ln(1/r)/sqrt(4*pi^2 + ln(1/r)^2)
tolerance: 0.5%

## $\zeta$ from a peak ratio of {r}?

::: misses
- [any] $\frac{\delta}{\sqrt{4\pi^2 + \delta^2}}$ with $\delta = \ln(1/r)$.
:::
:::

::: check
given: R = 1..200; L = 1..100; C = 0.1..10
answer: R/2*sqrt(C*1e-6/(L*1e-3))
tolerance: 0.5%

## $\zeta$ of a series RLC with {R} Ω, {L} mH, {C} µF?

::: misses
- [any] $\frac R2\sqrt{C/L}$.
:::
:::
:::
