C2 · Lesson 25 · Transient solver lab
Stepping circuits through time in NumPy, and why step size matters
Every transient sim in this course, and every SPICE run, does the same thing: it starts from the circuit's state and takes small steps through time, using the differential equations of lessons 7 to 17 to decide each step. How it takes those steps matters a great deal. The obvious method can be wrong by a few percent with small steps and explode with large ones, while a slightly different one stays stable at any step. In this lab you'll write three steppers and see both effects.
- The exact RC charging curve (dashed) and the forward Euler approximation with a time step Δt (dots joined by lines).
- Small steps track the curve closely. Push Δt past τ and Euler overshoots; past 2τ it oscillates with growing swings.
- That instability isn't about accuracy: the exact answer never goes above 5 V, whatever the step.
Predict
Forward Euler steps an RC circuit with a time step three times its time constant. What happens?
It blows up. Each Euler step multiplies the error by , so the error flips sign and doubles every step. Forward Euler is stable only for . Circuits with both fast and slow parts (stiff circuits) force such tiny steps that simulators use implicit methods instead.
Name it
Forward Euler
Write the circuit as , here for an RC charging toward . Forward Euler takes the slope at the start of each step:
so . Its error per unit time shrinks in proportion to Δt (first order), and it's stable only if , that is .
Backward Euler
Use the slope at the end of the step instead, and solve for it:
The error now shrinks by per step, which is less than 1 for any step: unconditionally stable, though still first order. Methods like this, which solve an equation at each step, are implicit; circuit simulators use implicit methods (trapezoidal, Gear, SDIRK) for exactly this reason.
Second order and RK4
An RLC circuit has two state variables, the inductor current and the capacitor voltage :
The classical Runge–Kutta method (RK4) samples the slope four times per step and combines them; its error shrinks as , so modest steps give very accurate answers.
k1 = f(x); k2 = f(x + dt/2*k1); k3 = f(x + dt/2*k2); k4 = f(x + dt*k3)
x = x + dt/6*(k1 + 2*k2 + 2*k3 + k4)Worked example
Forward Euler charges a 1 kΩ, 1 µF circuit toward 5 V with Δt = 10 µs. What does it give after 100 steps, against the exact 1 ms value?
Show the solution
- Forward Euler after 100 steps.
- Exact value at 1 ms.
- The error. Euler is 0.3% high after one time constant, and halving the step roughly halves the error.
Answer forward Euler gives 3.170 V against the exact 3.161 V, 0.3% high.
Worked example
Backward Euler takes the same circuit with Δt = 3 ms, three time constants. What are its first values?
Show the solution
- The update. Each step is
- First values. 3.75, 4.69, 4.92, 4.98 V.
- How it behaves. Crude, but it never overshoots 5 V.
- Forward Euler with the same step. It gives 15, −15, 45, −75 V.
Answer 3.75, 4.69, 4.92 and 4.98 V, closing in on 5 V without overshooting.
Check
Forward Euler steps an RC circuit (τ = 1 ms) with Δt = 1.5 ms. By what factor does the error change each step?
Check
Above what time step does forward Euler become unstable for an RC with τ = 1 ms?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
Backward Euler steps an RC (τ = 1 ms) with Δt = 3 ms. By what factor does the error shrink each step?
Check
Forward Euler charges toward 5 V from 0 with Δt/τ = 0.1. What does it give after 10 steps?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Check
RK4's error goes as . An RK4 run has error 1 mV. What error do you expect with steps 2 times smaller?
Check
Write euler_rc(R, C, V, dt, n) and backward_rc(R, C, V, dt, n): each returns an array of n + 1 capacitor voltages, starting from 0, for an RC charging toward V.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write rk4_rlc(R, L, C, V, dt, n): step a series RLC from rest after a step to V, with state [i, v], and return an array of n + 1 capacitor voltages.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a circuit simulator use an implicit method rather than forward Euler, even though each implicit step costs more?
Forward Euler is stable only when its time step is shorter than about twice the circuit's fastest time constant. Real circuits mix fast and slow parts, nanosecond parasitics alongside millisecond filters, so forward Euler would need nanosecond steps for the whole simulation even after the fast parts have settled, millions of steps for a millisecond. An implicit method stays stable at any step size, so the simulator can take large steps whenever nothing fast is happening and small ones only where the waveforms change quickly. Solving an equation each step is far cheaper than taking a million tiny steps.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.