8.6 Exercises
279
Exercise 8.15: The Leapfrog Method
We consider the general ODE problem u (t) = f (u, t), u(0) = U 0 . To solve such
an ODE numerically, the second order Leapfrog method approximates the derivative
(at some time t n = nΔt) by use of a centered difference over two time steps,
u
(t n ) ≈
u(t n+1 ) − u(t n−1 )
2Δt
=
u n+1 − u n−1
2Δt
.
a) Replace the derivative in the ODE by the given centered difference approximation and show that this allows us to formulate:
u
n+1
= u
n−1
+ 2Δtf (u
n , t n ) ,
n = 1, 2, . . . , N t − 1,
with u 0 = U 0 . Do we have the information we need to get the scheme started?
b) The problem you discovered in the previous question, may be fixed by using the
Forward Euler method. However, the Leapfrog method is a second order method,
while the Forward Euler method is first 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) Implement the Leapfrog scheme in a function leapfrog. Make sure the function
takes an appropriate set of input parameters, so that it is easy to import and use.
d) Write a function compare_FE_leapfrog that compares graphically the solutions produced by the Forward Euler and Leapfrog methods, when they solve the
population growth model u = 0.1u, with u(0) = 100. Let the total time span
T = 20, and use a time step dt = 2. In the plot produced, include also the exact
solution, so that the numerical solutions can be assessed.
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
function should take appropriate input parameters, so that it can be used also for
other ODE solvers, in particular the ode_FE implemented previously.
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 gets easy to either import functions from the module,
or to run it as a program.
Finally, run the program (so that compare_FE_leapfrog gets called, as well
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
convergence rates you expected for Forward Euler and Leapfrog?
Filename: growth_leapfrog.py.
Précédent

- 299/350

Suivant