TExcelTAD
Home › Circuit Theory & Network Analysis › Differential Equations in Circuits

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):

iC=C dvCdt,vL=L diLdti_C = C\,\frac{dv_C}{dt}, \qquad v_L = L\,\frac{di_L}{dt}

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 (12Cv2\tfrac{1}{2}Cv^2, 12Li2\tfrac{1}{2}Li^2), so they cannot jump, and their values just before a switching event are the initial conditions.

+ - Vs 10 V t = 0 R 1 kOhm C 10 uF + - vC i KCL at the top of C: C dvC/dt = (Vs - vC)/R tau = RC = 10 ms

For the RC circuit, KCL at the top of the capacitor after the switch closes:

C dvCdt=Vs−vCR⟺τ dvCdt+vC=Vs,τ=RCC\,\frac{dv_C}{dt} = \frac{V_s - v_C}{R} \quad\Longleftrightarrow\quad \tau\,\frac{dv_C}{dt} + v_C = V_s, \qquad \tau = RC

A first-order linear equation with constant coefficients. Its solution is a constant forced part plus a decaying natural part:

vC(t)=Vs+(vC(0)−Vs)e−t/τv_C(t) = V_s + \left(v_C(0) - V_s\right) e^{-t/\tau}

Second order: two storage elements

A series RLC loop driven by VsV_s gives, by KVL with i=C dvC/dti = C\,dv_C/dt,

LC d2vCdt2+RC dvCdt+vC=VsLC\,\frac{d^2 v_C}{dt^2} + RC\,\frac{dv_C}{dt} + v_C = V_s

Trying este^{st} in the homogeneous equation gives the characteristic equation s2+2αs+ω02=0s^2 + 2\alpha s + \omega_0^2 = 0 with

α=R2L,ω0=1LC,s1,2=−α±α2−ω02\alpha = \frac{R}{2L}, \qquad \omega_0 = \frac{1}{\sqrt{LC}}, \qquad s_{1,2} = -\alpha \pm \sqrt{\alpha^2 - \omega_0^2}
Case Roots Response
α>ω0\alpha > \omega_0, overdamped two negative real roots sum of two decaying exponentials, no overshoot
α=ω0\alpha = \omega_0, critically damped one double root −α-\alpha (A+Bt)e−αt(A + Bt)e^{-\alpha t}, fastest without overshoot
α<ω0\alpha < \omega_0, underdamped −α±jωd-\alpha \pm j\omega_d, ωd=ω02−α2\omega_d = \sqrt{\omega_0^2 - \alpha^2} decaying oscillation at ωd\omega_d

Complex roots are why the complex numbers of the previous lesson keep appearing: e(−α+jωd)te^{(-\alpha + j\omega_d)t} is a growing or shrinking rotation. A circuit with nn independent storage elements gives an nn-th order equation, or equivalently nn 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: Vs=10 VV_s = 10\ \text{V}, R=1 kΩR = 1\ \text{k}\Omega, C=10 μFC = 10\ \mu\text{F}, capacitor initially discharged.

Find: τ\tau, vCv_C at 5 ms5\ \text{ms}, and the time to reach 9 V9\ \text{V}.

Solve:

τ=RC=1000 Ω⋅10×10−6 F=10 msvC(5 ms)=10 V (1−e−0.5)=3.935 V9 V=10 V (1−e−t/τ)  ⇒  t=τln⁡10=23.03 ms\begin{aligned} \tau &= RC = 1000\ \Omega \cdot 10 \times 10^{-6}\ \text{F} = 10\ \text{ms} \\ v_C(5\ \text{ms}) &= 10\ \text{V}\,\left(1 - e^{-0.5}\right) = 3.935\ \text{V} \\ 9\ \text{V} = 10\ \text{V}\,(1 - e^{-t/\tau}) \;&\Rightarrow\; t = \tau \ln 10 = 23.03\ \text{ms} \end{aligned}

Check: units, Ω⋅F=(V/A)(A⋅s/V)=s\Omega \cdot \text{F} = (\text{V/A})(\text{A} \cdot \text{s}/\text{V}) = \text{s}. After one τ\tau the capacitor is at 63.2 % (6.32 V6.32\ \text{V}); 5 ms5\ \text{ms} is half a τ\tau, so a value a bit under 4 V4\ \text{V} is right. At t=0t = 0 the current is Vs/R=10 mAV_s/R = 10\ \text{mA}: 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 dvC/dt=iL/Cdv_C/dt = i_L/C, diL/dt=(Vs−RiL−vC)/Ldi_L/dt = (V_s - Ri_L - v_C)/L and compare with the formula for the underdamped case (R=100 ΩR = 100\ \Omega, L=10 mHL = 10\ \text{mH}, C=1 μFC = 1\ \mu\text{F}). Last, plain forward Euler shows why step size matters (Euler's Method).

python · try it yourself
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 α=5000 s−1<ω0=10 000 rad/s\alpha = 5000\ \text{s}^{-1} < \omega_0 = 10\,000\ \text{rad/s}: underdamped, so vCv_C overshoots (it is still at 11.5 V11.5\ \text{V} at 0.4 ms0.4\ \text{ms}, just after the peak of 11.63 V11.63\ \text{V} at π/ωd=0.363 ms\pi/\omega_d = 0.363\ \text{ms}) before settling at 10 V10\ \text{V}, and the numerical solver agrees with the formula to all printed digits. Euler's error shrinks in proportion to the step (0.200.20, 0.0780.078, 0.0076 V0.0076\ \text{V}): a first-order method needs steps much smaller than τ\tau. SPICE uses the trapezoidal rule and adaptive steps for exactly this reason.

Guided lab: the RC charging curve

Hand calculation. τ=10 ms\tau = 10\ \text{ms}; vC(τ)=6.32 Vv_C(\tau) = 6.32\ \text{V}, vC(5τ)=9.93 Vv_C(5\tau) = 9.93\ \text{V}; initial current 10 mA10\ \text{mA}.

Simulation. The transient lab uses the same values. Its clock runs in units of τ\tau; drag the time to t/τ=1t/\tau = 1.

+-R = 1.00 kOhmswitch closed at t = 0V = 10.0 V, C = 10.0 uFtau = RC = 10.0 mst10.0 mst/tau1.00vC6.32 Vi3.68 mAvR3.68 V0.02.55.07.510.001 tau2 tau3 tau4 tau5 tau6 tautv (V)0 A5.0 mA10 mAi63.2 %5 tau: 99.3 %vC(t), capacitor voltagei(t)

At t/τ=1t/\tau = 1 it reads τ=10.0 ms\tau = 10.0\ \text{ms}, vC=6.32 Vv_C = 6.32\ \text{V} and i=3.68 mAi = 3.68\ \text{mA}. Switch to "switch off" to see the discharge, vC=Vse−t/τv_C = V_s e^{-t/\tau}, the same exponential upside down.

What you would measure. Drive the RC from a function generator set to a 10 Hz10\ \text{Hz} square wave, 00 to 5 V5\ \text{V} (half-period 50 ms=5τ50\ \text{ms} = 5\tau, so the capacitor charges almost fully each half cycle). On the oscilloscope, measure the time from the rising edge to 63.2 %63.2\,\% of the step: expect 10 ms10\ \text{ms}, within about ±10\pm 10 to 20 %20\,\% because electrolytic capacitors are typically specified at ±20 %\pm 20\,\% (use a film capacitor for a tighter result). The generator's 50 Ω50\ \Omega output resistance adds to RR: 1050 Ω1050\ \Omega instead of 1000 Ω1000\ \Omega, a 5 % longer τ\tau.

Hazards and safe setup: 5 V5\ \text{V} 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. vCv_C and iLi_L 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 RR and lengthens τ\tau.

Practice

  1. An RL circuit, R=100 ΩR = 100\ \Omega, L=50 mHL = 50\ \text{mH}, is switched onto 12 V12\ \text{V}. Write the ODE for iLi_L and give τ\tau, the final current and iL(τ)i_L(\tau).
  2. A series RLC has L=10 mHL = 10\ \text{mH}, C=1 μFC = 1\ \mu\text{F}. Which RR makes it critically damped?
  3. With R=400 ΩR = 400\ \Omega in question 2, find the roots s1,2s_{1,2}.
Answer
  1. KVL: L diL/dt+RiL=12 VL\,di_L/dt + R i_L = 12\ \text{V}. τ=L/R=0.5 ms\tau = L/R = 0.5\ \text{ms}, iL(∞)=120 mAi_L(\infty) = 120\ \text{mA}, iL(τ)=120 mA⋅0.632=75.9 mAi_L(\tau) = 120\ \text{mA} \cdot 0.632 = 75.9\ \text{mA}.
  2. α=ω0\alpha = \omega_0 gives R=2L/C=210−2/10−6 Ω=200 ΩR = 2\sqrt{L/C} = 2\sqrt{10^{-2}/10^{-6}}\ \Omega = 200\ \Omega.
  3. α=20 000 s−1\alpha = 20\,000\ \text{s}^{-1}, ω0=10 000 rad/s\omega_0 = 10\,000\ \text{rad/s}: s=−20 000±4×108−108=−2679 s−1s = -20\,000 \pm \sqrt{4 \times 10^8 - 10^8} = -2679\ \text{s}^{-1} and −37 321 s−1-37\,321\ \text{s}^{-1} (overdamped).

Test yourself

Which quantities cannot change instantly in a circuit with finite voltages and currents?

  1. capacitor current and inductor voltage
  2. capacitor voltage and inductor current
  3. all voltages
  4. resistor currents

An RC circuit has R=2 kΩR = 2\ \text{k}\Omega and C=47 nFC = 47\ \text{nF}. What is τ\tau?

  1. 94 μs94\ \mu\text{s}
  2. 23.5 μs23.5\ \mu\text{s}
  3. 94 ms94\ \text{ms}
  4. 42.6 ms42.6\ \text{ms}

A series RLC has α=5000 s−1\alpha = 5000\ \text{s}^{-1} and ω0=10 000 rad/s\omega_0 = 10\,000\ \text{rad/s}. Its step response is…

  1. overdamped
  2. critically damped
  3. underdamped: it overshoots and rings
  4. unstable

Forward Euler with step hh has an error at a fixed time that is roughly…

  1. independent of hh
  2. proportional to hh
  3. proportional to h4h^4
  4. proportional to 1/h1/h