248
8 Solving Ordinary Differential Equations
nonlinear equations, we can approximate or predict u n+1 using a Forward Euler
step:
u
n+1
= u
n
+ Δtf (u
n , t n ) .
This reasoning gives rise to the method
u
∗
= u
n
+ Δtf (u
n , t n ),
(8.57)
u
n+1
= u
n
+
Δt
2
(f (u
n , t n ) + f (u
∗ , t n+1 )) .
(8.58)
The scheme applies to both scalar and vector ODEs.
For an oscillating system with f = (v, −ω 2 u) the file osc_Heun.py implements
this method. The demo function in that file runs the simulation for 10 periods with 20
time steps per period. The corresponding numerical and exact solutions are shown
in Fig. 8.24. We see that the amplitude grows, but not as much as for the Forward
Euler method. However, the Euler-Cromer method performs better!
We should add that in problems where the Forward Euler method gives satisfactory approximations, such as growth/decay problems or the SIR model, the secondorder Runge-Kutta method (Heun’s method) usually works considerably better and
produces greater accuracy for the same computational cost. It is therefore a very
valuable method to be aware of, although it cannot compete with the Euler-Cromer
scheme for oscillation problems. The derivation of the RK2/Heun scheme is also
good general training in “numerical thinking”.
Fig. 8.24 Simulation of 10 periods of oscillations by Heun’s method
Précédent

- 268/350

Suivant