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

Page - 221 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 221 -

Image of the Page - 221 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 221 -

8.2 PopulationGrowth:AFirstOrderODE 221 for n in range(N_t): u[n+1] = u[n] + dt*f(u[n], t[n]) return u, t def demo_population_growth(): """Test case: u’=r*u, u(0)=100.""" def f(u, t): return 0.1*u u, t = ode_FE(f=f, U_0=100, dt=0.5, T=20) plt.plot(t, u, t, 100*np.exp(0.1*t)) plt.show() if __name__ == ’__main__’: demo_population_growth() This programfile, calledode_FE.py, is a reusable piece of code with a general ode_FE function that can solve any differential equationu′ = f(u,t) and a demo function for the special case u′ = 0.1u,u(0) = 100. Observe that the call to the demo function is placed in a test block. This implies that the call is not active if ode_FE is importedasamoduleinanotherprogram,butactive ifode_FE.py is run asa program. Thesolutionshouldbeidentical towhat thegrowth1.pyprogramproduceswith the same parameter settings (r = 0.1,N0 = 100). This feature can easily be tested byinsertingaprintcommand,butamuchbetter,automatedverificationissuggested in Exercise8.2. You are stronglyencouragedto take a “break”and do that exercise now. Remarkonthe Use ofuasVariable In theode_FEprogram,thevariableu isused indifferentcontexts. Inside the ode_FE function, u is an array, but in the f(u,t) function, as exemplified in the demo_population_growth function, the argument u is a number. Typically, we callf (in ode_FE) with theu argument as one element of the arrayu in theode_FE function:u[n]. 8.2.6 AMoreRealisticPopulationGrowthModel ExponentialgrowthofapopulationaccordingthemodelN′ = rN,withexponential solutionN =N0ert, is unrealistic in the long run because the resources needed to feed thepopulationarefinite.At somepoint therewill notbeenoughresourcesand the growth will decline. A common model taking this effect into account assumes that r dependson thesize of thepopulation,N: N(t+Δt)−N(t)= r(N(t))N(t).
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python