Page - 279 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 279 -
Text of the Page - 279 -
8.6 Exercises 279
Exercise8.15:The LeapfrogMethod
We consider the general ODE problemu′(t)= f(u,t),u(0)=U0. To solve such
anODEnumerically,thesecondorderLeapfrogmethodapproximatesthederivative
(at some time tn=nΔt) byuseofacentereddifferenceover two time steps,
u′(tn)≈ u(tn+1)−u(tn−1)
2Δt = u n+1 −un−1
2Δt .
a) Replace the derivative in the ODE by the given centered difference approxima-
tionandshowthat this allowsus to formulate:
un+1 =un−1+2Δtf(un,tn), n=1,2,.. .,Nt −1,
withu0 =U0.Dowe have the informationwe need toget theschemestarted?
b) Theproblemyoudiscovered in thepreviousquestion,maybefixedbyusing the
ForwardEulermethod.However, theLeapfrogmethodisasecondordermethod,
while the ForwardEulermethod isfirst order.
Argue, with reference to the Taylor series (see, e.g., Exercise 8.14), why the
Forward Euler method can be used without reducing the order of the overall
scheme.
c) ImplementtheLeapfrogschemeinafunctionleapfrog.Makesurethefunction
takesanappropriateset of inputparameters, so that it is easy to importanduse.
d) Write a function compare_FE_leapfrog that compares graphically the solu-
tionsproducedbytheForwardEulerandLeapfrogmethods,whentheysolve the
population growth modelu′ = 0.1u, withu(0) = 100. Let the total time span
T =20,anduse a time stepdt=2. In the plotproduced, includealso theexact
solution, so that thenumerical solutionscanbeassessed.
e) Suggest a reasonable asymptotic error model before you write a proper test
function test_convergence_rates that may be used to compute and check
the convergence rates of the implemented Leapfrog method. However, the test
functionshould takeappropriate inputparameters, so that it canbeusedalso for
otherODE solvers, in particular theode_FE implementedpreviously.
Include your test function in a program, together with the two functions you
defined previously (leapfrog and compare_FE_leapfrog). Write the code
with a test block, so that it getseasy toeither import functionsfromthe module,
or to run it as aprogram.
Finally, run theprogram(so thatcompare_FE_leapfroggetscalled,aswell
as test_convergence_rates for both FE and Leapfrog) and confirm that it
works as expected. In particular, does the plot look good, and do you get the
convergenceratesyouexpectedforForwardEulerandLeapfrog?
Filename:growth_leapfrog.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