Seite - 275 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 275 -
Text der Seite - 275 -
8.6 Exercises 275
as long as half of the population is not vaccinated. The campaign starts after
Δ days. That is, p = p0 if V < 12(S0 + I0) and t > Δ days, otherwise
p=0.
Demonstrate the effect of this vaccination policy: choose β, γ , and ν as in
Sect. 8.3.9, setp=0.001,Δ=10days, andsimulate for200days.
Hint Thisdiscontinuousp(t) functioniseasiest implementedasaPythonfunction
containing the indicatedif test. You may use the file SIRV1.py as starting point,
butnote that it implementsa time-dependentp(t)viaanarray.
Filename:SIRV_p_adapt.py.
Exercise8.8:Make aSIRV Model with Time-LimitedEffectofVaccination
We consider the SIRV model from Sect. 8.3.8, but now the effect of vaccination
is time-limited. After a characteristic period of time,π, the vaccination is no more
effectiveand individualsare consequentlymovedfrom the V to the S categoryand
can be infected again. Mathematically, this can be modeled as an average leakage
−π−1V from the V category to the S category (i.e., a gain π−1V in the latter).
Writeupthecompletemodel, implementit, andrerunthecasefromSect.8.3.8with
variouschoicesofparameters to illustratevariouseffects.
Filename:SIRV1_V2S.py.
Exercise8.9:Refactora FlatProgram
Consider the file osc_FE.py implementing the Forward Euler method for the
oscillatingsystemmodel(8.43)–(8.44).Theosc_FE.pycodeiswhatweoftenrefer
toasaflatprogram,meaningthat it is justonemainprogramwithnofunctions.Your
task is to refactor the code in osc_FE.py according to the specifications below.
Refactoring, means to alter the inner structure of the code, while, to a user, the
programworks just asbefore.
To easily reuse the numerical computations in other contexts, place the part
that produces the numerical solution (allocation of arrays, initializing the arrays
at time zero, and the time loop) in a function osc_FE(X_0, omega, dt, T),
which returnsu, v, t. Place theparticular computationalexample inosc_FE.py
in a function demo(). Construct the file osc_FE_func.py such that the osc_FE
function can easily be reused in other programs. In Python, this means that
osc_FE_func.py is a module that can be imported in other programs. The
requirement of a module is that there should be no main program, except in the
test block. You must therefore call demo from a test block (i.e., the block after
if __name__ == ’__main__’).
Filename:osc_FE_func.py.
Exercise8.10:Simulate OscillationsbyaGeneralODESolver
Solve the system (8.43)–(8.44) using the general solver ode_FE described in
Sect. 8.3.6. Program the ODE system and the call to the ode_FE function in a
separatefileosc_ode_FE.py.
Equip this file with a test function that reads a file with correct u values and
compares these with those computed by the ode_FE function. To find correct u
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik