---
title: Second-order circuits lab
heading: RLC step responses in every damping regime, measuring overshoot and settling, designing for a target, and finding Q from a sweep or a ring-down, in Python
eyebrow: C2 · Lesson 19 · Second-order lab
xp: 50
---

::: hook
Lessons 15 to 18 described second-order circuits with three numbers, $\omega_0$, ζ and Q, and formulas for overshoot, settling and bandwidth. Engineers check those formulas against waveforms: they measure an overshoot off a scope trace, read Q from how fast ringing dies, and adjust a resistor until a response meets its spec. In this lab you'll write the exact step response for every damping regime, measure it the way a scope would, design a resistor by bisection, and extract Q two independent ways.
:::

::: play
- The curve is a series RLC's capacitor voltage for a 1 V step, normalized to the natural frequency, for the damping ratio on the slider.
- Below ζ = 1 it overshoots and rings; at ζ = 1 it rises as fast as it can without overshoot; above 1 it creeps up.
- The readouts give the overshoot formula's value and the rule-of-thumb 2% settling time, $\frac{4}{\zeta\omega_0}$. Step 2 measures both from samples.
:::

```sim plot
view: graph
x: t = 0..20
y: v = 0..2
slider: z = 0.05..0.99 step 0.01 = 0.5
plot: 1 - exp(-z*t)*(cos(sqrt(1 - z^2)*t) + z/sqrt(1 - z^2)*sin(sqrt(1 - z^2)*t)) | label: capacitor voltage, ω₀t on the axis | samples: 800
marker: v = 1 | label: final value
readout: overshoot, % = exp(-pi*z/sqrt(1 - z^2))*100 | digits: 3
readout: 2% settling, in units of 1/ω₀ = 4/z | digits: 3
```

::: predict
## A series RLC has ζ = 0.5. How does its measured 2% settling time compare with the rule of thumb $\frac{4}{\zeta\omega_0}$?

- [ ] Half of it
- [x] Within a few percent of it
- [ ] Twice it
- [ ] The rule doesn't apply below ζ = 0.7

::: reveal
Within a few percent: for ζ = 0.5 and $\omega_0$ = 1000 rad/s the rule gives 8.0 ms, and the response last leaves the ±2% band at 8.08 ms. The rule comes from the envelope $e^{-\zeta\omega_0t}$ reaching 2%, which is $\frac{\ln50}{\zeta\omega_0} \approx \frac{3.9}{\zeta\omega_0}$; the oscillation's phase decides exactly when the last crossing happens.
:::
:::

::: nameit
## The step response, three ways

For a series RLC driven by a unit step from rest, with $\omega_0 = \frac{1}{\sqrt{LC}}$ and $\zeta = \frac R2\sqrt{\frac CL}$, the capacitor voltage is:

- underdamped (ζ < 1): $1 - e^{-\zeta\omega_0t}\left(\cos\omega_dt + \frac{\zeta}{\sqrt{1 - \zeta^2}}\sin\omega_dt\right)$, with $\omega_d = \omega_0\sqrt{1 - \zeta^2}$;
- critical (ζ = 1): $1 - (1 + \omega_0t)e^{-\omega_0t}$;
- overdamped (ζ > 1): $1 - \frac{s_2e^{s_1t} - s_1e^{s_2t}}{s_2 - s_1}$, with $s_{1,2} = -\omega_0(\zeta \mp \sqrt{\zeta^2 - 1})$.

## Measuring like a scope

From samples: overshoot is the peak minus the final value, as a fraction; the 2% settling time is the last time the response is more than 2% from final. Both should match the formulas, $e^{-\pi\zeta/\sqrt{1 - \zeta^2}}$ and about $\frac{4}{\zeta\omega_0}$.

## Designing to a target

Inverting the overshoot formula gives $\zeta = \frac{-\ln OS}{\sqrt{\pi^2 + \ln^2OS}}$, and then $R = 2\zeta\sqrt{\frac LC}$. A bisection on R that simulates and measures does the same without the algebra, which is how you'd tune a real circuit, or one with no formula.

## Q two ways

For a parallel RLC, Q is the center frequency over the half-power bandwidth, and equals $R\sqrt{\frac CL}$. From ringing, successive peaks shrink by the logarithmic decrement $\delta = \ln\frac{v_n}{v_{n+1}}$, and $Q \approx \frac{\pi}{\delta}$ for light damping. A sweep and a ring-down should agree.
:::

::: example
## A series RLC has L = 10 mH, C = 100 nF and R = 20 Ω. How much does its step response overshoot, and what if R is raised to critical damping?

::: solution
1. **Damping ratio.**
   $$\begin{aligned} \zeta &= \frac{20}{2}\sqrt{\frac{10^{-7}}{10^{-2}}} \\ &= 0.0316 \end{aligned}$$
2. **Overshoot.**
   $$\begin{aligned} \text{overshoot} &= e^{-\pi \times 0.0316/\sqrt{1 - 0.001}} \\ &= 0.905 \end{aligned}$$
   so it swings to 1.905 V for a 1 V step, ringing at about 5 kHz.
3. **Critical damping.** It needs
   $$\begin{aligned} R &= 2\sqrt{L/C} \\ &= 632\ \Omega \end{aligned}$$
   and then the response never overshoots.
4. **How fast it gets there.** At $t = \frac{1}{\omega_0}$ (31.6 µs) it has reached $1 - \frac2e$ = 0.264 of the way, the fastest approach possible without crossing the final value.

**Answer:** with 20 Ω the overshoot is 0.905 of the step (1.905 V for a 1 V step); with R raised to the critical 632 Ω, there is no overshoot at all.
:::
:::

::: example
## Choose R for 10% overshoot with L = 10 mH and C = 100 nF.

::: solution
1. **Log of the overshoot.**
   $$\ln 0.1 = -2.303$$
2. **Damping ratio.**
   $$\begin{aligned} \zeta &= \frac{2.303}{\sqrt{\pi^2 + 2.303^2}} \\ &= 0.591 \end{aligned}$$
3. **Resistance.**
   $$\begin{aligned} R &= 2 \times 0.591 \times \sqrt{\frac{10^{-2}}{10^{-7}}}\ \Omega \\ &= 374\ \Omega \end{aligned}$$
4. **Check by simulation.** A bisection that simulates the response and measures its overshoot lands on the same 374 Ω.
5. **With standard parts.** You'd fit 390 Ω and get 8.5%, or add a smaller resistor in series to trim it.

**Answer:** R = 374 Ω; the standard 390 Ω gives 8.5%.
:::
:::

::: example
## A ringing waveform's peaks are 1.000, 0.933, 0.871 and 0.813 V. What's the Q?

::: solution
1. **Ratio of successive peaks.** Each ratio is close to 1.072.
2. **Logarithmic decrement.**
   $$\begin{aligned} \delta &= \ln 1.072 \\ &= 0.0695 \end{aligned}$$
3. **Q.**
   $$\begin{aligned} Q &\approx \frac{\pi}{\delta} \\ &= 45 \end{aligned}$$
4. **A better estimate.** Averaging over many peaks, by fitting a line to the logarithm of the peak heights, beats using one pair.
5. **Cross-check.** The same Q measured as center frequency over bandwidth in a sweep should agree, a useful check that the circuit really is second order.

**Answer:** Q is about 45.
:::
:::

::: check
kind: python

## Step 1. Write `rlc_step(R, L, C, t)` returning the capacitor voltage of a series RLC for a 1 V step from rest, at the times in array t, for any damping.

```python starter
import numpy as np

def rlc_step(R, L, C, t):
    return 1 - np.exp(-np.asarray(t) / (R * C))

print(rlc_step(20, 10e-3, 100e-9, [0.0, 1e-4]))
```

```python solution
import numpy as np

def rlc_step(R, L, C, t):
    t = np.asarray(t, float)
    w0 = 1 / np.sqrt(L * C)
    z = R / 2 * np.sqrt(C / L)
    if abs(z - 1) < 1e-9:
        return 1 - (1 + w0 * t) * np.exp(-w0 * t)
    if z < 1:
        wd = w0 * np.sqrt(1 - z * z)
        return 1 - np.exp(-z * w0 * t) * (np.cos(wd * t) + z / np.sqrt(1 - z * z) * np.sin(wd * t))
    s1 = -w0 * (z - np.sqrt(z * z - 1))
    s2 = -w0 * (z + np.sqrt(z * z - 1))
    return 1 - (s2 * np.exp(s1 * t) - s1 * np.exp(s2 * t)) / (s2 - s1)

print(rlc_step(20, 10e-3, 100e-9, [0.0, 1e-4]))
```

```python tests
import numpy as np, math
t = np.linspace(0, 0.02, 200001)
v = rlc_step(20, 10e-3, 100e-9, t)
assert abs(v[0]) < 1e-12, "The response starts at zero."
assert abs(v.max() - 1 - 0.90538) < 1e-4, f"ζ = 0.0316 overshoots by 0.905; got {v.max() - 1}"
Rc = 2 * math.sqrt(10e-3 / 100e-9)
vc = rlc_step(Rc, 10e-3, 100e-9, np.array([math.sqrt(10e-3 * 100e-9)]))
assert abs(vc[0] - (1 - 2 / math.e)) < 1e-9, "Critically damped, it reaches 1 − 2/e at t = 1/ω₀."
vo = rlc_step(5 * Rc, 10e-3, 100e-9, t)
assert vo.max() <= 1 + 1e-9 and abs(vo[-1] - 1) < 1e-3, "Overdamped: no overshoot, still settling to 1."
```

::: right
Right. One function covers all three regimes, matched at their boundaries.
:::
:::

::: check
kind: python

## Step 2. Write `measure(t, v, final)` returning `(overshoot, settle)`: the overshoot as a fraction of `final`, and the last time |v − final| exceeds 2% of `final` (the time after that sample).

```python starter
def measure(t, v, final):
    return 0.0, 0.0

print(measure([0, 1, 2], [0, 1.2, 1.0], 1.0))
```

```python solution
import numpy as np

def measure(t, v, final):
    t = np.asarray(t, float)
    v = np.asarray(v, float)
    os = max(0.0, (v.max() - final) / final)
    outside = np.nonzero(np.abs(v - final) > 0.02 * abs(final))[0]
    settle = float(t[outside[-1] + 1]) if outside.size else 0.0
    return float(os), settle

print(measure([0, 1, 2], [0, 1.2, 1.0], 1.0))
```

```python tests
import numpy as np, math
z, w = 0.5, 1000.0
t = np.linspace(0, 0.02, 200001)
wd = w * math.sqrt(1 - z * z)
v = 1 - np.exp(-z * w * t) * (np.cos(wd * t) + z / math.sqrt(1 - z * z) * np.sin(wd * t))
os, ts = measure(t, v, 1.0)
assert abs(os - math.exp(-math.pi * z / math.sqrt(1 - z * z))) < 1e-6, f"ζ = 0.5 overshoots 16.3%; got {os}"
assert abs(ts - 8.076e-3) < 2e-5, f"It settles within 2% at 8.08 ms; got {ts}"
```

::: right
Right. The measured settling time sits within 1% of $\frac{4}{\zeta\omega_0}$ here, but it jumps around as ζ changes, depending on where a peak falls.
:::
:::

::: check
kind: python

## Step 3. Write `r_for_overshoot(L, C, target)` returning the series resistance that gives the target overshoot fraction, by bisection on R using simulated responses and your `measure`-style overshoot.

```python starter
def r_for_overshoot(L, C, target):
    return 100.0

print(r_for_overshoot(10e-3, 100e-9, 0.1))
```

```python solution
import numpy as np

def overshoot(R, L, C):
    w0 = 1 / np.sqrt(L * C)
    z = R / 2 * np.sqrt(C / L)
    wd = w0 * np.sqrt(1 - z * z)
    t = np.linspace(0, 2 * np.pi / wd, 20001)
    v = 1 - np.exp(-z * w0 * t) * (np.cos(wd * t) + z / np.sqrt(1 - z * z) * np.sin(wd * t))
    return v.max() - 1

def r_for_overshoot(L, C, target):
    lo, hi = 1e-6, 2 * np.sqrt(L / C) * 0.999
    for _ in range(80):
        mid = 0.5 * (lo + hi)
        if overshoot(mid, L, C) > target:
            lo = mid
        else:
            hi = mid
    return float(0.5 * (lo + hi))

print(r_for_overshoot(10e-3, 100e-9, 0.1))
```

```python tests
import math
R = r_for_overshoot(10e-3, 100e-9, 0.1)
assert abs(R - 373.88) < 0.5, f"10% overshoot needs 374 Ω; got {R}"
z = -math.log(0.05) / math.sqrt(math.pi**2 + math.log(0.05) ** 2)
assert abs(r_for_overshoot(1e-3, 1e-6, 0.05) - 2 * z * math.sqrt(1e-3 / 1e-6)) < 0.1, "5% overshoot matches the closed form."
```

::: right
Right. Simulate, measure, adjust: the same loop works when there's no formula to invert.
:::
:::

::: check
kind: python

## Step 4. Write `parallel_q(R, L, C)` returning Q = center frequency ÷ half-power bandwidth, found by sweeping the parallel RLC's impedance magnitude on a fine grid around resonance.

```python starter
import math

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

print(parallel_q(10e3, 10e-6, 2.533e-9))
```

```python solution
import numpy as np

def parallel_q(R, L, C):
    f0 = 1 / (2 * np.pi * np.sqrt(L * C))
    f = np.linspace(0.5 * f0, 1.5 * f0, 2000001)
    w = 2 * np.pi * f
    Z = 1 / np.abs(1 / R + 1j * w * C + 1 / (1j * w * L))
    above = f[Z >= R / np.sqrt(2)]
    return float(f0 / (above.max() - above.min()))

print(parallel_q(10e3, 10e-6, 2.533e-9))
```

```python tests
import math
Q = parallel_q(10e3, 10e-6, 2.533e-9)
assert abs(Q / (10e3 * math.sqrt(2.533e-9 / 10e-6)) - 1) < 2e-3, f"Q should be R√(C/L) = 159; got {Q}"
assert abs(parallel_q(1e4, 1e-3, 1e-9) / 10 - 1) < 2e-3, "A tank with Q = 10 matches R√(C/L) too."
```

::: right
Right. The sweep and the formula agree, as they must for a pure second-order circuit.
:::
:::

::: check
kind: python

## Step 5. Write `ringdown_q(t, v)` returning Q from a ringing record: take each cycle's peak as the largest sample between successive upward zero crossings, fit a line to the logarithm of the peak heights against cycle number, and use $Q = \frac{\pi}{\delta}$ with δ the decrement per cycle.

```python starter
def ringdown_q(t, v):
    return 10.0

print(ringdown_q([0, 1, 2], [1, 0, 1]))
```

```python solution
import numpy as np

def ringdown_q(t, v):
    v = np.asarray(v, float)
    up = np.nonzero((v[:-1] <= 0) & (v[1:] > 0))[0]
    heights = np.array([v[a:b].max() for a, b in zip(up[:-1], up[1:])])
    n = np.arange(heights.size)
    slope, _ = np.polyfit(n, np.log(heights), 1)
    return float(np.pi / -slope)

t = np.arange(0, 1e-3, 1e-7)
print(ringdown_q(t, np.exp(-t / 2e-4) * np.cos(2 * np.pi * 2e4 * t)))
```

```python tests
import numpy as np
Qt, f = 45.0, 15.9e3
t = np.arange(0, 2e-3, 1e-7)
v = np.exp(-np.pi * f * t / Qt) * np.cos(2 * np.pi * f * t)
assert abs(ringdown_q(t, v) / Qt - 1) < 0.01, f"A clean ring-down with Q = 45 should give 45; got {ringdown_q(t, v)}"
rng = np.random.default_rng(1)
assert abs(ringdown_q(t, v + rng.normal(0, 1e-4, t.size)) / Qt - 1) < 0.03, "Light noise barely matters when fitting many peaks."
```

::: right
Right. A sweep and a ring-down should give the same Q; when they don't, something else is in the circuit.
:::
:::

::: check
kind: parts
given: L = 1 | 10; C = 10 | 100; R = 10 | 50

## A series RLC has L = {L} mH, C = {C} nF and R = {R} Ω.

::: part
answer: R/2*sqrt(C*1e-9/(L*1e-3))
tolerance: 0.5%

### What's ζ?

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

::: part
answer: exp(-pi*R/2*sqrt(C*1e-9/(L*1e-3))/sqrt(1 - (R/2*sqrt(C*1e-9/(L*1e-3)))^2))*100
unit: %
tolerance: 0.5%

### How much does its step response overshoot?

::: misses
- [any] $e^{-\pi\zeta/\sqrt{1 - \zeta^2}}$.
:::
:::
:::

::: check
given: o = 2 | 5 | 10 | 20
answer: -ln(o/100)/sqrt(pi^2 + ln(o/100)^2)
tolerance: 0.5%

## What damping ratio gives {o}% overshoot?

::: right
Right. $\zeta = \frac{-\ln OS}{\sqrt{\pi^2 + \ln^2OS}}$.
:::

::: misses
- [any] Invert $OS = e^{-\pi\zeta/\sqrt{1 - \zeta^2}}$.
:::
:::

::: check
given: a = 1 | 0.95; b = 0.9 | 0.8
answer: pi/ln(a/b)
tolerance: 0.5%

## Two successive peaks of a ringing waveform are {a} and {b} V. About what's the Q?

::: right
Right. $Q \approx \frac{\pi}{\ln(v_n/v_{n+1})}$.
:::

::: misses
- [any] Logarithmic decrement δ, then $\frac\pi\delta$.
:::
:::

::: check
given: z = 0.3 | 0.5 | 0.7; w = 1 | 10
answer: 4/(z*w)
unit: ms
tolerance: 0.5%

## A second-order response has ζ = {z} and $\omega_0$ = {w} krad/s. Roughly how long does it take to settle within 2%?

::: right
Right. About $\frac{4}{\zeta\omega_0}$, from the decay envelope.
:::

::: misses
- [any] The envelope $e^{-\zeta\omega_0t}$ reaches about 2% after $\frac{4}{\zeta\omega_0}$.
:::
:::

::: explain
## Why can Q be measured either from a frequency sweep or from a ring-down, and why should they agree?

::: model
Q describes one property of a resonator, how much energy it stores compared with how much it loses per cycle. In the frequency domain, that ratio sets how narrow the resonance peak is: a resonator that loses little per cycle responds strongly only very near its natural frequency, so the half-power bandwidth is $f_0/Q$. In the time domain, the same loss per cycle sets how quickly free oscillations die: each cycle the amplitude shrinks by a fixed fraction, about $\pi/Q$. Both measurements read the same energy balance, so for a true second-order circuit they agree. If they don't, the circuit has more than one resonance, or its losses depend on amplitude.
:::
:::

::: practice

::: check
given: o = 1 | 2 | 5 | 10 | 15 | 20 | 25 | 30 | 40
answer: -ln(o/100)/sqrt(pi^2 + ln(o/100)^2)
tolerance: 0.5%

## Damping ratio for {o}% overshoot?

::: misses
- [any] $\frac{-\ln OS}{\sqrt{\pi^2 + \ln^2OS}}$.
:::
:::

::: check
given: a = 2 | 1; b = 1.8 | 0.85
answer: pi/ln(a/b)
tolerance: 0.5%

## Q from successive peaks {a} and {b}?

::: misses
- [any] $\frac{\pi}{\ln(v_n/v_{n+1})}$.
:::
:::
:::
