Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 Β· Projects and Practice

Project: Simulate a Series RLC Circuit

Build a simulator for a series RLC circuit in Python: solve the differential equation numerically, compare with the exact solution, and explore damping and resonance.

You now know many formulas for circuits, but formulas describe a circuit only in special cases. A computer can follow the circuit moment by moment, using the basic laws directly. In this project you will build a small simulator for a series circuit with a resistor, an inductor and a capacitor, connected to a battery that is switched on at t = 0. You will check it against the exact solution, then explore what happens when you change the resistance and when you drive the circuit with an AC source.

The simulator is a numerical model written for this lesson, not a lab measurement. Its value is that it applies the loop rule and the component laws you learned, without simplifying assumptions beyond ideal components.

Step 1: write the physics as equations

Apply the loop rule to a series loop with source V, resistor R, inductor L and capacitor C:

Text
V = R i + L di/dt + q / C,      with i = dq/dt

That is a single second-order equation for the charge q, but simulators prefer first-order equations, so we use the two state variables q (the charge on the capacitor) and i (the current):

Text
dq/dt = i
di/dt = (V - R i - q / C) / L

Everything else follows: the voltage across the capacitor is v_C = q / C, the resistor's is R i, and the inductor's is L di/dt.

flowchart LR
  A["State: q and i"] --> B["Loop rule gives di/dt"]
  B --> C["Step forward in time"]
  C --> A
  C --> D["Record v_C, i, energy"]

The simulation loop: compute derivatives from the loop rule, then advance.

Step 2: choose parameters and predict behavior

Take R = 20 Ξ©, L = 10 mH, C = 10 ΞΌF and V = 10 V. Before running anything, predict:

  • The natural angular frequency is Ο‰β‚€ = 1 / √(L C) = 1 / √(1.0Γ—10⁻⁷) β‰ˆ 3162 rad/s.
  • The damping rate is Ξ± = R / (2 L) = 20 / 0.02 = 1000 s⁻¹.
  • Because Ξ± < Ο‰β‚€, the circuit is underdamped: it will oscillate while the amplitude shrinks. The oscillation frequency is Ο‰_d = √(Ο‰β‚€Β² - Ξ±Β²) = √(1.0Γ—10⁷ - 1.0Γ—10⁢) = 3000 rad/s (a convenient number).
  • The critical resistance (the dividing line) is R_c = 2 √(L / C) = 2 √1000 β‰ˆ 63.2 Ξ©.
  • The capacitor voltage will settle at V = 10 V, but will overshoot first.

The exact capacitor voltage for an underdamped step response is

Text
v_C(t) = V [ 1 - e^(-Ξ± t) ( cos(Ο‰_d t) + (Ξ± / Ο‰_d) sin(Ο‰_d t) ) ]

Step 3: simulate and compare

We will use scipy's solve_ivp, a general-purpose solver for first-order equations, and compare with the formula above.

Python Simulate the RLC step response

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The two curves lie on top of each other, which verifies the simulator. Because the code and the formula use the same physics, agreement tells you the numerics are right; it does not, by itself, prove that an actual circuit would behave this way. Real components have parasitic resistance and tolerances.

Step 4: check energy conservation

A good simulator should respect conservation of energy. At any time:

Text
energy from source = energy in L + energy in C + heat in R

The source delivers energy at power V i. The inductor stores Β½ L iΒ², the capacitor Β½ qΒ² / C, and the resistor's heat is the integral of R iΒ². The check below confirms that they balance throughout.

Python Energy bookkeeping

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The tiny imbalance comes only from the numerical integration of the power, not from the physics.

Step 5: change the damping

Now sweep the resistance through the three regimes:

Condition Name Behavior
R < 2 √(L/C) Underdamped Oscillates, decaying
R = 2 √(L/C) Critically damped Fastest return without oscillation
R > 2 √(L/C) Overdamped Slow, no oscillation
Python Underdamped, critical and overdamped

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Car suspensions and door closers are mechanical versions of the same mathematics: engineers tune them near critical damping.

Step 6: drive it with AC

Replace the battery with V(t) = Vβ‚€ sin(Ο‰ t). After the early transient dies away, the steady current amplitude should match the impedance formula Iβ‚€ = Vβ‚€ / Z from the reactance lesson. Sweep the driving frequency and watch the response peak near fβ‚€ = Ο‰β‚€ / 2Ο€ β‰ˆ 503 Hz, as in the resonance lesson.

Python Frequency sweep: simulation versus impedance formula

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Extension ideas

  • Add a second resistor in parallel with the capacitor and see how the equations change.
  • Plot q against i (a phase portrait): the underdamped circuit spirals into the equilibrium point.
  • Add a nonlinear element, such as a diode modeled by an exponential current-voltage curve, and see what the formulas can no longer predict.
  • Compute the quality factor from the width of the simulated resonance and compare it with Q = Ο‰β‚€ L / R.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. Why are two state variables (q and i) needed for a series RLC circuit?

    The loop rule contains both di/dt and q, so the system is second order. Choosing q and i as variables makes it two first-order equations.
  2. 2. With L = 10 mH and C = 10 μF, what is the critical resistance 2√(L/C)?

    L/C = 1000 Ω², √1000 β‰ˆ 31.6, and doubling gives about 63 Ξ©.
  3. 3. If R is increased above the critical value, the step response becomes…

    Above critical resistance the circuit is overdamped: it creeps to the final value without oscillating.
  4. 4. What should the energy balance of the simulation show?

    Conservation of energy requires that the source energy goes into the fields of L and C or is dissipated in R.

Summary

  • A series RLC circuit is described by dq/dt = i and L di/dt = V - R i - q/C.
  • A numerical solver follows the circuit in time; checking against the exact solution and energy conservation builds trust in it.
  • Underdamped, critically damped and overdamped regimes are separated by R = 2√(L/C).
  • The driven circuit's steady current amplitude follows Vβ‚€ / Z, with a peak near fβ‚€.
  • A simulation only models ideal components; real circuits add imperfections.

Next: Back to the course overview