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

A5 · Lesson 38 · Lab: stepping through time

Solving a rate equation by taking small steps

The equation y′=(y∞−y)/τ 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 5(1−e−t/2). The dots are Euler's method with n 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 n 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?

Name it

Euler's method

For y′=f(t,y) with y(0)=y0, choose a step h and repeat

yk+1=yk+hf(tk,yk),tk+1=tk+h

Each step follows the tangent line at the start of the step. Over a span T with n=T/h steps, the error at the end is proportional to h.

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 h 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, T′=(Pθ+Ta−T)/τ; when it switches off, P=0. A stepper handles the switch by changing P inside the loop at the right time.
  • A voltage-dependent capacitor: charging through R into a capacitor whose value falls with voltage, C=C0/(1+v/2), gives v′=(Vs−v)(1+v/2)RC0. 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 y passes the level, then interpolate between the last two points for the crossing time.

Worked example

Take two Euler steps of h=0.5 s on y′=(5−y)/2 from y=0.

Show the solution
  1. First step. At y=0 the slope is 2.5: y1=0+0.5×2.5=1.25
  2. Second step. At y=1.25 the slope is 1.875: y2=1.25+0.5×1.875=2.1875
  3. Compare with the exact value. y(1)=5(1−e−0.5)=1.967 so Euler is ahead of the curve at 2.19.
  4. 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 y2=2.1875, ahead of the exact 1.967.

Worked example

A heater with P = 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
  1. While it's on. It heads toward 25+30=55 °C. After 120 s: T=25+30(1−e−2)=50.94°C
  2. After it switches off. It decays toward 25 °C. After another 120 s: T=25+25.94e−2=28.51°C
  3. In a stepper. A stepper reproduces this by setting P=0 once t 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 h. About what is it with step h/2?

Check

Take one Euler step of h = 1 on y′=(5−y)/2 from y = 0. Where does it land?

Check

Write euler(f, y0, t_end, n) returning y at t_end after n steps of Euler's method on y′=f(t,y) from y(0)= y0. Test it on y′=(5−y)/2.

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 T′=(Pθ+25−T)/60 with h = 0.01 s, switching P to 0 once t≥ 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 v′=(5−v)(1+v/2)RC0, with RC0 = 1 ms, from 0 V. Step it with h=10−7 s until v 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 y′=(5−y)/2 from 0 to t = 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?

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.