Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Page - 103 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 103 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Image of the Page - 103 -

Image of the Page - 103 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 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
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python