A6 · Lesson 19 · Lab: differential equations in code
Stepping equations you can and can't solve
Separation of variables solved the logistic equation, the draining tank and quadratic decay exactly. A computer can solve all of them, and the many that don't separate, by stepping: Runge–Kutta, which A4 introduced, takes four slope samples per step and is accurate enough for almost anything. In this lab you'll check it against the exact solutions you found, then use it where formulas run out: finding where a heating part settles, deciding whether it runs away, and timing a tank to the last drop.
- The curve is the temperature rise of a part that heats itself, , for the loop gain you choose.
- Below it settles at ; at and above 1 it never settles.
- Move toward 1 and watch the settling level and the time it takes both grow.
Predict
A part's loop gain rises from 0.5 to 0.9. How does its settling temperature rise change, and how long does it take to settle?
Both grow 5 times. The settled rise is , which goes from to , and the deviation decays as , with a time constant that also grows from to . Near the system is barely stable: slow and very hot.
Name it
Runge–Kutta 4
For with step :
Its error goes as , so a modest number of steps gives many digits.
Finding where it settles
Step until the rate falls below a small tolerance, then report . If grows past a cap instead, report that it runs away. That's an empirical stability test, and it agrees with the sign of at the equilibrium.
Events
To time an event, such as a tank running dry, step until the quantity crosses the level, then interpolate between the last two steps. For a tank with , guard the square root with max(h, 0) so a step that overshoots below zero doesn't fail.
Worked example
Check RK4 on the logistic equation with = 1000, = 0.5, = 100, out to = 8 with 80 steps.
Show the solution
- Exact value.
- RK4 with . It gives 858.486 44, off by .
- Halve the number of steps. With 40 steps the error grows by about 16, as a fourth-order method's should.
Answer RK4 with 80 steps gives 858.486 44, within of the exact 858.486 45.
Worked example
A part has = 20 °C and = 0.08. Where does it settle, numerically and exactly?
Show the solution
- Step the equation. Step it with = 60 s until the rate is below °C/s: The stepper settles at 21.739 13 °C.
- Use the formula. the same value.
- Try the runaway case. With (say = 250 °C), the stepper passes 1000 °C and reports a runaway.
Answer it settles 21.739 13 °C above ambient, both numerically and from the formula.
Check
A part's rise without feedback would be 10 °C; its loop gain is 0.5. Where does it settle?
Check
For the same part, with = 30 s, what is the time constant of its settling?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
Write rk4(f, y0, t_end, n) returning for . Check it on the logistic equation ( = 1000, = 0.5, = 100) at = 8, and set ratio to its error with 40 steps divided by its error with 80.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write settle(th, a, P0=1, tau=60): step from 0 with Euler steps of 0.5 s until the rate is below (return ) or passes 1000 (return None). Set d1 = settle(th, 0.004) and d2 = settle(th, 0.02) for = 20 °C.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step a tank from = 2 m with = 0.05, using Euler steps of 1 ms, until . Set t_empty to the time, interpolated within the last step, and exact to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Use your rk4 (in the starter) on from 2 to = 4 with 40 steps. Set y_num and y_exact, and half to the time it reaches half its start, from the exact solution.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why test a numerical solver on equations you can already solve exactly?
A solver's output looks equally confident whether it's right or wrong: a bug in a coefficient or a step that uses the wrong time still produces smooth, plausible curves. Equations with known solutions are the only way to measure its actual error, and to check that the error shrinks as it should (by 16 for RK4 when the step halves). Once it passes on the logistic equation, quadratic decay and the tank, you can trust it on the nonlinear thermal problem or any other where no formula exists, because the code is the same and only the right-hand side changes. Engineers do the same with simulators: validate on circuits with closed-form answers before believing them on the ones that matter.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.