The equations of motion are derived for a multi-pendulum with n links, the system is simulated, animated and a controller is developed to balance the pendulum. The problem is detailed in-depth in the IPython notebook file. It is hosted as a blog post at http://www.moorepants.info/blog/npendulum.html.
$ ipython notebook