In my last blog post, I demonstrated how to drive the Leapfrog integrator using Taylor series of , , and . I mentioned that since we truncated the series for and after the second-order terms, the Leapfrog integrator was a second-order method where the error is divided by eight every time we half the timestep.
In this blog post, I’ll analyze my statements about the error in more depth. First, I’ll give a definition for the truncation error for a single step. Assume that the differential equation has an analytical solution and the estimated solution generated by the Leapfrog integrator is given by . Then, the truncation error is simply the difference between the real and approximate solutions:
We can verify the local trunction error for a single step by comparing the difference between the analytical solution at time with a single step of the Leapfrog integrator. First, assume that we can express the analytical solution as a Taylor series:
We substitute the Taylor series and the Leapfrog equation for into . We note that and . Errors tend accumulate from step to step. Thus , , and all need to come from the analytical solution so that we are only computing a single step with the Leapfrog method.
Using a harmonic oscillator model solved with the analytical and Leapfrog methods, we can demonstrate how to verify the per-step error. We evaluate the analytical solution at discretized time points over 100 seconds using a range of time steps. For each time point, we also compute the approximate solution from the previous analytical evaluation using the Leapfrog method. For each step size, we take an average over the local truncation errors from every time step. I did so, creating a table like so:
|Step size||Steps||Average Position Error||Average Velocity Error|
We can then estimate the order of the error with respect to the time step. Assume the average error is dominated by the time step to a power. We set up an equation to model that relationship and take the logarithm of both sides:
where is the average error, is the time step, is the order, and is a constant.
Using the log-log formulation, we can estimate and using linear regression on the table of time steps and average errors.
|Error Type||Estimated Order|
We can see that the estimated orders of the local position and velocity truncation errors are close to 3, matching our expectation from the analytical analysis. Our implementation is thus likely correct.
(All code for this blog post is available on GitHub under the Apache License v2.)