ODE's & Particle Dynamics
simulator
directory.simulator code
Binaries for Mac OSX are also available:
simulator binary
Cannon
A simple cannon with adjustible elevation and direction. Variables in the system are the mass of the slug, the amount of gravity, the amount of powder used to fire the slug, the amount of air-drag, and the simulation time step.
powder: 1
friction: 50
mass: 100
elevation: 45
gravity: -9.81
step: 0.1
As this case clearly shows, very little error is accumulated when the step size is small. In general, all three integration methods perform reasonably well. While Euler does incur an error penalty due to its single derivative evaluation, it is difficult to cause instability with this simple particle system.
powder: 3
friction: 10
mass: 100
elevation: 85
gravity: -9.81
step: 5
As the step size increases, the the distance between derivative estimates increases, greatly increasing the possibility to accumulate error. As seen in this example, Euler's method is quite inaccurate compared to the higher order integration methods. Note that the Midpoint method's local error is beginning to show.
Spring
A spring-mass system. Features adjustible spring constant, mass, gravity amount, drag factor, and step size. The small red disc marks the rest point of the spring system.
spring constant: 15
drag: 0
mass: 5
step: 0.1
The Euler method diverges very quickly due to the more complex acceleration term. The other methods remain fairly stable.
spring constant: 20
drag: 0
mass: 10
step: 0.1
A slight alteration in the initial values causes the more oscillation due to the greater mass.
spring constant: 15
drag: 0.3
mass: 5
step: 0.5
With a dampening factor, the relative error in the three methods is obvious. Euler diverges to infinity in a mear handful of time steps. Midpoint fairs much better than Euler and does not immediately diverge, but it still slowly diverges rather than converging as desired. RK4 remains very stable and converges nicely, even for this large step size.
Analysis
As to which method is more efficient, it depends on the application. RK4 is capable of evaluating four derivatives in each step, allowing it to solve simulations much faster than the other methods (by taking larger steps). Yet, Midpoint and Euler's require less computation per step, and if an accurate solution is not needed, these methods are quite appropriate (see cannon cases for example).