8.6 Exercises
275
as long as half of the population is not vaccinated. The campaign starts after
Δ days. That is, p = p 0 if V <
1
2 (S 0 + I 0 ) and t > Δ days, otherwise
p = 0.
Demonstrate the effect of this vaccination policy: choose β, γ , and ν as in
Sect. 8.3.9, set p = 0.001, Δ = 10 days, and simulate for 200 days.
Hint This discontinuous p(t) function is easiest implemented as a Python function
containing the indicated if test. You may use the file SIRV1.py as starting point,
but note that it implements a time-dependent p(t) via an array.
Filename: SIRV_p_adapt.py.
Exercise 8.8: Make a SIRV Model with Time-Limited Effect of Vaccination
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
effective and individuals are consequently moved from the V to the S category and
can be infected again. Mathematically, this can be modeled as an average leakage
−π −1 V from the V category to the S category (i.e., a gain π −1 V in the latter).
Write up the complete model, implement it, and rerun the case from Sect. 8.3.8 with
various choices of parameters to illustrate various effects.
Filename: SIRV1_V2S.py.
Exercise 8.9: Refactor a Flat Program
Consider the file osc_FE.py implementing the Forward Euler method for the
oscillating system model (8.43)–(8.44). The osc_FE.py code is what we often refer
to as a flat program, meaning that it is just one main program with no functions. 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
program works just as before.
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 returns u, v, t. Place the particular computational example in osc_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.
Exercise 8.10: Simulate Oscillations by a General ODE Solver
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
separate file osc_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
275
as long as half of the population is not vaccinated. The campaign starts after
Δ days. That is, p = p 0 if V <
1
2 (S 0 + I 0 ) and t > Δ days, otherwise
p = 0.
Demonstrate the effect of this vaccination policy: choose β, γ , and ν as in
Sect. 8.3.9, set p = 0.001, Δ = 10 days, and simulate for 200 days.
Hint This discontinuous p(t) function is easiest implemented as a Python function
containing the indicated if test. You may use the file SIRV1.py as starting point,
but note that it implements a time-dependent p(t) via an array.
Filename: SIRV_p_adapt.py.
Exercise 8.8: Make a SIRV Model with Time-Limited Effect of Vaccination
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
effective and individuals are consequently moved from the V to the S category and
can be infected again. Mathematically, this can be modeled as an average leakage
−π −1 V from the V category to the S category (i.e., a gain π −1 V in the latter).
Write up the complete model, implement it, and rerun the case from Sect. 8.3.8 with
various choices of parameters to illustrate various effects.
Filename: SIRV1_V2S.py.
Exercise 8.9: Refactor a Flat Program
Consider the file osc_FE.py implementing the Forward Euler method for the
oscillating system model (8.43)–(8.44). The osc_FE.py code is what we often refer
to as a flat program, meaning that it is just one main program with no functions. 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
program works just as before.
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 returns u, v, t. Place the particular computational example in osc_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.
Exercise 8.10: Simulate Oscillations by a General ODE Solver
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
separate file osc_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
