A8 · Lesson 45 · Numerical solutions
Solving any state equation with small steps
Most circuits have no neat formula for their response: a diode, a transistor or a dozen parts is enough to make the algebra hopeless. A computer solves them anyway, by stepping the state equation forward in small steps, the way A4 stepped one capacitor. But try Euler's method on a lossless LC circuit and something strange happens: the simulated circuit gains energy every cycle, out of nowhere. A better recipe for each step fixes that almost completely, and it's the one used across science and engineering.
Play
- The exact path of an ideal LC circuit in its state plane (scaled so the radius is 1) is a circle, traveled clockwise. The spiral is where Euler's method takes it, step by step. RK4's path sits so close to the circle that it hides it.
- Raise the step size h (in units of ). What does Euler's path do? Does RK4's path leave the circle?
- Raise the number of steps. The readouts show how much energy each method has gained or lost.
Predict
Euler's method steps an ideal LC circuit with . After many steps, what has happened to the simulated energy?
It has grown. Each Euler step heads along the tangent to the circle, and a tangent line always ends up outside the circle, so every step lands a little farther out. For this system the energy grows by a factor per step, 4% at , and it compounds: after 30 steps it's more than three times what it should be. Shrinking h only slows the drift. RK4's error per step is about , so tiny that it barely moves off the circle.
Name it
Stepping a state equation
Euler for systems. The state equation gives the rate of change of the whole state vector. Euler's method uses the rate at the start of each step for the whole step:
That's A4's method with vectors in place of numbers, and it has the same weakness: its error shrinks only in proportion to . For the LC circuit in its scaled state form (, ), each step multiplies the state by a matrix whose size is , so the amplitude creeps outward.
Runge–Kutta (RK4). Sample the rate four times in each step and take a weighted average:
The first sample is at the start, two are at the middle (the second using the first to get there), and one is at the end. The weights 1, 2, 2, 1 are Simpson's rule from A6. The result matches the true solution's Taylor series through the term, so RK4 is a fourth-order method: halving the step cuts the error by .
| Method | Rate samples per step | Halve h and the error falls by |
|---|---|---|
| Euler | 1 | 2 |
| midpoint (RK2) | 2 | 4 |
| RK4 | 4 | 16 |
Four samples per step costs four times the work of Euler, but for the same accuracy RK4 can usually take steps hundreds of times longer, or more.
Choosing the step. A good step is small compared with the fastest thing the circuit does: its shortest time constant or ringing period. Real simulators estimate their own error each step and adjust automatically. Circuits with both very fast and very slow parts, called stiff, need special implicit methods that stay stable with long steps. The circuit simulator in this course uses the exact solution for linear circuits and an adaptive, implicit fourth-order method once diodes and transistors are involved.
Worked example
Euler's method on from with : what does it give at ?
Show the solution
- One step. Each step multiplies by .
- Ten steps.
- Compare. The exact value is : an error of 0.019, about 5%.
Answer Euler gives 0.3487 against the exact 0.3679, an error of about 5%.
Worked example
One RK4 step of on from 1. How close is it?
Show the solution
- The RK4 step. RK4 gives
- Compare. The exact value is : an error of , from a single step.
Answer RK4 gives 0.9048375, within of the exact value.
Worked example
Euler's method on from : compare the error at for = 0.1 and = 0.05.
Show the solution
- The step factor. Each step multiplies by .
- With . Ten steps give against : an error of 0.0192.
- With . Twenty steps give an error of 0.0094.
- Compare. Halving the step halves the error, the signature of a first-order method. RK4, being fourth order, would cut it by 16.
Answer the error is 0.0192 with = 0.1 and 0.0094 with = 0.05: halving the step halves the error.
Check
Simulate the scaled ideal LC circuit , from with Euler's method, step = 0.1, for 50 steps. Fill the arrays x and y, and set ratio to the energy at the end divided by the energy at the start.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write rk4(f, t, s, h), one RK4 step for a state vector s. Use it to find the step response of with ζ = 0.5, from rest, at (200 steps of 0.05). Set x_end.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The function below runs RK4 on from to . Find its error against with step 0.2 (err1) and with half that step (err2), and set 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 the scaled LC circuit with = 0.2. By what factor does each step multiply the amplitude?
Check
RK4 gives an error of with a step of 1 µs. Roughly what error would a step of 0.5 µs give?
Check
Euler's method steps from to with = 0.2.
(a)
What does it give?
(b)
What is its error?
Check
Find the factor each step multiplies by on .
Step 1
For forward Euler?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
For backward Euler?
Explain why
Why does Euler's method make a lossless LC circuit gain energy, and why doesn't making the step smaller fully fix it?
Each Euler step moves along the tangent to the true path, which for an LC circuit is a circle in the state plane. A tangent always leads outside the circle, so every step lands slightly farther from the center: the simulated energy grows by a factor per step. A smaller step makes each gain smaller, but it also takes more steps to cover the same time, and the gains compound, so the energy still drifts upward, just more slowly. A higher-order method like RK4 follows the curve so closely that the drift is negligible for practical step sizes.
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.