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

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, τΔT′=θP0(1+αΔT)−ΔT, for the loop gain g=αθP0 you choose.
  • Below g=1 it settles at θP01−g; at and above 1 it never settles.
  • Move g toward 1 and watch the settling level and the time it takes both grow.

Predict

A part's loop gain αθP0 rises from 0.5 to 0.9. How does its settling temperature rise change, and how long does it take to settle?

Name it

Runge–Kutta 4

For y′=f(t,y) with step h:

k1=f(t,y),k2=f(t+h2,y+h2k1),k3=f(t+h2,y+h2k2),k4=f(t+h,y+hk3)
y←y+h6(k1+2k2+2k3+k4)

Its error goes as h4, so a modest number of steps gives many digits.

Finding where it settles

Step until the rate |y′| falls below a small tolerance, then report y. If y grows past a cap instead, report that it runs away. That's an empirical stability test, and it agrees with the sign of f′ 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 h′=−ch, 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 K = 1000, r = 0.5, y0 = 100, out to t = 8 with 80 steps.

Show the solution
  1. Exact value. y(8)=10001+9e−4=858.48645
  2. RK4 with h=0.1. It gives 858.486 44, off by 6×10−6.
  3. 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 6×10−6 of the exact 858.486 45.

Worked example

A part has θP0 = 20 °C and αθP0 = 0.08. Where does it settle, numerically and exactly?

Show the solution
  1. Step the equation. Step it with τ = 60 s until the rate is below 10−9 °C/s: τΔT′=20(1+0.004ΔT)−ΔT The stepper settles at 21.739 13 °C.
  2. Use the formula. 201−0.08=21.73913 the same value.
  3. Try the runaway case. With αθP0=1 (say θP0 = 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 y(tend) for y′=f(t,y). Check it on the logistic equation (K = 1000, r = 0.5, y0 = 100) at t = 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 τΔT′=θP0(1+αΔT)−ΔT from 0 with Euler steps of 0.5 s until the rate is below 10−9 (return ΔT) or ΔT passes 1000 (return None). Set d1 = settle(th, 0.004) and d2 = settle(th, 0.02) for θP0 = 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 h′=−cmax⁡(h,0) from h0 = 2 m with c = 0.05, using Euler steps of 1 ms, until h≤0. Set t_empty to the time, interpolated within the last step, and exact to 2h0/c.

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 y′=−ky2 from 2 to t = 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?

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.