Seite - 153 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 153 -
Text der Seite - 153 -
4.4 Exercises 153
4.4 Exercises
Exercise4.1:Geometricconstructionof theForwardEulermethod
Section 4.1.4 describes a geometric interpretation of the Forward Euler method.
This exercisewill demonstrate thegeometric constructionof the solution indetail.
Consider thedifferential equationu0 Duwithu.0/D 1. Weuse time steps t D
1.
a) Start at t D 0 and drawa straight linewith slopeu0.0/ D u.0/ D 1. Go one
timestep forward to t D t andmark the solutionpointon the line.
b) Drawastraight line throughthesolutionpoint. t;u1/withslopeu0. t/Du1.
Goone timestep forward to t D2 t andmark the solutionpointon the line.
c) Drawastraight line through the solutionpoint .2 t;u2/with slopeu0.2 t/D
u2. Go one time step forward to t D 3 t andmark the solution point on the
line.
d) Setup theForwardEuler schemefor theproblemu0 Du. Calculateu1,u2, and
u3. Check that thenumbersare the sameasobtained ina)-c).
Filename:ForwardEuler_geometric_solution.py.
Exercise4.2:Make test functions for theForwardEulermethod
Thepurposeof this exercise is tomake afiletest_ode_FE.py thatmakes use of
the ode_FE function in the file ode_FE.py and automatically verifies the imple-
mentationofode_FE.
a) The solution computed by hand inExercise 4.1 can be used as a reference so-
lution.Makea functiontest_ode_FE_1() that callsode_FE to compute three
time steps in the problemu0 D u,u.0/ D 1, and compare the threevaluesu1,
u2, andu3with thevaluesobtained inExercise4.1.
b) Thetest ina)canbemademoregeneralusingthefact that iff is linear inuand
does not dependon t, i.e.,wehaveu0 D ru, for someconstant r, theForward
EulermethodhasaclosedformsolutionasoutlinedinSect.4.1.1:un DU0.1C
r t/n. Use this result to construct a test functiontest_ode_FE_2() that runs
anumberofsteps inode_FEandcomparesthecomputedsolutionwiththelisted
formula forun.
Filename:test_ode_FE.py.
Exercise4.3: ImplementandevaluateHeun’smethod
a) A2nd-orderRunge-Kuttamethod,alsoknownhasHeun’smethod, isderivedin
Sect. 4.3.5.Makea functionode_Heun(f, U_0, dt, T) (as a counterpart to
ode_FE(f, U_0, dt, T) in ode_FE.py) for solving a scalar ODE problem
u0 D f.u;t/,u.0/ D U0, t 2 .0;T , with thismethod using a time step size
t.
b) Solve the simpleODEproblemu0 D u,u.0/ D 1, by theode_Heun and the
ode_FE function. Make a plot that comparesHeun’smethod and the Forward
Eulermethodwith the exact solutionu.t/ D et for t 2 Œ0;6 . Use a time step
t D0:5.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik