Homework 1

For both of these problem I implemented three numerical integrators: Euler, Midpoint and Runga Kutta 4. Any number of the integrators can be run at once and the results displayed on the screen (in different colors). I also solved for a closed form solution that can also be displayed.

Problem A

For the cannon, I implemented gravity and air resistance and treated the firing as an instantaneous explosion that simply imparted an initial velocity on the cannon ball.

Here you can see the interface for the cannon. The mass, amount of powder, and the direction of the cannon are all variable, as is the time step. You can also select whether or not to display a line representing where the cannon ball has been.
The program GUI

Here is an image of the cannon running Runga Kutta 4. The mass of the cannon ball is 500kg and I am using 1kg of powder. The cannon is pointed at an elevation of 45 degrees and the simulation is run using a time step of 0.01.
The ball fired using RK4

In this example I have run all the integrators and changed the conditions so that there is twice as much powder (2 kg) and the elevation of the cannon is at 65 degrees. I also lowered the time step to 0.1. The red line represents the solution to the Euler method, whereas the black, yellow and blue (which are difficult to individually distinguish) are the explicit, midpoint and Runga Kutta 4 solutions, respectively.
Comparison of the Euler, Midpoint and RK4 integration methods with the explicit solution

Since we are only running a simulation forward in time for a limited number of time steps, stability is not of major concern. It is clear, however, that the Euler method is substantially less accurate than either the midpoint or the Runga Kutta method. On modern hardware with only one particle (the cannon ball) any of these methods will be able to run in real time, however, since we would like to be able to add many particles in a future, I stress tested each of the integrators by running it forward for 1,000,000 time steps, the results are below (average of three trials):

Euler2.589s
Midpoint5.877s
Runga Kutta 417.949s
Clearly, Runga Kutta is substantially slower than the other two. Given the fact that the midpoint method was almost as accurate as the Runga Kutta method in the testing and is substantially faster, it seems like the best option for this problem.

Problem B

For the spring-mass system I wrote implements the spring force, a spring damping force and gravity. This is done by keeping track of a system of particles (in this example, we only have two) that can be connected via springs and fixed in one location. The top particle (the small black one) is fixed and connected to the other larger box via a spring. The mass initially hangs at the rest length for the spring, when the simulation begins, gravity pulls the mass downward and begins the oscillations.

Here you can see the interface for the spring-mass system. The mass, spring constant and are all variable, as is the time step.
The program GUI for the mass-spring system

Here is an example of the mass while running Runga Kutta 4.
A screenshot of the mass-spring system using an RK4 integrator

Here is an example of the Euler method diverging from Runga Kutta 4.
A screenshot of the mass-spring system with both RK4 and Euler--Euler is clearly diverging

Here is an example displaying the instability of all the numerical integrators, especially the Euler method. As expected, the closed for solution is an infinite sinusoidal wave. The numerical integrators, due to the fact that there is no damping term, perpetually compound the numerical errors and eventually "blow-up" and diverge. The Euler method diverges much more quickly than the Runga-Kutta and midpoint methods (they are almost identical and therefore difficult to distinguish in the graph).
Displacement as a function of time for the various integration methods, undamped spring

Here is an example of the mass-spring system being weakly damped. All of the answers eventually converge to the solution, however, Euler again lags behind Runga Kutta and midpoint (which are again, difficult to distinguish on the graph).
Displacement as a function of time for the various integration methods, damped spring

As can be seen from both the mass-spring system and the cannon, the Euler method is inaccurate and less stable than the other two integration methods. The interesting thing (for me) is the relative stability and accuracy of midpoint vs. Runga Kutta 4. Runga Kutta 4 required three times more computation than the midpoint method and did not appear to be substantially more accurate or stable for these examples. This would suggest that the midpoint method is an excellent alternative when the system is not terribly stiff, as it can provide solid accuracy and stability at a fraction of the computation of Runga Kutta 4.

The program can be downloaded here.
The source can be downloaded here.