A5 · Lesson 38 · Lab: stepping through time
Solving a rate equation by taking small steps
The equation has a formula for its solution, but change it slightly (a heater that switches off, a capacitor whose value depends on its voltage) and the formula disappears. A computer doesn't need one. It starts at the known value and takes small steps, each in the direction the equation gives. That's Euler's method, the simplest idea behind every circuit simulator's transient analysis. In this lab you'll write it, measure how its error shrinks with the step, and use it on two problems with no tidy formula.
- The solid curve is the exact charging curve . The dots are Euler's method with steps over 4 s: each step moves along the slope at its start.
- With few steps the dots run above the curve: each step uses the slope at its start, which is steeper than the average over the step, so it overshoots.
- Double and watch the error at the end roughly halve.
Predict
Euler's method with 20 steps ends up 0.047 off. About how far off will it be with 40 steps?
About 0.023, half as far. Euler's method is first order: each step's error goes as , and there are steps, so the error at the end goes as . Halving the step halves the error. That's slow, which is why simulators use higher-order methods such as Runge–Kutta (A4) for most work, but Euler's method shows the idea plainly.
Name it
Euler's method
For with , choose a step and repeat
Each step follows the tangent line at the start of the step. Over a span with steps, the error at the end is proportional to .
Checking a stepper
Run it on a problem with a known answer, at two step sizes. If the errors shrink by the factor the method promises (2 for Euler when halves), the code is right; if not, look for an off-by-one in the loop or a slope evaluated at the wrong time.
Problems with no formula
- A switched heater: while the heater runs, ; when it switches off, . A stepper handles the switch by changing inside the loop at the right time.
- A voltage-dependent capacitor: charging through into a capacitor whose value falls with voltage, , gives . It speeds up as the voltage rises. It can be integrated by partial fractions, which gives a check, but the stepper doesn't need that.
Stopping when a quantity first crosses a level is done in the loop too: step until passes the level, then interpolate between the last two points for the crossing time.
Worked example
Take two Euler steps of s on from .
Show the solution
- First step. At the slope is 2.5:
- Second step. At the slope is 1.875:
- Compare with the exact value. so Euler is ahead of the curve at 2.19.
- Why it overshoots. The curve flattens as it rises, and each step used the steeper slope from its start, so it overshot.
Answer after two steps Euler gives , ahead of the exact 1.967.
Worked example
A heater with = 10 W, = 3 °C/W and = 60 s starts at room temperature, 25 °C, runs for 120 s and switches off. What does it read at 240 s?
Show the solution
- While it's on. It heads toward °C. After 120 s:
- After it switches off. It decays toward 25 °C. After another 120 s:
- In a stepper. A stepper reproduces this by setting once passes 120 s.
Answer it reads 28.51 °C at 240 s.
Check
Euler's method has an error of 0.05 at the end with step . About what is it with step ?
Check
Take one Euler step of = 1 on from = 0. Where does it land?
Check
Write euler(f, y0, t_end, n) returning at t_end after n steps of Euler's method on from y0. Test it on .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A heater ( = 3 °C/W, = 60 s) starts at 25 °C, runs at P = 5 W until t1 = 60 s, then switches off. Step with = 0.01 s, switching to 0 once t1, and set T_end to the temperature at 240 s and T_peak to the highest temperature reached.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A capacitor whose value falls with voltage charges as , with = 1 ms, from 0 V. Step it with s until first reaches V = 4 V, and set t_cross to the crossing time, interpolated between the last two steps.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write heun(f, y0, t_end, n), the improved Euler method: from each point, take an Euler step to get a trial value, then use the average of the slopes at the start and at the trial point. Show it's second order: set ratio to the error with 10 steps divided by the error with 20, for from 0 to = 2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does halving the step only halve Euler's error, when each step's own error shrinks by four?
Within one step, Euler's method follows the tangent line, and the curve departs from its tangent by an amount proportional to , so each step adds an error of order . Halving cuts that per-step error by four, but it also doubles the number of steps needed to cover the same span. The errors accumulate over all steps, so the total is about , proportional to . Halving the step therefore only halves the final error. Methods that match the curve's bending within each step, like the midpoint method or Runge–Kutta, make the per-step error a higher power of and so improve much faster as the step shrinks.
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.