Seite - 217 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 217 -
Text der Seite - 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))
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik