Who doesn't love numerical calculations? When I teach this stuff in class, students usually use the following recipe:
- Find the forces on the object.
- Find the new momentum (based on the force and the small time interval)
- Find the new position (based on the velocity and time interval).
Simple. And it even works most of the time. In cases where this does not give a nice value, you can always make your time step smaller to get it to work. This is essentially the Euler method. We can use it because computers are fast enough that we can be sloppy in our algorithm.
Believe it or not, people do think about the most efficient way to do these types of things. One of my colleagues pointed out the Leapfrog method and claims that it is really nice.
In the leapfrog method, the recipe changes a little bit.
- Find the forces.
- Find the new momentum based on the force and HALF of the small time step interval (not the whole time step)
- Find the new position.
- Find the next new momentum with the other half of the time step.
This is not the real leapfrog method. However, it does use the velocity calculated on the 'half step' to calculate the position. It then calculates the final velocity. I think in the real leap frog method, the position and velocity data are out of phase by half a time step. Still, let me see how well this method works.
Simple Harmonic Oscillator - Analytic Solution
I like the SHO to model. Why? First, it is solvable analytically without too much trouble. Second, it pops up all over the place. Third, if you are not careful your numerical model can do weird stuff.
Suppose I have a mass (m) on a horizontal spring (with no friction). When the mass is at x = 0, the force from the spring is also zero.
So, I pull the mass to the side a little and let go. I get the following solution (which I am not going derive right now)
Now that I have an analytical solution, I can compare different numerical methods to this.
Euler Method
Let me go ahead and calculate the motion of this mass on a spring with the normal plain method. Here is a plot of three things. First, the analytic solution, second the Euler method (as described above) and third the Euler method calculating position, then velocity, then acceleration.
I guess I should state the parameters for these calculations. Had a time step of 0.2 seconds. The mass, the spring constant and the starting position all had a value of 1 (in their proper units of course). The graph only looks like it has two plots because the first Euler method fits so well compared to the backwards ordered one.
Notice that the backwards ordered Euler gets worse over time. So, to somehow show the variation let me plot the difference between the two methods and the analytic solution.
If you make the time interval larger, the backwards Euler gets real bad real quick. At 0.5 seconds for a time interval, the other Euler method starts to look messed up also.
Leapfrog
Let me now compare the leapfrog method to the better Euler method. This is a plot of the difference between the two methods and the analytical method.
The red data is the leapfrog, the blue is the acceleration-velocity-position order (the leapfrog could be written as a-.5v-x.5v). What if I change the order around? In this case, I calculate the velocity after half the interval, then I calculate position, then accleration and then the rest of the velocity. This looks much better.
Question: is this leapfrog method better than reducing the time step by 2? (here I turned off the analytic solution so you could see better)
So, yes. Adding that extra half-step is better than just making the time smaller. Here is the error for the leapfrog with a time step of 0.2 and the Euler with a time step of 0.04 seconds. So, I guess the leapfrog is better.