Seite - 273 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 273 -
Text der Seite - 273 -
8.6 Exercises 273
corresponding lists, so that convergence rates (r) can be computed after the loop.
Observe, in the loop, how ode_FE returns (the solution u and) the time mesh t,
which then is used as input to the u_exact function, causing the exact function
values to alsobecomputedat the verysamemeshpointsasu.
The Forward Euler method is a first order method, so we should get r near 1 as
thetimestepbecomessmallenough.Acall to this test functiondoesindeedconfirm
(remove# in frontofprint(r)) thatrcomesveryclose to 1asdtgets smaller.
8.6 Exercises
Exercise8.1:RestructureaGiven Code
Section 8.1.1 gives a code for computing the development of water volume V in
a tank. Restructure the code by introducing an appropriate function compute_V
that computes and returns the volumes, and a functionapplication that calls the
formerfunctionandplots the result.
Note that your restructuring should not cause any change in program behavior,
asexperiencedbyauser of the program.
Filename:restruct_tank_case1.py.
Exercise8.2:GeometricConstructionof the ForwardEuler Method
Section 8.2.4 describes a geometric interpretation of the Forward Euler method.
This exercise will demonstrate the geometric construction of the solution in detail.
Consider thedifferentialequationu′ =uwithu(0)=1.Weuse timestepsΔt=1.
a) Start at t=0anddrawa straight linewith slopeu′(0)=u(0)=1.Goone time
step forward to t=Δt andmark thesolutionpointon the line.
b) Drawastraight line throughthesolutionpoint (Δt,u1)withslopeu′(Δt)=u1.
Goone timestep forward to t=2Δt andmark thesolutionpointon the line.
c) Draw a straight line through the solution point (2Δt,u2)with slopeu′(2Δt)=
u2.Goonetimestepforwardto t=3Δt andmarkthesolutionpointonthe line.
d) Set up the Forward Euler scheme for the problemu′ =u. Calculateu1,u2, and
u3.Check that the numbersare the sameasobtained ina)-c).
Filename:ForwardEuler_geometric_solution.py.
Exercise8.3:Make Test Functions for theForwardEuler Method
The purpose of this exercise is to make a file test_ode_FE.py that makes use
of the ode_FE function in the file ode_FE.py and automatically verifies the
implementationofode_FE.
a) The solution computed by hand in Exercise 8.2 can be used as a reference
solution.Makeafunctiontest_ode_FE_1()thatcallsode_FE tocomputethree
timesteps in theproblemu′ =u,u(0)=1,andcomparethe threevaluesu1,u2,
andu3 with the valuesobtained in Exercise8.2.
b) The test in a) can be made more general using the fact that if f is linear in
u and does not depend on t, i.e., we have u′ = ru, for some constant r, the
ForwardEulermethodhasaclosedformsolutionasoutlinedinSect.8.2.1:un=
U0(1+rΔt)n.Use this result toconstructa test functiontest_ode_FE_2()that
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