Page - 276 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 276 -
Text of the Page - 276 -
276 8 SolvingOrdinaryDifferentialEquations
values,modifytheprogramosc_FE.py todumptheuarraytofile, runosc_FE.py,
and let the test functionread the referenceresults fromthatfile.
Filename:osc_ode_FE.py.
Exercise8.11:Compute the Energy in Oscillations
a) Make a function osc_energy(u, v, omega) for returning the potential and
kineticenergyofanoscillatingsystemdescribedby(8.43)–(8.44).Thepotential
energy is taken as 12ω 2u2 while the kinetic energy is 12v 2. (Note that these
expressionsare notexactly thephysicalpotential andkineticenergy,since these
wouldbe 12mv 2 and 12ku 2 fora modelmx′′+kx=0.)
Place the osc_energy in a separate file osc_energy.py such that the
functioncanbecalled fromother functions.
b) Addacall toosc_energy in theprogramsosc_FE.pyandosc_EC.pyandplot
the sum of the kinetic and potential energy. How does the total energy develop
for theForwardEulerand theEuler-Cromerschemes?
Filenames:osc_energy.py,osc_FE_energy.py,osc_EC_energy.py.
Exercise8.12:Use aBackwardEuler SchemeforPopulationGrowth
We consider the ODE problem N′(t) = rN(t), N(0) = N0. At some time,
tn = nΔt, we can approximate the derivative N′(tn) by a backward difference,
seeFig. 8.22:
N′(tn)≈ N(tn)−N(tn−Δt)
Δt = N n−Nn−1
Δt ,
which leads to
Nn−Nn−1
Δt = rNn,
called theBackwardEuler scheme.
a) Find an expression for theNn in terms ofNn−1 and formulate an algorithm for
computingNn,n=1,2,.. .,Nt.
b) Implement the algorithm in a) in a function growth_BE(N_0, dt, T) for
solvingN′ = rN,N(0)=N0, t∈ (0,T],with time stepΔt (dt).
c) Implement the Forward Euler scheme in a functiongrowth_FE(N_0, dt, T)
asdescribed inb).
d) Compare visually the solution produced by the Forward and Backward Euler
schemes with the exact solution when r = 1 andT = 6. Make two plots, one
withΔt=0.5andonewithΔt=0.05.
Filename:growth_BE.py.
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