In this post, we build off the theory developed in Part 1 and actually implement a double-pendulum simulator in a few different programming languages. We'll start by writing our own Runge-Kutta solvers which will give us the position, velocity, and acceleration data that is needed to completely describe the trajectory of a double pendulum system given an arbitrary set of initial conditions. Once we have this data, we can move onto plotting it using a few different plotting libraries. The languages I'll be going over are Matlab, C++, and Python. I chose these languages because they are all quite popular and any given programmer is likely to understand at least one of them.

### Getting the Code

All of the code is hosted here on Github, and more details can be found at the Double-Pendulum-Simulation project page on my website. The entire project is available for download via either of these two links : .Tar , .Zip. You can also clone it straight from the command line :

```
git clone https://github.com/jhallard/Double-Pendulum-Simulation.git
cd Double-Pendulum-Simulation
```

### Quick Review

Below is a quick summary of the last post, for more information, read Part 1.

- Expand Equations
Below are the 4 differential-equations describing the motion of the masses on ends of the upper and lower pendulums.

1 |
$\theta_1' = \omega_1$ |

2 |
$\theta_2' = \omega_2$ |

3 |
$\omega_1' = \frac{-g(2m_1 + m_2)\sin(\theta_1) - m_2g\sin(\theta_1 - 2\theta_2) - 2\sin(\theta_1 - \theta_2)m_2(L_2\omega_2^2+L_1\omega_1^2\cos(\theta_1 - \theta_2))}{L_1(2m_1+ m_2(1 - \cos(2(\theta_1-\theta_2))))} $ |

4 |
$ \omega_2' = \frac{2\sin(\theta_1-\theta_2)(L_1\omega_1^2(m_1+m_2)+\cos(\theta_1)g(m_1+m_2) + L_2m_2\omega_2^2\cos(\theta_1-\theta_2))}{L_2(2m_1+m_2(1-cos(2(\theta_1-\theta_2))))}$ |

Here is the definiton of the Runge-Kutta-4 method that we will be using/implementing.

$$ \textbf{x}_{n+1} = \textbf{x}_n + \frac{h}{6}(\textbf{K}_{1n} + 2\textbf{K}_{2n} + 2\textbf{K}_{3n} + \textbf{K}_{4n})$$
$$ t_{n+1} = t_n + h $$

$\textbf{K}_{1n} = \textbf{f}(t, \textbf{x}_n)$ |
$\textbf{K}_{2n} = \textbf{f}(t+\frac{h}{2}, \textbf{x}_n + \frac{h}{2}\textbf{K}_{1n})$ |

$\textbf{K}_{3n} = \textbf{f}(t+\frac{h}{2}, \textbf{x}_n + \frac{h}{2}\textbf{K}_{2n})$ |
$\textbf{K}_{4n} = \textbf{f}(t+h, \textbf{x}_n + h \textbf{K}_{3n})$ |