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

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 1/ω0). 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 h=0.2/ω0. After many steps, what has happened to the simulated energy?

Name it

Stepping a state equation

Euler for systems. The state equation 𝐱˙=𝐟(t,𝐱) 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:

𝐱k+1=𝐱k+h𝐟(tk,𝐱k)

That's A4's method with vectors in place of numbers, and it has the same weakness: its error shrinks only in proportion to h. For the LC circuit in its scaled state form (x˙=y, y˙=−x), each step multiplies the state by a matrix whose size is 1+h2, so the amplitude creeps outward.

Runge–Kutta (RK4). Sample the rate four times in each step and take a weighted average:

𝐤1=𝐟(tk,𝐱k)𝐤2=𝐟(tk+h/2,𝐱k+h2𝐤1)𝐤3=𝐟(tk+h/2,𝐱k+h2𝐤2)𝐤4=𝐟(tk+h,𝐱k+h𝐤3)𝐱k+1=𝐱k+h6(𝐤1+2𝐤2+2𝐤3+𝐤4)

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 h4 term, so RK4 is a fourth-order method: halving the step cuts the error by 24=16.

MethodRate samples per stepHalve h and the error falls by
Euler12
midpoint (RK2)24
RK4416

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 h 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 y˙=−y from y(0)=1 with h=0.1: what does it give at t=1?

Show the solution
  1. One step. Each step multiplies by 0.9.
  2. Ten steps. y(1)≈0.910=0.3487
  3. Compare. The exact value is e−1=0.3679: 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 h=0.1 on y˙=−y from 1. How close is it?

Show the solution
  1. The RK4 step. RK4 gives y1=1−0.1+0.012−0.0016+0.000124=0.9048375
  2. Compare. The exact value is e−0.1=0.9048374: an error of 8×10−8, from a single step.

Answer RK4 gives 0.9048375, within 8×10−8 of the exact value.

Worked example

Euler's method on y˙=−y from y(0)=1: compare the error at t=1 for h = 0.1 and h = 0.05.

Show the solution
  1. The step factor. Each step multiplies by 1−h.
  2. With h=0.1. Ten steps give 0.910=0.34868 against e−1=0.36788: an error of 0.0192.
  3. With h=0.05. Twenty steps give 0.9520=0.35849 an error of 0.0094.
  4. 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 h = 0.1 and 0.0094 with h = 0.05: halving the step halves the error.

Check

Simulate the scaled ideal LC circuit x˙=y, y˙=−x from (x,y)=(1,0) with Euler's method, step h = 0.1, for 50 steps. Fill the arrays x and y, and set ratio to the energy x2+y2 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 x¨+2ζx˙+x=1 with ζ = 0.5, from rest, at t=10 (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 dv/dt=−v from v=1 to t=5. Find its error against e−5 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 h = 0.2. By what factor does each step multiply the amplitude?

Check

RK4 gives an error of 10−6 with a step of 1 µs. Roughly what error would a step of 0.5 µs give?

Check

Euler's method steps y˙=−1y from y(0)=1 to t=1 with h = 0.2.

(a)

What does it give?

(b)

What is its error?

Check

Find the factor each step multiplies by on y˙=λy.

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°.

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?

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.