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 - 217 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 217 -

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

Text of the Page - 217 -

8.2 PopulationGrowth:AFirstOrderODE 217 Fig. 8.5 The numerical solution at points can be extended by linear segments between the mesh points 8.2.3 ProgrammingtheFEScheme;theSpecialCase Let uscompute (8.8) in a program.The inputvariablesareN0,Δt, r, andNt. Note that we need to computeNt new valuesN1,.. .,NNt . A total ofNt +1 valuesare neededin anarrayrepresentationofNn,n=0,.. .,Nt. Ourfirstversionofthisprogram(growth1.py)isassimpleaspossible,andvery similar to the codeswe wrotepreviouslyfor the water tankexample: import numpy as np import matplotlib.pyplot as plt N_0 = int(input(’Give initial population size N_0: ’)) r = float(input(’Give net growth rate r: ’)) dt = float(input(’Give time step size: ’)) N_t = int(input(’Give number of steps: ’)) t = np.linspace(0, N_t*dt, N_t+1) N = np.zeros(N_t+1) N[0] = N_0 for n in range(N_t): N[n+1] = N[n] + r*dt*N[n] numerical_sol = ’bo’ if N_t < 70 else ’b-’ plt.plot(t, N, numerical_sol, t, N_0*np.exp(r*t), ’r-’) plt.legend([’numerical’, ’exact’], loc=’upper left’) plt.xlabel(’t’); plt.ylabel(’N(t)’) filestem = ’growth1_{:d}steps’.format(N_t) plt.savefig(’{:s}.png’.format(filestem)) plt.savefig(’{:s}.pdf’.format(filestem))
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