Page - 117 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 117 -
Text of the Page - 117 -
4.2 SpreadingofDiseases 117
andone for the right-handside functions,
f.u;t/D . ΛSI;ΛSI I; I/:
Theequationu0 Df.u;t/meanssetting thetwovectorsequal, i.e., thecomponents
mustbepairwiseequal. Sinceu0 D .S0;I 0;R0/,weget thatu0 Df implies
S0 D ΛSI;
I 0 DΛSI I;
R0 D I :
The generalized short notation u0 D f.u;t/ is very handy since we can derive
numericalmethods and implement software for this abstract system and in a par-
ticular application just identify the formulas in thef vector, implement these, and
call functionality that solves thedifferential equationsystem.
4.2.6 ProgrammingtheNumericalMethod;theGeneralCase
InPythoncode, theForwardEuler step
unC1 DunC tf.un;tn/;
beingascalaror avectorequation, canbecodedas
u[n+1] = u[n] + dt*f(u[n], t[n])
both in the scalar and vector case. In the vector case, u[n] is a one-dimensional
numpyarrayof lengthmC1holding themathematicalquantityun, and thePython
functionfmust returnanumpyarrayof lengthmC1. Then theexpressionu[n] +
dt*f(u[n], t[n]) is anarrayplusa scalar timesanarray.
For all this to work, the complete numerical solutionmust be represented by
a two-dimensional array, createdbyu = zeros((N_t+1, m+1)). Thefirst index
counts the timepoints and the second thecomponentsof the solutionvector at one
timepoint. That is,u[n,i]correspondsto themathematicalquantityuni .Whenwe
useonlyoneindex,asinu[n], this is thesameasu[n,:]andpicksoutall thecom-
ponents in thesolutionat the timepointwith indexn. Then theassignmentu[n+1]
= ... becomes correct because it is actually an in-place assignment u[n+1, :]
= .... Thenice featureof these facts is that the samepieceofPythoncodeworks
forbotha scalarODEandasystemofODEs!
Theode_FE function for thevectorODEisplaced in thefileode_system_FE.
pyandwaswrittenas follows:
from numpy import linspace, zeros, asarray
import matplotlib.pyplot as plt
def ode_FE(f, U_0, dt, T):
N_t = int(round(float(T)/dt))
back to the
book Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik