Sub topic 14
Checking results with a numerical simulation
This chapter assumes you can write the first-order equation for an RC or RL circuit, know the step-response formula, and can read basic Python. You will integrate the circuit equation step by step and compare the result with the analytic answer. The comparison catches algebra mistakes, wrong time constants, and sign errors.
Why check numerically
An analytic result such as v_C(t) = V(1 - e^(-t/τ)) is easy to get subtly wrong. Common errors are using the wrong resistance in τ, forgetting the initial value, and flipping a sign. A simulation does not use your closed-form answer. It starts from the differential equation and the initial condition, so agreement between the two is real evidence that both are right.
The equation to integrate
For the RC step response, KVL gives the first-order equation
dv/dt = (V - v)/τ, with v(0) = 0 and τ = RC.
This says the rate of change is proportional to the remaining gap between the source and the capacitor voltage. The same form works for an RL circuit with i in place of v, V/R in place of V, and τ = L/R.
Forward Euler
Forward Euler replaces the derivative with a finite step. If the state is v at time t, the next state after a step dt is
v_next = v + dt · (V - v)/τ
You apply this repeatedly from the initial condition. The method uses only the slope at the start of each step, so it has a small error that shrinks as dt shrinks.
import math
R, C, V = 10e3, 100e-9, 5.0
tau = R * C
dt = tau / 100
steps = 500 # simulate 5 tau
v = 0.0
for _ in range(steps):
v += dt * (V - v) / tau # forward Euler on dv/dt = (V - v)/tau
t = steps * dt
exact = V * (1 - math.exp(-t / tau))
print(f"Euler: {v:.4f} V, exact: {exact:.4f} V")Here dt is τ/100, so 500 steps cover 5τ. The two printed values agree to about two decimal places, near 4.97 V. The small difference is the Euler error, not a mistake in the formula.
Choosing the step size
Forward Euler needs dt much smaller than τ. A step of τ/100 is a good start. If you use dt close to τ, the answer becomes inaccurate. If dt is larger than 2τ, the method becomes unstable and the voltage oscillates with growing size, which the real circuit never does.
| Step size dt | Behavior |
|---|---|
| τ/100 | Error well under 1% |
| τ/10 | Visible error, still stable |
| 2τ or more | Unstable, results are meaningless |
A quick test of your simulation is to halve dt and rerun. If the answer barely moves, the step is small enough.
Same check for an RL circuit
For the RL step response, the state is the inductor current and the target is V/R. Only the constants change.
import math
R, L, V = 100.0, 10e-3, 12.0
tau = L / R # 100 microseconds
i_final = V / R
dt = tau / 100
i = 0.0
for k in range(1, 501):
i += dt * (i_final - i) / tau
if k % 100 == 0:
t = k * dt
exact = i_final * (1 - math.exp(-t / tau))
print(f"t = {k // 100} tau: Euler {i*1000:.2f} mA, exact {exact*1000:.2f} mA")If you had mistakenly used τ = RL, the simulation and the formula would still agree with each other, because both would use the same wrong τ. Compute τ independently from the circuit before you trust the match.
Exercise
You simulate an RC circuit with forward Euler using dt = 3τ, and the voltage swings above and below the source value with growing size. Is the analytic formula wrong? What should you change?
Show answer
No. The formula is fine. The step is too large, so forward Euler is unstable. Reduce dt to a small fraction of τ, such as τ/100, and rerun.
Exercise
A simulation and your analytic formula agree perfectly, but both use τ = 2 ms for a circuit whose equivalent resistance seen by the capacitor you never recomputed. Why is the agreement not enough?
Show answer
Both calculations share the same τ, so an error in τ appears in both and cancels in the comparison. The check only tests the formula and the integration. You must verify τ separately from the equivalent resistance seen by the storage element.