# Hamiltonian (and Lagrangian) Mechanics for Computer Scientitss

Hamiltonian mechanics is a conceptually very simple model of classical mechanics, i.e. the laws which govern how things move on a normal, human scale.11 Lagrangian mechanics are very similar but slightly different. The way I understand it, Hamiltonian mechanics are more fundamental, but Lagrangian mechanics may make some things easier to calculate. We’ll see if we get to that. Otherwise you can rest confident that if you understand Hamiltonian mechanics, you can easily change your frame of reference to work with Lagrangian mechanics. The equations behind Hamiltonian mechanics can be summarised as

$\mathscr{H}(q, p) = T(q, p) - U(q, p)$

$\frac{dq}{dt} = \frac{d\mathscr{H}}{dp}$

$\frac{dp}{dt} = -\frac{d\mathscr{H}}{dq}$

which looks scary but is, in fact, very simple. Using them to calculate the motions of a system can be much easier than with other equations of motion.

# The Search for Intuition

But that’s not why I care about it. I like getting different perspectives on things. I like taking equations and going, “So what does this stuff really mean? What would happen if we changed this variable here? How weird would the universe become then?”

I know Newtons’ equations. They are very intuitive, and are written in terms of quantities we can “feel”, on one level or another. However, I like the challenge of trying to intuit and “feel” stuff that is more abstract, more concise, and more powerful.

I wanted to learn Hamiltonian mechanics to get a different perspective on my physical reality, and get a better sense of how things are connected together. Unfortunately, most sources are rather maths-heavy22 which shouldn’t be a problem to a computer scientist, but I’m also lazy, okay? so I wanted a more programming-centric approach. This is it.

# Index

This turns out to be a large subject with lots of text, so it’s split into several parts.