Sub topic 13
Checking the analytical results numerically
You have now derived closed-form step responses for three damping cases. A sign error, a swapped root, or a wrong initial slope can still produce a formula that looks fine. This chapter shows how to test a result against an independent calculation. It also shows which checks catch which mistakes.
This chapter assumes you can write the series RLC step response in each damping case and that you know the capacitor-voltage ODE, v'' + 2αv' + ω0²v = ω0²Vs. You also need basic Python (functions, loops, math). No external libraries are used.
Why a numerical check is independent
The closed-form answer comes from the second-order equation for the capacitor voltage, a characteristic equation, and two initial conditions. A numerical check should avoid that chain. The most useful independent route is to integrate the original circuit equations directly, as two first-order equations in the state variables, capacitor voltage v and inductor current i:
- dv/dt = i / C
- di/dt = (Vs − R·i − v) / L
These come straight from the element laws and KVL. They never use α, ω0, roots, or the derived v'(0). If the integrated waveform matches your formula, the algebra and the initial conditions are probably both right. If it does not, the size and shape of the mismatch point to the mistake.
Step 1: simulate and compare
The script below uses the fourth-order Runge-Kutta method (RK4). It takes four slope estimates per step and combines them, so the error shrinks quickly as the step gets smaller. Save it as rlc_check.py.
import math
def step_response(R, L, C, Vs, t):
"""Closed-form capacitor voltage, zero initial state, step Vs at t=0."""
alpha = R / (2 * L)
w0 = 1 / math.sqrt(L * C)
if math.isclose(alpha, w0, rel_tol=1e-9):
return Vs * (1 - (1 + alpha * t) * math.exp(-alpha * t))
if alpha > w0:
root = math.sqrt(alpha**2 - w0**2)
s1, s2 = -alpha + root, -alpha - root
A1 = -Vs * s2 / (s2 - s1)
A2 = Vs * s1 / (s2 - s1)
return Vs + A1 * math.exp(s1 * t) + A2 * math.exp(s2 * t)
wd = math.sqrt(w0**2 - alpha**2)
return Vs * (1 - math.exp(-alpha * t)
* (math.cos(wd * t) + (alpha / wd) * math.sin(wd * t)))
def simulate(R, L, C, Vs, t_end, dt):
"""Integrate dv/dt = i/C, di/dt = (Vs - R*i - v)/L with RK4."""
def deriv(v, i):
return i / C, (Vs - R * i - v) / L
n = int(round(t_end / dt))
v, i = 0.0, 0.0
times, volts = [0.0], [0.0]
for k in range(n):
k1v, k1i = deriv(v, i)
k2v, k2i = deriv(v + 0.5 * dt * k1v, i + 0.5 * dt * k1i)
k3v, k3i = deriv(v + 0.5 * dt * k2v, i + 0.5 * dt * k2i)
k4v, k4i = deriv(v + dt * k3v, i + dt * k3i)
v += dt / 6 * (k1v + 2 * k2v + 2 * k3v + k4v)
i += dt / 6 * (k1i + 2 * k2i + 2 * k3i + k4i)
times.append((k + 1) * dt)
volts.append(v)
return times, volts
if __name__ == "__main__":
L, C, Vs = 1e-3, 1e-6, 10.0
R_crit = 2 * math.sqrt(L / C)
for R in (20, R_crit, 200):
ts, vs = simulate(R, L, C, Vs, 500e-6, 1e-7)
err = max(abs(v - step_response(R, L, C, Vs, t))
for t, v in zip(ts, vs))
print(f"R={R:7.2f} ohm max |error| = {err:.2e} V")The three printed errors should all be far below a microvolt. With a 0.1 µs step and the fastest dynamics near 3×10⁴ rad/s, each step covers only about 0.3% of a radian, so RK4 truncation error is negligible. If you see errors near 10⁻¹ V or larger, a formula or a parameter is wrong.
Step 2: choose the step size deliberately
The step must be small compared with the fastest time scale in the circuit. A practical rule is dt ≤ 1/(100·max(α, ω0)) for the underdamped and critical cases. For the overdamped case, use the larger of the two roots' magnitudes, |s2| = α + √(α² − ω0²). For the 200 Ω case, |s2| is about 1.95×10⁵ rad/s, so dt = 1×10⁻⁷ s gives dt·|s2| ≈ 0.02. That is comfortable for RK4.
To confirm that the step is small enough, halve dt and rerun. The error should drop by a large factor, about 16 for a fourth-order method until floating-point rounding takes over. If the error does not change, the mismatch comes from your model or formula, not from the integrator.
Step 3: targeted checks on the formula
A single max-error number tells you that something is wrong. Targeted checks tell you what. Each check below fails for a different kind of mistake.
| Check | What you compare | Mistake it catches |
|---|---|---|
| v(0) | Formula at t = 0 against the known initial voltage | Wrong constant when matching the initial value |
| Early-time shape | v(t) ≈ ½·(Vs/LC)·t² for very small t | Wrong v'(0) (a nonzero initial slope breaks the quadratic start) |
| Final value | Formula at several time constants against Vs | Wrong forced response |
| Peak and overshoot | Simulated maximum against the metric formulas | Wrong ω_d or damping ratio |
| Branch boundary | Formula just above and below critical R | Cancellation or a missing case |
The early-time check follows from the equations. With zero initial state, v(0) = 0 and v'(0) = i(0)/C = 0. The second derivative at zero is v''(0) = (Vs − R·i(0) − v(0))/(LC) = Vs/(LC). So the response starts as a parabola, not a line. A formula with a nonzero initial slope will fail this check immediately.
The second script runs these checks. It imports from the first file.
import math
from rlc_check import step_response, simulate
L, C, Vs = 1e-3, 1e-6, 10.0
R_crit = 2 * math.sqrt(L / C)
for R in (20, R_crit, 200):
# v(0) is zero
assert abs(step_response(R, L, C, Vs, 0.0)) < 1e-12
# early time: parabola with curvature Vs/(L*C)
t = 1e-7
expected = 0.5 * Vs / (L * C) * t**2
got = step_response(R, L, C, Vs, t)
assert math.isclose(got, expected, rel_tol=2e-2), (R, got, expected)
# final value after many time constants
assert abs(step_response(R, L, C, Vs, 5e-3) - Vs) < 1e-6
# Peak and overshoot for the underdamped case, R = 20 ohm
R = 20
alpha = R / (2 * L)
w0 = 1 / math.sqrt(L * C)
zeta = alpha / w0
wd = w0 * math.sqrt(1 - zeta**2)
os_formula = 100 * math.exp(-math.pi * zeta / math.sqrt(1 - zeta**2))
tp_formula = math.pi / wd
ts, vs = simulate(R, L, C, Vs, 500e-6, 1e-7)
k = max(range(len(vs)), key=vs.__getitem__)
os_sim = 100 * (vs[k] - Vs) / Vs
print(f"overshoot: formula {os_formula:.2f}% simulated {os_sim:.2f}%")
print(f"peak time: formula {tp_formula*1e6:.2f} us simulated {ts[k]*1e6:.2f} us")For this circuit, ζ ≈ 0.316 and ω_d = 30 000 rad/s. The formulas give an overshoot of about 35.1% and a peak time of about 104.7 µs. The simulated values should agree to within the time resolution of 0.1 µs. The assertions pass silently. Any failure prints the offending resistance and the two values.
Step 4: read the shape of the error
When the simulated and formula curves disagree, plot or print the difference v_sim − v_formula over time. The pattern is diagnostic:
- An error that is largest at t = 0 and decays points to wrong initial conditions or a wrong constant.
- An error that grows toward a nonzero constant at late times points to a wrong forced response, usually a wrong final value.
- A sinusoidal error at about the ringing frequency points to a wrong ω_d, or to a sine and cosine coefficient mix-up.
- An error that is zero at t = 0 and has the wrong initial curvature points to a wrong v'(0).
- An error in only one damping case means the formula for that case is wrong. A shared setup error would show up in all three.
Edge cases and failure modes
Near critical damping. The overdamped formula divides by s2 − s1 = −2√(α² − ω0²). When α is within a hair of ω0, that denominator approaches zero. The two exponentials are then nearly equal and large with opposite signs, and subtracting them loses precision. The math.isclose branch in step_response avoids this by switching to the critical formula. Try R = 63.3 Ω and R = 63.2 Ω without the branch to see how the case behaves. The correct curves are nearly identical to the critical one, so a plot will not reveal a coding error here. Compare numbers instead.
Tolerance in the classification. Floating-point values of α and ω0 rarely match exactly, so never test alpha == w0. A relative tolerance such as 10⁻⁹ is reasonable for a teaching script. For real component values, which have tolerances of 1% or worse, "critically damped" is a design target, not a state you can reach exactly.
Stiff circuits. If one root is much larger than the other, as with a very large R, the step size must resolve the fast root even though that term dies almost immediately. RK4 becomes unstable if dt·|s| exceeds about 2.8. For extreme ratios, use an implicit method or a smaller step.
Nonzero initial conditions. The same simulator handles them. Change the starting values of v and i, and set the formula's initial conditions to match. This is a strong test of your v'(0) = i(0)/C step, since the sign and the C factor are easy to get wrong.
Units. Mixing mH with H or µF with F shifts α and ω0 by orders of magnitude. If the simulation and the formula agree but the answer looks absurd, check the units against a hand estimate of ω0.
Parallel RLC
The same method checks a parallel RLC circuit. Use the state equations dv/dt = (Is − v/R − i)/C and di/dt = v/L for a current-source step. Only α changes, to 1/(2RC), and ω0 stays 1/√(LC). Swap the deriv function and the closed-form α, then repeat the same checks.
Exercise
A student's underdamped formula for the circuit above (R = 20 Ω, L = 1 mH, C = 1 µF, Vs = 10 V) uses the sine term with coefficient 1 instead of α/ωd, so v(t) = Vs(1 − e^(−αt)(cos ωd t + sin ω_d t)). The formula still gives v(0) = 0 and still settles to 10 V. Which of the checks in this chapter detects the error, and what does it show?
Show answer
The early-time shape check detects it. The derivative of the student's formula at t = 0 is Vs·α·(1 − 1) + ... evaluated fully: v'(0) = Vs(α − ωd·1 + α·... ) reduces to Vs(α − ωd + ... ), which is not zero. Specifically, differentiating gives v'(0) = Vs(α·1 − (−ωd·0 + ωd·1)) = Vs(α − ωd), which is about 10 × (10 000 − 30 000) = −2×10⁵ V/s instead of 0. The response therefore starts by moving downward, not as a parabola. The simulated curve starts with zero slope, so the error plot is zero at t = 0, grows roughly linearly at first, and then oscillates at ωd. The v(0) and final-value checks both pass, which is why the targeted early-time check is needed.
Exercise
You halve the step size dt in the RK4 simulation and the maximum error against your formula does not change at all (it stays near 0.4 V). What does this tell you?
Show answer
The integrator is not the source of the error. RK4 error should fall by a large factor when dt is halved, so a constant error means the simulation and the formula describe different problems. Likely causes are a wrong formula, wrong initial conditions, or different component values or units in the two calculations.