The Cart-Pole and Inverted Pendulum
Balancing a stick on your hand is hard because the stick falls faster the further it tips. A pole on a cart works the same way. It is the classic test problem for control, and the same physics underlies balancing robots, walking, and rockets.
The dynamics
With cart mass \(m_c\), pole mass \(m_p\) at length \(\ell\), pole angle \(\theta\) from upright, and push \(F\):
Near upright, \(\ddot\theta \approx \frac{(m_c + m_p)\,g}{m_c\,\ell}\,\theta\): the further it tips, the faster it falls. That makes upright an unstable balance point.
LQR
The linear quadratic regulator computes gains \(K\) for the state \(\mathbf{s} = (x, \dot x, \theta, \dot\theta)\) that minimize a cost on errors and effort. The push is then just \(F = -K\mathbf{s}\).
What to notice
Try balancing it yourself in slow motion. To fix a pole leaning right, you must push the cart right, under it, which is the same trick you use with a stick on your hand. LQR does this smoothly and also brings the cart back to the middle. A shorter pole falls faster and is harder to balance, which is why it is easier to balance a broom than a pencil.