Page - 225 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 225 -
Text of the Page - 225 -
8.3 SpreadingofDisease:ASystemofFirstOrderODEs 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
Asameasureof theerror,wehaveheresimplyusedthemaximumerror(pickedout
bya call tomaxandassigned todiff).
Recall that test functionsshould start with the nametest_, have no arguments,
andformulatethetestasabooleanexpressionsuccessthat isTrue if thetestpasses
andFalse if it fails.Test functionsshouldmake the test asassert success (here
successcanalso bea booleanexpressionas inassert diff < tol).
Observethatwecannotcomparediff tozero,which iswhatwemathematically
expect, because diff is a floating-point variable that most likely contains small
rounding errors. Therefore, we must comparediff 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 SpreadingofDisease:ASystemofFirstOrderODEs
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 equationswith three unknownfunctions. To derive such a model,
we can use mainly intuition, so no specific background knowledge of diseases is
required.
8.3.1 SpreadingofFlu
Imagineaboardingschoolout in thecountryside.Thisschool isasmall andclosed
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’sstaffwillbeaffected.Somequitesimplemathematicscanhelpustoachieve
insight into the dynamicsofhowthedisease spreads.
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