C225 of 30
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

Name it

Forward Euler

Write the circuit as dxdt=f(x), here dvdt=V−vτ for an RC charging toward V. Forward Euler takes the slope at the start of each step:

vk+1=vk+ΔtV−vkτ

so vk=V[1−(1−Δt/τ)k]. Its error per unit time shrinks in proportion to Δt (first order), and it's stable only if |1−Δt/τ|<1, that is Δt<2τ.

Backward Euler

Use the slope at the end of the step instead, and solve for it:

vk+1=vk+ΔtV−vk+1τ⇒vk+1=vk+ΔtV/τ1+Δt/τ

The error now shrinks by 11+Δt/τ 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 i and the capacitor voltage v:

didt=V−Ri−vLdvdt=iC

The classical Runge–Kutta method (RK4) samples the slope four times per step and combines them; its error shrinks as Δt4, 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
  1. Forward Euler after 100 steps. v=5[1−(1−0.01)100]V=3.170V
  2. Exact value at 1 ms. v=5(1−e−1)V=3.161V
  3. 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
  1. The update. Each step is vk+1=vk+154
  2. First values. 3.75, 4.69, 4.92, 4.98 V.
  3. How it behaves. Crude, but it never overshoots 5 V.
  4. 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 Δt4. 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?

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.