A4 · Lesson 41 · Stepping through time
Simulating a circuit one small step at a time
A capacitor charging through a resistor has a neat formula. Add a diode, a temperature-dependent part or a second capacitor, and the formula is gone. Simulators don't need one: they know the rate at which things change right now, take a small step, and repeat. That's the whole idea behind every circuit simulator, and you can write one in five lines.
Predict
A simulation of a capacitor charging takes steps much longer than its time constant: three times as long. What does it produce?
Nonsense. With huge steps, each step overshoots the final voltage by more than it started out missing it, so the error flips sign and grows every step. For this simple method, steps longer than twice the time constant are unstable. It's the first lesson every simulator author learns: the step has to be small compared with the fastest thing happening.
Name it
Euler's method
A capacitor charging from a source through obeys
The rate of change is known from where you are now: big when is far from , small as it gets close. Euler's method pretends that rate stays fixed for a short time :
In code:
import numpy as np
tau, Vs, dt, N = 1e-3, 5.0, 1e-5, 500
v = np.zeros(N + 1) # v[0] = 0, the starting voltage
for k in range(N):
v[k + 1] = v[k] + dt * (Vs - v[k]) / tau
t = dt * np.arange(N + 1)
for k in range(N): repeats the indented line with k = 0, 1, ..., N − 1.
How accurate? Each step uses the rate at the start of the step, which is a little too high for a charging capacitor, so Euler runs slightly ahead of the true curve. The error shrinks in proportion to : halve the step and the error halves. That makes Euler a first-order method. Real simulators use higher-order methods, where halving the step cuts the error 16 times or more, and pick the step size automatically.
How stable? For this equation, Euler's update multiplies the gap by every step. If , that factor is less than −1, and the gap grows while flipping sign. Hence the rule: keep the step well below the fastest time constant.
Worked example
Take three Euler steps of from , with and , by hand and in code.
Show the solution
- Write the Euler step. Each step adds .
- Step by hand.
- Step in code.
v = 0.0
for k in range(3):
v = v + 0.1 * (1 - v)
print(v) # 0.271
- Compare with the exact value. Euler overshoots by 0.012 here, an error that halves when the step halves.
Answer three steps give 0.1, 0.19 and 0.271, by hand and in code, against the exact 0.259.
Worked example
What happens to Euler's method on with ?
Show the solution
- Find the factor per step. Each step multiplies by
- Run it from 1. The values go −2, 4, −8, ...: the "simulation" explodes while the real voltage decays to zero.
- Know the limits. Euler's method on this equation is only stable for , and only accurate for much smaller than .
Answer the simulation blows up, flipping sign and doubling every step, because is past the stability limit of .
Check
Simulate a capacitor charging toward = 6 V from 0 V, with = 1 ms, using Euler's method with = 0.01 ms for 1000 steps. Store the voltages in v (1001 values, starting at 0).
Vs and tau (in ms) are already defined. Work in milliseconds throughout.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Measure Euler's error. Run the discharge from 1 V over twice, with and . Set err1 and err2 to each run's largest error against the exact , and ratio to err1 / err2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Euler's method steps a discharge with . What does each step multiply the voltage by?
Check
An inductor L = 10 mH in series with R = 10 Ω is switched onto V = 5 V at , so . Step it with Euler's method from for 5 time constants (), with . Set i_end to the final current and err to its difference from the exact .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Euler's method on multiplies by each step, with . For each r in the starter's list, run 60 steps from . Set decays to a list of booleans saying whether ended below 1, and flips to a list saying whether changed sign on the first step.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Euler's method steps from V with . What is after 3 steps?
Explain why
Why does Euler's method get more accurate as the step gets smaller?
Euler assumes the rate of change stays at its starting value for the whole step, but the true rate drifts during the step. The shorter the step, the less it can drift, so the straight-line guess stays closer to the real curve. Each step's error shrinks roughly with the square of the step, and there are proportionally more steps, so the total error shrinks in proportion to the step size.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.