Differential Equations in Circuits
Close a switch onto a resistor and the current is there at once. Close it onto a resistor in series with a capacitor and the capacitor voltage creeps up over milliseconds; add an inductor and it may overshoot and ring. The difference is that capacitors and inductors relate voltage and current through a derivative, so Kirchhoff's laws produce differential equations instead of algebraic ones. This lesson shows how those equations arise, what their solutions look like, and how to solve them symbolically and numerically in Python. The mathematics is in the Differential Equations course: Linear First-Order Equations, Constant Coefficients and the circuit-specific RLC Circuits. The full circuit treatment follows in First-Order RC Circuits and Second-Order Circuits.
Where the derivatives come from
The element laws (details in Capacitors in Circuits and Inductors in Circuits):
Write KCL or KVL as usual, substitute these laws, and you get an ordinary differential equation in the capacitor voltage or the inductor current. These two quantities are the state variables: they carry the stored energy (, ), so they cannot jump, and their values just before a switching event are the initial conditions.
For the RC circuit, KCL at the top of the capacitor after the switch closes:
A first-order linear equation with constant coefficients. Its solution is a constant forced part plus a decaying natural part:
Second order: two storage elements
A series RLC loop driven by gives, by KVL with ,
Trying in the homogeneous equation gives the characteristic equation with
| Case | Roots | Response |
|---|---|---|
| , overdamped | two negative real roots | sum of two decaying exponentials, no overshoot |
| , critically damped | one double root | , fastest without overshoot |
| , underdamped | , | decaying oscillation at |
Complex roots are why the complex numbers of the previous lesson keep appearing: is a growing or shrinking rotation. A circuit with independent storage elements gives an -th order equation, or equivalently first-order equations in the state variables, which is the form numerical solvers want. The Laplace transform later turns all of this into algebra.
Worked example: charging time
Given: , , , capacitor initially discharged.
Find: , at , and the time to reach .
Solve:
Check: units, . After one the capacitor is at 63.2 % (); is half a , so a value a bit under is right. At the current is : the uncharged capacitor acts as a short.
In Python: symbolic, numerical and a warning
SymPy solves the first-order equation in closed form. For the RLC case we let SciPy integrate the state equations , and compare with the formula for the underdamped case (, , ). Last, plain forward Euler shows why step size matters (Euler's Method).
import numpy as np
import sympy as sp
from scipy.integrate import solve_ivp
# 1) RC charging, symbolic: tau v' + v = Vs, v(0) = 0
t = sp.symbols("t", nonnegative=True)
R, C, Vs = sp.symbols("R C V_s", positive=True)
v = sp.Function("v")
sol = sp.dsolve(sp.Eq(R*C*v(t).diff(t) + v(t), Vs), v(t), ics={v(0): 0})
print("v(t) =", sp.simplify(sol.rhs))
vnum = sol.rhs.subs({R: 1e3, C: 10e-6, Vs: 10})
print("v(5 ms) = %.4f V" % float(vnum.subs(t, 5e-3)))
print("time to reach 9 V: %.2f ms" % (1e3 * float(sp.solve(sp.Eq(vnum, 9), t)[0])))
# 2) series RLC, numerically: state x = [vC, iL]
Rv, L, Cv, V = 100.0, 10e-3, 1e-6, 10.0
def f(t, x):
vC, iL = x
return [iL / Cv, (V - Rv * iL - vC) / L]
ts = np.array([0.1, 0.2, 0.4, 0.8]) * 1e-3
num = solve_ivp(f, (0, 1e-3), [0.0, 0.0], t_eval=ts, rtol=1e-9, atol=1e-12)
a, w0 = Rv / (2 * L), 1 / np.sqrt(L * Cv)
wd = np.sqrt(w0**2 - a**2)
exact = V * (1 - np.exp(-a * ts) * (np.cos(wd * ts) + a / wd * np.sin(wd * ts)))
print(f"alpha = {a:.0f} 1/s, omega0 = {w0:.0f} rad/s, omega_d = {wd:.1f} rad/s")
for tk, vn, ve in zip(ts, num.y[0], exact):
print(f"t = {tk*1e3:.1f} ms: solve_ivp {vn:8.4f} V, formula {ve:8.4f} V")
# 3) forward Euler on the RC circuit: error versus step size
tau = 10e-3
for h in [1.25e-3, 0.5e-3, 0.05e-3]:
vE = 0.0
for _ in range(round(5e-3 / h)):
vE += h * (10 - vE) / tau
print(f"Euler h = {h*1e3:.2f} ms: v(5 ms) = {vE:.4f} V")v(t) = V_s - V_s*exp(-t/(C*R))
v(5 ms) = 3.9347 V
time to reach 9 V: 23.03 ms
alpha = 5000 1/s, omega0 = 10000 rad/s, omega_d = 8660.3 rad/s
t = 0.1 ms: solve_ivp 3.4030 V, formula 3.4030 V
t = 0.2 ms: solve_ivp 8.4943 V, formula 8.4943 V
t = 0.4 ms: solve_ivp 11.5312 V, formula 11.5312 V
t = 0.8 ms: solve_ivp 9.7901 V, formula 9.7901 V
Euler h = 1.25 ms: v(5 ms) = 4.1382 V
Euler h = 0.50 ms: v(5 ms) = 4.0126 V
Euler h = 0.05 ms: v(5 ms) = 3.9423 V
The symbolic result is the formula above. The RLC values give : underdamped, so overshoots (it is still at at , just after the peak of at ) before settling at , and the numerical solver agrees with the formula to all printed digits. Euler's error shrinks in proportion to the step (, , ): a first-order method needs steps much smaller than . SPICE uses the trapezoidal rule and adaptive steps for exactly this reason.
Guided lab: the RC charging curve
Hand calculation. ; , ; initial current .
Simulation. The transient lab uses the same values. Its clock runs in units of ; drag the time to .
At it reads , and . Switch to "switch off" to see the discharge, , the same exponential upside down.
What you would measure. Drive the RC from a function generator set to a square wave, to (half-period , so the capacitor charges almost fully each half cycle). On the oscilloscope, measure the time from the rising edge to of the step: expect , within about to because electrolytic capacitors are typically specified at (use a film capacitor for a tighter result). The generator's output resistance adds to : instead of , a 5 % longer .
Hazards and safe setup: from a signal generator; observe the polarity of an electrolytic capacitor (a reversed one can vent) and stay well below its voltage rating.
Pitfalls
- Jumping state variables. and are continuous; the capacitor current and the inductor voltage can jump. Initial conditions come from the state variables.
- Wrong sign in the natural part. Write the solution as final value plus (initial minus final) times the exponential and the sign takes care of itself.
- Solver step too large. A step comparable to the fastest time constant gives wrong or even unstable results; check by halving it.
- Forgetting the source's own resistance. It adds to and lengthens .
Practice
- An RL circuit, , , is switched onto . Write the ODE for and give , the final current and .
- A series RLC has , . Which makes it critically damped?
- With in question 2, find the roots .
Answer
- KVL: . , , .
- gives .
- , : and (overdamped).
Test yourself
Which quantities cannot change instantly in a circuit with finite voltages and currents?
- capacitor current and inductor voltage
- capacitor voltage and inductor current
- all voltages
- resistor currents
An RC circuit has and . What is ?
A series RLC has and . Its step response is…
- overdamped
- critically damped
- underdamped: it overshoots and rings
- unstable
Forward Euler with step has an error at a fixed time that is roughly…
- independent of
- proportional to
- proportional to
- proportional to