A441 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

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?

Name it

Euler's method

A capacitor C charging from a source Vs through R obeys

dvdt=Vs−vRC

The rate of change is known from where you are now: big when v is far from Vs, small as it gets close. Euler's method pretends that rate stays fixed for a short time Δt:

vk+1=vk+Δt⋅Vs−vkRC

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 Δt: 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 Vs−v by (1−Δt/τ) every step. If Δt>2τ, 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 dv/dt=(1−v)/τ from v=0, with τ=1 and Δt=0.1, by hand and in code.

Show the solution
  1. Write the Euler step. Each step adds Δt×(1−v).
  2. Step by hand. v1=0+0.1×1=0.1v2=0.1+0.1×0.9=0.19v3=0.19+0.1×0.81=0.271
  3. Step in code.
v = 0.0
for k in range(3):
    v = v + 0.1 * (1 - v)
print(v)      # 0.271
  1. Compare with the exact value. v(0.3)=1−e−0.3=0.259 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 dv/dt=−v/τ with Δt=3τ?

Show the solution
  1. Find the factor per step. Each step multiplies v by 1−Δtτ=1−3=−2
  2. Run it from 1. The values go −2, 4, −8, ...: the "simulation" explodes while the real voltage decays to zero.
  3. Know the limits. Euler's method on this equation is only stable for Δt<2τ, and only accurate for Δt much smaller than τ.

Answer the simulation blows up, flipping sign and doubling every step, because Δt=3τ is past the stability limit of 2τ.

Check

Simulate a capacitor charging toward Vs = 6 V from 0 V, with τ = 1 ms, using Euler's method with Δt = 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 dv/dt=−v/τ from 1 V over 5τ twice, with Δt=τ/20 and Δt=τ/40. Set err1 and err2 to each run's largest error against the exact e−t/τ, 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 dv/dt=−v/τ with Δt=3τ. 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 t=0, so di/dt=(V−Ri)/L. Step it with Euler's method from i=0 for 5 time constants (τ=L/R), with Δt=τ/100. Set i_end to the final current and err to its difference from the exact VR(1−e−5).

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 dv/dt=−v/τ multiplies v by 1−r each step, with r=Δt/τ. For each r in the starter's list, run 60 steps from v=1. Set decays to a list of booleans saying whether |v| ended below 1, and flips to a list saying whether v 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 dv/dt=−v/τ from v=1 V with Δt=0.5τ. What is v after 3 steps?

Explain why

Why does Euler's method get more accurate as the step gets smaller?

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.