Seite - 103 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 103 -
Text der Seite - 103 -
4.1 PopulationGrowth 103
N[0] = N_0
for n in range(N_t+1):
N[n+1] = N[n] + r*dt*N[n]
import matplotlib.pyplot as plt
numerical_sol = ’bo’ if N_t < 70 else ’b-’
plt.plot(t, N, numerical_sol, t, N_0*exp(r*t), ’r-’)
plt.legend([’numerical’, ’exact’], loc=’upper left’)
plt.xlabel(’t’); plt.ylabel(’N(t)’)
filestem = ’growth1_%dsteps’ % N_t
plt.savefig(’%s.png’ % filestem); plt.savefig(’%s.pdf’ % filestem)
Thecompletecodeaboveresides in thefilegrowth1.py.
Let us demonstrate a simulationwherewe startwith 100animals, a net growth
rate of 10 percent (0.1) per time unit, which can be onemonth, and t 2 Œ0;20
months. Wemayfirst try t of half amonth (0.5),which impliesNt D 40 (or to
be absolutely precise, the last time point to be computed according to our set-up
above is tNtC1 D 20:5). Figure 4.4 shows the results. The solid line is the exact
solution,while thecirclesare thecomputednumerical solution. Thediscrepancy is
clearly visible. What ifwemake t 10 times smaller? The result is displayed in
Fig. 4.5,wherewenowuse a solid line also for thenumerical solution (otherwise,
400 circleswould lookverycluttered, so the programhas a test onhow todisplay
the numerical solution, either as circles or a solid line). Wecanhardlydistinguish
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 Eulermethod is sufficiently
accurate for practical purposes. (This is not always true for large, complicated
simulationmodels inengineering, somore sophisticatedmethodsmaybeneeded.)
Fig.4.4 Evolutionof apopulationcomputedwith timestep0.5month
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