Seite - 110 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 110 -
Text der Seite - 110 -
110 4 SolvingOrdinaryDifferentialEquations
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
Recall that test functionsshouldstartwith thenametest_,havenoarguments,and
formulate the test as a boolean expressionsuccess that isTrue if the test passes
andFalse if it fails. Test functionsshouldmakethe testasassert success(here
successcanalsobeabooleanexpressionas inassert diff < tol).
Observethatwecannotcomparediff tozero,whichiswhatwemathematically
expect, because diff is a floating-point variable that most likely contains small
rounding errors. Therefore,wemust comparediff to zerowith a tolerance, here
10 15.
Youare encouraged todoExercise4.2where thegoal is tomakea test function
for a verification based oncomparisonwith hand-calculated results for a few time
steps.
4.2 SpreadingofDiseases
Our aimwith this section is to show in detail howonecan applymathematics and
programmingto investigate spreadingofdiseases. Themathematicalmodel is now
a system of three differential equationswith three unknown functions. To derive
suchamodel,wecanusemainly intuition, sonospecificbackgroundknowledgeof
diseases is required.
4.2.1 SpreadingofaFlu
Imagineaboardingschoolout in thecountryside. Thisschool isasmallandclosed
society. Suddenly, one ormoreof the pupils get a flu. Weexpect that the flumay
spread quite effectively or die out. The question is howmany of the pupils and
the school’s staffwill be affected. Some quite simplemathematics can help us to
achieve insight into thedynamicsofhowthedisease spreads.
Let themathematical functionS.t/ count howmany individuals, at time t, that
have thepossibility to get infected. Here, tmaycounthours or days, for instance.
These individuals make up a category called susceptibles, labeled as S. Another
category, I, consists of the individuals that are infected. Let I.t/ count howmany
there are in category I at time t. An individual having recovered from the disease
is assumed togain immunity. There is also a small possibility that an infectedwill
die. Ineithercase, the individual ismovedfromtheIcategory toacategorywecall
the removedcategory, labeledwithR.We letR.t/count thenumberof individuals
in theR category at time t. Those who enter theR category, cannot leave this
category.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik