8.6 Exercises
283
A general vector ODE u = f (u, t), where u and f are vectors, can use this
approximation as follows:
u n − u n−1
Δt
= f (u
n , t n ),
which leads to an equation for the new value u n :
u
n
− Δtf (u
n , t n ) = u
n−1 .
For a general f , this is a system of nonlinear algebraic equations.
However, the ODE (8.43)–(8.44) is linear, so a Backward Euler scheme leads to
a system of two algebraic equations for two unknowns:
u
n
− Δtv
n
= u
n−1 ,
(8.89)
v
n
+ Δtω
2 u
n
= v
n−1 .
(8.90)
a) Solve the system for u n and v n .
b) Implement the found formulas for u n and v n in a program for computing the
entire numerical solution of (8.43)–(8.44).
c) Run the program with a Δt corresponding to 20 time steps per period of the
oscillations (see Sect. 8.4.3 for how to find such a Δt). What do you observe?
Increase to 2000 time steps per period. How much does this improve the
solution?
Filename: osc_BE.py.
Remarks While the Forward Euler method applied to oscillation problems u +
ω 2 u = 0 gives growing amplitudes, the Backward Euler method leads to significantly damped amplitudes.
Exercise 8.20: Use Heun’s Method for the SIR Model
Make a program that computes the solution of the SIR model from Sect. 8.3.1 both
by the Forward Euler method and by Heun’s method (or equivalently: the secondorder Runge-Kutta method) from Sect. 8.4.5. Compare the two methods in the
simulation case from Sect. 8.3.3. Make two comparison plots, one for a large and
one for a small time step. Experiment to find what “large” and “small” should be:
the large one gives significant differences, while the small one lead to very similar
curves.
Filename: SIR_Heun.py.
Exercise 8.21: Use Odespy to Solve a Simple ODE
Solve
u
= −au + b, u(0) = U 0 , t ∈ (0, T ]
Précédent

- 303/350

Suivant