Page - 274 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 274 -
Text of the Page - 274 -
274 8 SolvingOrdinaryDifferentialEquations
runsa numberof steps inode_FEandcompares the computedsolution with the
listed formulaforun.
Filename:test_ode_FE.py.
Exercise8.4: Implement andEvaluateHeun’sMethod
a) Asecond-orderRunge-Kuttamethod,alsoknownhasHeun’smethod, isderived
in Sect. 8.4.5. Make a functionode_Heun(f, U_0, dt, T) (as a counterpart
toode_FE(f, U_0, dt, T) inode_FE.py) forsolvingascalarODEproblem
u′ = f(u,t), u(0) = U0, t ∈ (0,T], with this method using a time step size
Δt.
b) Solve the simple ODE problemu′ = u, u(0) = 1, by the ode_Heun and the
ode_FE function. Make a plot that compares Heun’s method and the Forward
Euler method with the exact solutionu(t) = et for t ∈ [0,6]. Use a time step
Δt=0.5.
c) For the case in b), find through experimentation the largest value ofΔt where
the exact solution and the numerical solution by Heun’s method cannot be
distinguished visually. It is of interest to see how far off the curve the Forward
Euler method is when Heun’s method can be regarded as “exact” (for visual
purposes).
Filename:ode_Heun.py.
Exercise8.5:Find anAppropriateTime Step;LogisticModel
Compute the numerical solution of the logistic equation for a set of repeatedly
halved time steps: Δtk = 2−kΔt, k = 0,1,.. .. Plot the solutions corresponding
to the last two time stepsΔtk andΔtk−1 in the same plot.Continuedoing thisuntil
you cannot visually distinguish the two curves in the plot. Then one has found a
sufficientlysmall time step.
Hint Extendthelogistic.pyfile. Introducea loopoverk,writeoutΔtk, andask
theuser if the loop is tobecontinued.
Filename:logistic_dt.py.
Exercise8.6:Find anAppropriateTime Step;SIR Model
RepeatExercise8.5 for theSIRmodel.
Hint Import the ode_FE function from the ode_system_FEmodule and make a
modifieddemo_SIR function thathasa loopover repeatedlyhalvedtimesteps.Plot
S, I, andRversus timefor the two last timestep sizes in thesame plot.
Filename:SIR_dt.py.
Exercise8.7:Modelan AdaptiveVaccinationCampaign
In the SIRV model with time-dependent vaccination from Sect. 8.3.9, we want to
test the effect of an adaptive vaccination campaign where vaccination is offered
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