Page - 218 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 218 -
Text of the Page - 218 -
218 8 SolvingOrdinaryDifferentialEquations
Note thecompactassignment statement
numerical_sol = βboβ if N_t < 70 else βb-β
This is a one-linealternative to, e.g.,
if N_t < 70:
numerical_sol = βboβ
else:
numerical_sol = βb-β
Let us demonstrate a simulation where we start with 100 animals, a net growth
rate of 10% (0.1) per time unit, which can be 1 month, and t β [0,20]months. We
may first tryΞt of half a month (0.5), which impliesNt = 40. Figure 8.6 shows
the results. The solid line is the exact solution, while the circles are the computed
numericalsolution.Thediscrepancyisclearlyvisible.What ifwemakeΞt 10times
smaller?TheresultisdisplayedinFig.8.7,wherewenowuseasolidlinealsoforthe
numericalsolution(otherwise,400circleswouldlookverycluttered,sotheprogram
hasa test onhowto display the numerical solution,eitherascirclesora solid line).
We can hardly distinguish the exact and the numerical solution. The computing
time is also a fraction of a second on a laptop, so it appears that the Forward Euler
method is sufficiently accurate for practical purposes. (This is not always true for
large,complicatedsimulationmodelsinengineering,somoresophisticatedmethods
maybeneeded.)
It is also of interest to see what happens if we increase Ξt to 2 months. The
results inFig. 8.8 indicate that this is an inaccuratecomputation.
Fig. 8.6 Evolutionof a population computed with timestep 0.5 month
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