Using the Correct Lagrangian for the Inverted Pendulum

10 minute read


The Lagrangian and the Inverted Pendulum

The inverted pendulum is a canonical system studied extensively in control theory because it has a simple goal – keep a pendulum upright by moving its base left to right – but the equations of motion are nonlinear, making it a good test application for novel controllers. These nonlinear equations are found through two methods: application of Newton’s second law, and Lagrange’s method. This post will focus on the latter, and how to use Lagrange’s method correctly to get the equations of motion for the inverted pendulum.

A quick google search for the equations of the motion for the inverted pendulum gives many different results that look similar, but have different notions and assumptions on the system. Quickly applying any one of these might be good enough for your application, but how do you know for sure your Lagrangian is correct? The only way to find out is to derive it from scratch, which we will do right now.

What is a Lagrangian?

The Lagrangian is a function that mixes together the kinetic and potential energy functions of a system in a special way. It is studied extensively in Lagrangian mechanics, which is taught in many undergraduate and graduate physics courses. In most applications, the Lagrangian $L$ is posed as the kinetic energy $T$ of the system minus the potential energy $V$:

\begin{equation} L = T - V \end{equation}

As is known by most scientists and engineers, the kinetic energy is usually a function of velocity $\dot{x}$, and the potential energy is usually a function of position $x$. So the variables of the Lagrangian can be explicitly stated as:

\begin{equation} L(x,\dot{x}) = T - V \end{equation}

In order to analyze any system, both $T$ and $V$ need to be defined. Let’s look at the inverted pendulum system to do so.

The Inverted Pendulum System

For simplicity, we denote things like angle orientation and masses according to the wikipedia entry on the inverted pendulum. Here is a picture of the system:

A force $F$ pushes a cart of mass $M$ left to right, while an inverted pendulum of mass $m$ is left to swing freely about a point fixed on the cart. While this model makes assumptions that are reasonable for many systems (e.g. no frictional forces), there is one assumption that might not be so reasonable: the pendulum is treated as a point mass.

The inspiration for this blog post really came when I asked the question: “what if we didn’t make this assumption?” One simple answer is that we can always make this assumption, since we can replace any physical pendulum with a point mass located at the pendulum’s center of mass. The problem is that this answer is wrong, and to see why, consider the following. Suppose we had a rod whose mass was almost all concentrated at its end. In this case, simplifying all mass to a point at the end makes sense:

Moment of Inertia 1

The moment of inertia for this system is simple: $I_1 = mL^2$. Thus, if rotating at angular speed $\omega$, this system would then have rotational kinetic energy $T_1 = mL^2\omega/2$.

Now, cut the mass in half and place one half at the pivot, and one half a distance $2L$ from the pivot:

Moment of Inertia 2

The moment of inertia for this system is now $I_2 = (m/2)(2L)^2 = 2mL^2$. Even though the center of mass is the same, the moment of inertia has doubled! The immediate consequence is that the rotational energies between the two system would be different if they have the same rotational speed $\omega$. Thus, we can’t simply replace the physical pendulum with a point mass in any situation when rotations are concerned.

This is why it can be confusing when using Lagrangian mechanics on system that rotate. How should we proceed?

Calculating Kinetic and Potential Energy

One fool-proof method of calculating kinetic energy of any system is to take the kinetic energy of a point mass, $T_{pm}=\frac{mv^2}{2}$, and recognize that any physical system can be re-imagined as a collection of small particles of mass $dm$. Adding up the kinetic energies of all these particles (or in the limit of really small particles, taking an integral) gives you the total kinetic energy:

\begin{equation} \label{eq:integral_KE} T = \frac{1}{2}\int dmv^2 \end{equation}

This neat trick is usually taught at the start of physics undergraduate or calculus 3, and is a go-to if something is confusing you because Eq. \ref{eq:integral_KE} is most always true. In fact, its usually taught when finding moments of inertia for uniform objects.

As quick example, consider the physical rod rotating about a fixed point:

Moment of Inertia 3

You learn that the rotational kinetic energy of this system is $\frac{1}{2}I\omega^2$ in introductory physics. One derivation of this formula is start with $\frac{1}{2}\int dmv^2$ and recognize that for points at length $l$ away from the pivot point, the linear velocity is $v=l\omega$. Thus, we can replace this inside the integral:

\begin{equation} T = \frac{1}{2}\int dm(l\omega)^2 = \frac{1}{2}\omega^2\int_0^L l^2dm \end{equation}

The integral $\int_0^L l^2dm$ is precisely the definition of the moment of inertia $I$, so that we easily recover the formula $\frac{1}{2}I\omega^2$.

Now let us write down the kinetic energy in terms of the generalized position coordinates for this system. The generalized coordinates for the inverted pendulum are the position of the base of the cart, $x$, and the angle of the pendulum, $\theta$. The kinetic energy for the cart is easy: $T_{cart}=\frac{M}{2}\dot{x}^2$. In order to find the total kinetic energy of the pendulum, we can take the integral of a bunch of small point mass that make up the pendulum, like we did above. First write the x-y position of a point on the pendulum as a function of the length along the pendulum $l$:

\begin{equation*} x_{pend} = x - l\sin(\theta) \end{equation*} \begin{equation*} y_{pend} = l\cos(\theta) \end{equation*}

Next, take the derivative with respective to time:

\begin{equation*} \dot{x}_{pend} = \dot{x} - l\dot{\theta}\cos(\theta) \end{equation*} \begin{equation*} \dot{y}_{pend} = -l\dot{\theta}\sin(\theta) \end{equation*}

The kinetic energy of a point with mass $dm$ along the length of of the pendulum is thus given by: \begin{equation} KE_{pend}(l) = \frac{dm}{2}\left(\dot{x}^2 + l^2\dot{\theta}^2 - 2l\dot{\theta}\dot{x}\cos(\theta)\right) \end{equation}

In order to find the total kinetic energy, let’s sum these up, i.e. let’s take the integral over $dm$:

\begin{equation*} KE_{pend} = \int_0^LKE_{pend}(l) \end{equation*}

Let’s take the terms in the sum one at a time. The first term is simple:

\begin{equation*} \int_0^L\frac{dm}{2}\dot{x}^2 = \frac{\dot{x}}{2}\int_0^Ldm \end{equation*}

Here, defining $dm$ is simple. Along the length of the pendulum, each $dm$ corresponds to a small amount of mass that we add up with the integral. We can define $dm$ based on two assumptions: (1) the mass of the pendulum is uniformly distributed, and (2) the mass of th pendulum must be $m$. Due to (1), the value of $dm$ must be directly proportional to a small length of pendulum $dl$ and independent of $l$ at any point, i.e. $dm=kdl$. By choosing $k=\frac{m}{L}$, we can satisfy point (2): $\frac{m}{L}\int_0^Ldl=\frac{m}{L}L=m$. So this term is:

\begin{equation*} \frac{m\dot{x}^2}{2} \end{equation*}

This is the energy of the system as it translates from left to right.

The second term is also easy, as the integral $\frac{\dot{\theta}^2}{2}\int_0^L dm l^2$ is exactly the same as the example above. It simplifies to $\frac{1}{2}I\dot{\theta}^2$. Since we are assuming the mass of the pendulum is uniformly distributed, we can immediately make the substitution $I=\frac{1}{3}mL^2$. This term represents the rotational energy of the system.

Finally, the last term is a coupling term, since it contains both $\dot{x}$ and $\dot{\theta}$ – this is the term that is responsible for coupling the translational degree of freedom $x$ with the rotational degree of freedom $\theta$. The resulting integral is:

\begin{equation*} \dot{x}\dot{\theta}\cos(\theta)\frac{m}{L}\int_0^L l dl = \dot{x}\dot{\theta}\cos(\theta)\frac{mL}{2} \end{equation*}

If we assumed the pendulum was a point mass, we would arrived at a similar coupling term of $\dot{x}\dot{\theta}\cos(\theta)mL$. The factor of 2 comes from the fact that the center of mass for our physical pendulum is located at $L/2$ away from the pivot. Otherwise, the term would be the same!

The potential energy is found is a similar way. Since gravity is the only conservative force present, we can write $V=\int_0^Ldm g h=\frac{mg}{L}\cos(\theta)\int_0^Lldl=\frac{mgL}{2}\cos(\theta)$, where we used $h=l\cos(\theta)$.

The Correct Lagrangian, EOMs and Simulation

With the kinetic energy $T$ and potential energy $V$ defined above, we are then free to write down the Lagrangian $L=T-V$:

\begin{equation} L = \frac{1}{2}(M+m)\dot{x}^2 + \frac{1}{6}mL^2\dot{\theta}^2 - \frac{mL}{2}\dot{x}\dot{\theta}\cos(\theta) - \frac{mgL}{2}\cos(\theta) \end{equation}

Applying the Euler-Lagrange relationship in the variables $x$,$\dot{x}$,$\theta$ and $\dot{\theta}$ gives the resulting EOMs for the inverted pendulum. I won’t go through the steps here, but many others have done so in other parts of the web, so I hope it wouldn’t be too difficult to tackle.

Here are the final equations of motion:

\begin{equation} \ddot{x} = \frac{1}{M+m}\left( F + \frac{mL}{2}\ddot{\theta}\cos(\theta) - \frac{1}{2}\dot{\theta}^2mL\sin(\theta) \right) \end{equation}

\begin{equation} \label{eq:ddot_theta} \ddot{\theta} = \frac{3}{2L}\left( g\sin(\theta) + \ddot{x}\cos(\theta) \right) \end{equation}

Here, we defined $F$ as the (controllable) force on the base of the pendulum.

Notice how $\ddot{\theta}$ shows up when defining $\ddot{x}$, and vice versa. While this is fine from a physical standpoint, when simulating physics (i.e. integrating ODEs) we often want the highest derivative of the system to be defined only in terms of lower derivatives. For this reason, we can rearrange the equations like so:

\begin{equation} \label{eq:ddot_x_simp} \ddot{x} = \frac{F + \frac{3mg}{2}\sin(\theta)\cos(\theta)-\frac{mL}{2}\dot{\theta}^2\sin(\theta)}{M + (1 - \frac{3}{4}\cos^2(\theta)m)} \end{equation}

In this form, we only need $\dot{\theta}$ and $F$ in order to calculate $\ddot{x}$. We may do that same thing with $\ddot{\theta}$, but a quick shortcut is to use the value of $\ddot{x}$ from \ref{eq:ddot_x_simp} and plug it into \ref{eq:ddot_theta}.

Below is a simulation that I coded in matlab that shows these equations in action. Initially, I give the cart and pendulum a small push to the left. You can see both interact with each other: