Page - 156 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 156 -
Text of the Page - 156 -
156 4 SolvingOrdinaryDifferentialEquations
Exercise4.11:UseaBackwardEuler schemeforpopulationgrowth
We consider the ODE problemN 0.t/ D rN.t/, N.0/ D N0. At some time,
tn D n t, we can approximate the derivativeN 0.tn/ by a backward difference,
seeFig. 4.19:
N 0.tn/ N.tn/ N.tn t/
t D N n Nn 1
t ;
which leads to
Nn Nn 1
t D rNn;
called theBackwardEuler scheme.
a) Findanexpression for theNn in termsofNn 1 andformulateanalgorithmfor
computingNn,nD1;2;:: :;Nt.
b) Implementthealgorithmina) inafunctiongrowth_BE(N_0, dt, T) forsolv-
ingN 0 D rN ,N.0/DN0, t 2 .0;T ,with timestep t (dt).
c) Implement theForwardEuler scheme ina functiongrowth_FE(N_0, dt, T)
asdescribed inb).
d) Compare visually the solution produced by the Forward and Backward Euler
schemeswith the exact solutionwhen r D 1 andT D 6. Make twoplots, one
with t D0:5andonewith t D0:05.
Filename:growth_BE.py.
Exercise4.12:UseaCrank-Nicolsonschemeforpopulationgrowth
It is recommendedtodoExercise4.11prior to thepresentone.Herewe lookat the
same population growthmodelN 0.t/ D rN.t/,N.0/ D N0. The time derivative
N 0.t/ can be approximated by various types of finite differences. Exercise 4.11
considersabackwarddifference(Fig.4.19),whileSect.4.1.2explainedtheforward
difference (Fig. 4.2). A centered difference is more accurate than a backward or
forwarddifference:
N 0.tnC 1
2 t/ N.tnC t/ N.tn/
t D N nC1 Nn
t :
This typeofdifference,appliedat thepoint tnC12 D tnC 1
2 t, is illustratedgeomet-
rically inFig. 4.20.
a) Insert the finite difference approximation in theODEN 0 D rN and solve for
the unknownNnC1, assumingNn is already computed andhence known. The
resultingcomputationalschemeisoftenreferredtoasaCrank-Nicolsonscheme.
b) Implementthealgorithmina) inafunctiongrowth_CN(N_0, dt, T) forsolv-
ingN 0 D rN ,N.0/DN0, t 2 .0;T ,with timestep t (dt).
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