Skip to content
Tutorials Center
My learning

Topics

Lesson 61 of 68 ยท Projects

Project: Simulating an RLC Circuit

Build a series RLC circuit model from one second-order ODE: step response, resonance sweep, Laplace transfer function and phase plot, all in Python.

A series RLC circuit is one of the best places to see calculus working on a real device. A resistor, an inductor and a capacitor in a loop obey a single second-order differential equation, and that equation explains ringing, filtering, radio tuning and the way a power supply settles after you flip a switch.

In this project you build the model from scratch and look at it four ways. You simulate the step response numerically and check it against the exact formula. You sweep the driving frequency to find resonance. You move to the Laplace domain and read the behaviour straight off a transfer function. Finally you draw phase plots. You should be comfortable with Second-Order Linear ODEs, Solving ODEs Numerically and The Laplace Transform before you start.

The model

Put a voltage source V, a resistor R, an inductor L and a capacitor C in one loop. Kirchhoff's voltage law says the voltage drops around the loop add up to the source:

L * di/dt + R * i + q / C = V

Here i is the current and q is the charge on the capacitor, so i = dq/dt. Substituting gives one equation in q:

L * q'' + R * q' + q / C = V(t)

The capacitor voltage that you would measure with a probe is v_C = q / C. Dividing by L and naming three combinations gives the standard form used throughout vibration and circuit theory:

  • alpha = R / (2L) is the damping rate (1/s). It sets how fast oscillations die away.
  • w0 = 1 / sqrt(L*C) is the natural frequency (rad/s), the speed at which the circuit would ring with no resistance.
  • zeta = alpha / w0 is the damping ratio. Below 1 the circuit rings (underdamped), at 1 it is critically damped, above 1 it is overdamped.
  • wd = sqrt(w0**2 - alpha**2) is the damped frequency, the ringing speed that you actually see.
flowchart LR
    A["Kirchhoff's voltage law<br/>L q'' + R q' + q/C = V"] --> B["Time domain<br/>solve_ivp: q(t), i(t)"]
    A --> C["Frequency domain<br/>impedance Z(jw)"]
    A --> D["Laplace domain<br/>H(s) = 1/(LC s^2 + RC s + 1)"]
    B --> E["Step response,<br/>phase plot"]
    C --> F["Resonance curve"]
    D --> G["Poles, Bode plot"]

One equation, three viewpoints: each one answers a different engineering question.

We use R = 20 ohm, L = 0.1 H and C = 100 microfarad with a 5 V step. Those give alpha = 100, w0 = 316.2 and wd = 300 exactly, which makes the numbers easy to check.

Step 1: simulate the step response

Close the switch at t = 0 with the capacitor empty and no current. The state is the pair (q, i), and the ODE becomes a first-order system: q' = i and i' = (V - R*i - q/C) / L. That is exactly the form solve_ivp wants.

For an underdamped circuit the exact capacitor voltage is

v_C(t) = V * (1 - exp(-alpha*t) * (cos(wd*t) + (alpha/wd) * sin(wd*t)))

so we can test the numerical solution against a closed form. The maximum overshoot of any underdamped second-order system is exp(-pi * alpha / wd), which for these values is about 35 percent. It happens half a ringing period after the switch closes, at t = pi / wd, or 10.47 ms.

Python Step response: simulation versus 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.

The simulation and the formula agree to about 1e-9 volts, and the peak of 6.75 V is 35 percent above the 5 V final value, as predicted. A 35 percent overshoot on a 5 V rail is a real design problem: a chip rated for 5.5 V could be damaged. That is why engineers care about the damping ratio.

Try changing R to 63.2 (so zeta is about 1) and then to 200. The oscillation disappears, and the output rises smoothly but more slowly. You need the formula V*(1 - (1 + alpha*t)*exp(-alpha*t)) for the critically damped case, but the solve_ivp code does not change at all. That is the advantage of simulating.

Step 2: sweep the frequency and find resonance

Now drive the circuit with a sinusoidal source V * cos(w*t) and wait until the start-up behaviour has died out. Each element contributes an impedance: R for the resistor, j*w*L for the inductor and 1/(j*w*C) for the capacitor, where j is the imaginary unit (see Complex Numbers and Phasors). In series they add:

Z(w) = R + j * (w*L - 1/(w*C))

The current amplitude is V / |Z|. It is largest when the bracket is zero, which is when w*L = 1/(w*C), that is w = w0. At that frequency the inductor and capacitor cancel and only R remains, so the peak current is V / R. The quality factor Q = w0*L / R measures how sharp the peak is: the larger Q, the narrower the band of frequencies the circuit responds to, which is the principle behind radio tuners.

Python Resonance sweep

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.

All three curves peak at 50.3 Hz, the same resonant frequency, because w0 depends only on L and C. Resistance changes the height (V/R) and the width of the peak, not its position. With R = 5 the quality factor is 6.3 and the peak is tall and narrow; with R = 80 it is 0.4 and the curve is a low, wide hump.

Step 3: the Laplace transfer function

Taking the Laplace transform of the ODE with zero initial conditions turns derivatives into multiplication by s. For the capacitor voltage as output and the source voltage as input,

H(s) = V_C(s) / V(s) = 1 / (L*C*s**2 + R*C*s + 1)

The poles of H, the roots of the denominator, are s = -alpha +/- j*wd. They are the whole story of the transient: the real part gives the decay rate and the imaginary part gives the ringing frequency. Moving the poles is what filter design is about.

scipy.signal accepts the coefficients directly, and sympy can solve for the poles and confirm the algebra. The step response from signal.step should match Step 1 divided by V.

Python Transfer function, poles and Bode data

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 poles come out as -100 +/- 300j, matching -alpha +/- j*wd from Step 1. The step peak of 1.3509 per volt, multiplied by 5 V, is the 6.7546 V from the time-domain simulation, so the two methods agree. The final value is 1.0037 rather than exactly 1 only because the plot window ends at 50 ms, when the ringing has not quite died out.

The Bode data tells you the circuit is a low-pass filter: gain is near 0 dB at low frequency, peaks at 4.44 dB just below the natural frequency (near 283 rad/s, which is w0 * sqrt(1 - 2*zeta**2) for the capacitor voltage), and falls to -60 dB at 10,000 rad/s, a drop of 40 dB per decade.

Step 4: phase plots

The state (v_C, i) moves through a plane as time passes. Plotting one against the other, a phase plot, shows the dynamics without time on any axis. An underdamped circuit spirals into the equilibrium (V, 0), where the capacitor is fully charged and the current has stopped. A more heavily damped circuit takes a short, direct curve.

The second kind of phase is the angle between current and source voltage in the steady state. It is -angle(Z). Below resonance the capacitor dominates and the current leads the voltage; above resonance the inductor dominates and the current lags; exactly at resonance the phase is zero.

Python Phase plot and frequency-response phase

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.

After 0.1 s the state has settled to v = 5.00004 V and a current of about -7e-6 A, which is zero for practical purposes. The phase curve on the right falls from a large positive lead at low frequency toward a lag of up to 90 degrees at high frequency, crossing zero at the resonant frequency. A small resistance makes that crossing steep; a large resistance makes it gentle.

Extending the project

Once the base model works, these changes are all small edits and each teaches something:

  • Parallel RLC. Swap the loop for a parallel network and watch resonance become a minimum in current.
  • Square-wave source. Replace V with a function of t and see the ringing at every edge; compare with the series in Fourier Series.
  • Component tolerance. Draw R, L, C from normal distributions (see Probability and Random Variables) and plot many resonance curves to see manufacturing spread.
  • Energy check. The energy L*i**2/2 + q**2/(2*C) should never increase when V = 0; checking that is a good test of your solver tolerances.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. For a series RLC circuit, what is the natural frequency w0?

    w0 = 1 / sqrt(L*C) depends only on the inductor and capacitor. R / (2L) is the damping rate alpha.
  2. 2. In the example, zeta is about 0.32. What does that tell you about the step response?

    A damping ratio below 1 means complex poles, so the response oscillates around the final value. Here the overshoot is about 35 percent.
  3. 3. What happens to the resonant frequency when you increase R while keeping L and C fixed?

    The impedance imaginary part vanishes when wL = 1/(wC), independent of R. R sets the peak current V/R and the sharpness (quality factor).
  4. 4. The poles of H(s) = 1/(LCs^2 + RCs + 1) are -100 +/- 300j. What does the 300 represent?

    The real part (-100) is minus the damping rate alpha; the imaginary part is the damped frequency wd = sqrt(w0^2 - alpha^2) = 300 rad/s.
  5. 5. Below resonance in a series RLC circuit, the current relative to the source voltage...

    Below w0 the term -1/(wC) outweighs wL, so the impedance is capacitive and the current leads the voltage.

Summary

  • Kirchhoff's voltage law gives L*q'' + R*q' + q/C = V(t), a second-order linear ODE.
  • alpha, w0, zeta and wd describe decay rate, natural frequency, damping and ringing frequency.
  • solve_ivp reproduced the exact underdamped step response to about 1e-9 V, with a 35 percent overshoot.
  • Resonance occurs at w0 for every R; resistance sets peak height V/R and the quality factor Q = w0*L/R.
  • The transfer function 1/(L*C*s**2 + R*C*s + 1) has poles -alpha +/- j*wd, and its Bode plot shows a low-pass filter falling 40 dB per decade.
  • Phase plots show the spiral into equilibrium; the current phase swings from leading to lagging (limits +90 and -90 degrees) across resonance.

Next: Project: Heat Flow in a Rod.