8.3 Spreading of Disease: A System of First Order ODEs
225
def exact_solution(t):
return a*t + b
def f(u, t): # ODE
return a + (u - exact_solution(t))**m
a = 4
b = -1
m = 6
dt = 0.5
T = 20.0
u, t = ode_FE(f, exact_solution(0), dt, T)
diff = abs(exact_solution(t) - u).max()
tol = 1E-15
# Tolerance for float comparison
success = diff < tol
assert success
As a measure of the error, we have here simply used the maximum error (picked out
by a call to max and assigned to diff).
Recall that test functions should start with the name test_, have no arguments,
and formulate the test as a boolean expression success that is True if the test passes
and False if it fails. Test functions should make the test as assert success (here
success can also be a boolean expression as in assert diff < tol).
Observe that we cannot compare diff to zero, which is what we mathematically
expect, because diff is a floating-point variable that most likely contains small
rounding errors. Therefore, we must compare diff to zero with a tolerance, here
10 −15 .
You are encouraged to do Exercise 8.3 where the goal is to make a test function
for a verification based on comparison with hand-calculated results for a few time
steps.
8.3 Spreading of Disease: A System of First Order ODEs
Our aim with this section is to show in detail how one can apply mathematics
and programming to solve a system of first-order ODEs. We will do this as we
investigate the spreading of disease. The mathematical model is now a system of
three differential equations with three unknown functions. To derive such a model,
we can use mainly intuition, so no specific background knowledge of diseases is
required.
8.3.1 Spreading of Flu
Imagine a boarding school out in the country side. This school is a small and closed
society. Suddenly, one or more of the pupils get the flu. We expect that the flu may
spread quite effectively or die out. The question is how many of the pupils and the
school’s staff will be affected. Some quite simple mathematics can help us to achieve
insight into the dynamics of how the disease spreads.
Précédent

- 245/350

Suivant