Page - 283 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 283 -
Text of the Page - 283 -
8.6 Exercises 283
A general vector ODE u′ = f(u,t), where u and f are vectors, can use this
approximationas follows:
un−un−1
Δt =f(un,tn),
which leads toanequationfor the newvalueun:
un−Δtf(un,tn)=un−1 .
Fora generalf , this is a systemofnonlinearalgebraicequations.
However, theODE(8.43)–(8.44)is linear, so aBackwardEuler scheme leads to
asystem of two algebraicequationsfor twounknowns:
un−Δtvn=un−1, (8.89)
vn+Δtω2un=vn−1 . (8.90)
a) Solve thesystemforun andvn.
b) Implement the found formulas for un and vn in a program for computing the
entirenumerical solutionof (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 problemsu′′ +
ω2u = 0 gives growing amplitudes, the Backward Euler method leads to signifi-
cantlydampedamplitudes.
Exercise8.20:Use Heun’sMethod for theSIR Model
Makea programthat computes the solutionof the SIRmodel fromSect. 8.3.1both
by the Forward Euler method and by Heun’s method (or equivalently: the second-
order 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.
Exercise8.21:Use OdespytoSolvea Simple ODE
Solve
u′ =−au+b, u(0)=U0, t∈ (0,T]
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik