Sub topic 13
Checking the analytical results numerically
This chapter assumes you can write the closed-form step response for each damping case and that you have read the earlier chapters on the series RLC equation and on overshoot. You also need basic Python.
A closed-form answer can be wrong in ways that look plausible: a sign error in a coefficient, a swapped root, or a wrong initial condition. A numerical check gives an independent test. You integrate the circuit equations directly, without using the closed-form result, and compare the two.
The verification idea
The numerical solver must not share any algebra with the analytical answer. The closed form came from the second-order equation for the capacitor voltage. The solver therefore uses the first-order equations for the two stored quantities instead:
- C dv/dt = i
- L di/dt = Vs − R i − v
These are KVL and the capacitor law. The state is (v, i), and both start at zero, matching the zero-initial-condition step in this chapter.
A complete check
The script below uses a fourth-order Runge-Kutta (RK4) integrator. It needs only the standard library. The step_response function is the same closed form used earlier.
import math
def step_response(R, L, C, Vs, t):
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 check(R, L, C, Vs, t_end, dt):
def f(v, i):
return i / C, (Vs - R * i - v) / L
v, i = 0.0, 0.0
worst, peak, t_peak = 0.0, 0.0, 0.0
for k in range(round(t_end / dt)):
k1v, k1i = f(v, i)
k2v, k2i = f(v + 0.5 * dt * k1v, i + 0.5 * dt * k1i)
k3v, k3i = f(v + 0.5 * dt * k2v, i + 0.5 * dt * k2i)
k4v, k4i = f(v + dt * k3v, i + dt * k3i)
v += dt / 6 * (k1v + 2 * k2v + 2 * k3v + k4v)
i += dt / 6 * (k1i + 2 * k2i + 2 * k3i + k4i)
t = (k + 1) * dt
worst = max(worst, abs(v - step_response(R, L, C, Vs, t)))
if v > peak:
peak, t_peak = v, t
return worst, peak, t_peak
L, C, Vs = 1e-3, 1e-6, 10.0
R_crit = 2 * math.sqrt(L / C)
for R in (20, R_crit, 200):
worst, peak, t_peak = check(R, L, C, Vs, 500e-6, 0.1e-6)
print(f"R={R:7.2f} max error={worst:.2e} V "
f"peak={peak:.3f} V at {t_peak * 1e6:.1f} us")Reading the output
Look at two things for each resistance.
- The maximum error. With a 0.1 µs step the error should be far below a millivolt in all three cases. A formula with a wrong coefficient typically shows an error of volts.
- The peak. For R = 20 Ω, the damping ratio is about 0.316 and the damped frequency is 30 000 rad/s. The overshoot formula predicts exp(−πζ/√(1−ζ²)) ≈ 35.1%, so a peak near 13.51 V. The peak time π/ωd is about 104.7 µs. The simulated peak should agree to within the step size. For the critically damped and overdamped cases the peak should stay at or just below 10 V, since neither case overshoots.
Common mistakes
- Starting the solver with the wrong state. If the circuit has nonzero initial conditions, set
vandito them and change the closed form to match. - Comparing only at t = 0 and at the end. Both are easy to match. The shape in between is where wrong roots show up.
- Testing the critical case only at exactly R_crit. Also try R slightly above and below it. The three formulas should join smoothly.
Exercise
You change the script to R = 63.2456 and the maximum error is about 0.4 V. The overdamped and underdamped runs are fine. Which part of your work should you inspect first, and why?
Show answer
Inspect the critically damped formula, Vs(1 - (1 + alphat)exp(-alphat)). The other two cases pass, so the circuit equations and the solver are working. The likely fault is the critical-case coefficient: the (1 + alpha*t) term, or a missing v'(0) = 0 condition. Also check that the code actually reaches that branch, since R = 63.2456 is only close to R_crit and a rounded value can fail the isclose test.