# Lagrangian mechanics¶

Marcos Duarte
Laboratory of Biomechanics and Motor Control (http://demotu.org/)
Federal University of ABC, Brazil

## The development of the laws of motion of bodies¶

"The theoretical development of the laws of motion of bodies is a problem of such interest and importance that it has engaged the attention of all the most eminent mathematicians since the invention of dynamics as a mathematical science by Galileo, and especially since the wonderful extension which was given to that science by Newton.
Among the successors of those illustrious men, Lagrange has perhaps done more than any other analyst to give extent and harmony to such deductive researches, by showing that the most varied consequences respecting the motions of systems of bodies may be derived from one radical formula; the beauty of the methods so suiting the dignity of the results as to make of his great work a kind of scientific poem."

— Hamilton, 1834 (apud Taylor, 2005).

The Lagrangian mechanics is a formulation of classical mechanics where the equations of motion are obtained from the kinetic and potential energy of the system (scalar quantities) represented in generalized coordinates instead of using Newton's laws of motion to deduce the equations of motion from the forces on the system (vector quantities) represented in Cartesian coordinates. Lagrangian mechanics was introduced by Joseph-Louis Lagrange in the late 18th century.

Let's deduce the Lagrange equations, but first let's review the basics of the Newtonian approach.

## Description of motion¶

One can describe the motion of a particle by specifying its position with respect to a frame of reference in the three-dimensional space as a function of time:

$$x(t),\, y(t),\, z(t) \quad \equiv \quad x_i(t) \quad i=1,\dotsc,3 \label{eq1}$$

A system of $N$ particles will require $3N$ equations to describe their motion.

The basic problem in classical mechanics is to find ways to determine functions such as these, also known as equations of motion, capable of specifying the position of objects over time, for any mechanical situation. Assuming as known the meaning of $x_i(t)$, one can define the components of velocity, $v_i$, and acceleration, $a_i$, at time $t$, as:

$$\begin{array}{rcl} v_i(t) = \dfrac{\mathrm d x_i(t)}{\mathrm d t} = \dot{x}_i(t) \\ a_i(t) = \dfrac{\mathrm d^2 x_i(t)}{\mathrm d t^2} = \dot{v}_i(t) \label{eq3} \end{array}$$

Where we used the Newton's notation for differentiation (also called the dot notation), a dot over the dependent variable. Of note, Joseph Louis Lagrange introduced the prime mark to denote a derivative: $x'(t)$. Read more about the different notations for differentiation at Wikipedia.

### Laws of motion¶

The Newton's laws of motion laid the foundation for classical mechanics. They describe the relationship between the motion of a body and the possible forces acting upon it.

In Newtonian mechanics, the body's linear momentum is defined as:

$$\mathbf{p} = m\mathbf{v} \label{eq5}$$

If the mass of the body is constant, remember that Newton's second law can be expressed by:

$$\mathbf{F} = \frac{\mathrm d \mathbf{p}}{\mathrm d t}=\frac{\mathrm d \big(m\mathbf{v}\big)}{\mathrm d t} = m\mathbf{a} \label{eq6}$$

Using Newton's second law, to determine the position of the body we will have to solve the following second order ordinary differential equation:

$$\frac{\mathrm d^2 x_i(t)}{\mathrm d t^2} = \frac{\mathbf{F}}{m} \label{eqa7}$$

Which has the general solution:

$$\mathbf{x}(t) = \int\!\bigg(\int\frac{\mathbf{F}}{m} \mathrm{d}t\bigg)\mathrm{d}t \label{eq8}$$

### Mechanical energy¶

A related physical quantity is the mechanical energy, which is the sum of kinetic and potential energies.
The kinetic energy, $T$ of a particle is given by:

$$T = \frac{1}{2}mv^2 \label{eq9}$$

The kinetic energy of a particle can be expressed in terms of its linear momentum:

$$T = \frac{p^2}{2m} \label{eq10}$$

And for a given coordinate of the particle's motion, its linear momentum can be obtained from its kinetic energy by:

$$p_i = \frac{\partial T}{\partial v_i} \label{eq11}$$

The potential energy, $V$ is the stored energy of a particle and its formulation is dependent on the force acting on the particle. For example, for a conservative force dependent solely on the particle position, such as due to the gravitational field near the Earth surface or due to a linear spring, force and potential energy are related by:

$$\mathbf{F} = - \frac{\partial \mathbf{V}}{\partial x} \label{eq12}$$

## Lagrange's equation in Cartesian Coordinates¶

The Lagrangian mechanics can be formulated independent of the Newtonian mechanics and Cartesian coordinates; in fact Joseph-Louis Lagrange this new formalism from the principle of least action.

For simplicity, let's first deduce the Lagrange's equation for a particle in Cartesian Coordinates and from Newton's second law.

Because we want to deduce the laws of motion based on the mechanical energy of the particle, one can see that the time derivative of the expression for the linear momentum as a function of the kinetic energy, cf. Eq. (\ref{eq11}), is equal to the force acting on the particle and we can substitute the force in Newton's second law by this term:

$$\frac{\mathrm d }{\mathrm d t}\bigg(\frac{\partial T}{\partial \dot x}\bigg) = m\ddot x \label{eq13}$$

We saw that a conservative force can also be expressed in terms of the potential energy of the particle, cf. Eq. (\ref{eq12}); substituting the right side of the equation above by this expression, we have:

$$\frac{\mathrm d }{\mathrm d t}\bigg(\frac{\partial T}{\partial \dot x}\bigg) = -\frac{\partial V}{\partial x} \label{eq14}$$

Using the fact that:

$$\frac{\partial T}{\partial x} = 0 \quad and \quad \frac{\partial V}{\partial \dot x} = 0 \label{eq15}$$

We can write:

$$\frac{\mathrm d }{\mathrm d t}\bigg(\frac{\partial (T-V)}{\partial \dot x}\bigg) - \frac{\partial (T-V)}{\partial x} = 0 \label{eq16}$$

Defining the Lagrange or Lagrangian function, $\mathcal{L}$, as the difference between the kinetic and potential energy in the system:

$$\mathcal{L} = T - V \label{eq17}$$

We have the Lagrange's equation in Cartesian Coordinates for a conservative force acting on a particle:

$$\frac{\mathrm d }{\mathrm d t}\bigg(\frac{\partial \mathcal{L}}{\partial \dot x}\bigg) - \frac{\partial \mathcal{L}}{\partial x} = 0 \label{eq18}$$

Once all derivatives of the Lagrangian function are calculated, this equation will be the equation of motion for the particle. If there are $N$ independent particles in a three-dimensional space, there will be $3N$ equations for the system.
The set of equations above for a system are known as Euler–Lagrange equations, or Lagrange's equations of the second kind.

Let's see some simple examples of the Lagrange's equation in Cartesian Coordinates just to consolidate what was deduced above. The real application of Lagrangian mechanics is in generalized coordinates, what will see after these examples.

### Example 1: Particle moving under the influence of a conservative force¶

Let's deduce the equation of motion using the Lagrangian mechanics for a particle with mass $m$ moving in the 3D space under the influence of a conservative force.
The Lagrangian $(\mathcal{L} = T - V)$ of the particle is:

$$\mathcal{L}(x,y,z,\dot x,\dot y,\dot z) = \frac{1}{2}m(\dot x^2 + \dot y^2 + \dot z^2) - V(x,y,z)$$

The equations of motion for the particle are found by applying the Lagrange's equation for each coordinate.

For the x coordinate:

$$\frac{\mathrm d }{\mathrm d t}\left( {\frac{\partial \mathcal{L}}{\partial \dot{x}}} \right) = \frac{\partial \mathcal{L}}{\partial x }$$

And the derivatives of $\mathcal{L}$ are given by:

$$\begin{array}{rcl} &\dfrac{\partial \mathcal{L}}{\partial x} &=& -\dfrac{\partial \mathbf{V}}{\partial x} \\ &\dfrac{\partial \mathcal{L}}{\partial \dot{x}} &=& m\dot{x} \\ &\dfrac{\mathrm d }{\mathrm d t}\left( {\dfrac{\partial \mathcal{L}}{\partial \dot{x}}} \right) &=& m\ddot{x} \end{array}$$

hence:

$$m\ddot{x} = -\frac{\partial \mathbf{V}}{\partial x}$$

and similarly for the $y$ and $z$ coordinates.

For instance, if the conservative force is due to the gravitational field near Earth's surface $(\mathbf{V}=[0, mgy, 0])$, the Lagrange's equations (the equations of motion) are:

$$\begin{array}{rcl} m\ddot{x} &=& -\dfrac{\partial (0)}{\partial x} = 0 \\ m\ddot{y} &=& -\dfrac{\partial (mgy)}{\partial y} = -mg \\ m\ddot{z} &=& -\dfrac{\partial (0)}{\partial z} = 0 \end{array}$$

### Example 2: Ideal mass-spring system¶

Consider a system with a mass $m$ attached to an ideal spring (massless) with spring constant $k$ at the horizontal direction $x$. If the system is perturbed (a force is momentarily applied to the mass), we know the mass will oscillate around the rest position of the spring. Let's deduce the equation of motion for this system using the Lagrangian mechanics.
The Lagrangian $(\mathcal{L} = T - V)$ of the system is:

$$\mathcal{L}(x,\dot x) = \frac{1}{2}m\dot x^2 - \frac{1}{2}kx^2$$

The derivatives of $L$ are given by:

$$\begin{array}{rcl} &\dfrac{\partial \mathcal{L}}{\partial x} &=& -kx \\ &\dfrac{\partial \mathcal{L}}{\partial \dot{x}} &=& m\dot{x} \\ &\dfrac{\mathrm d }{\mathrm d t}\left( {\dfrac{\partial \mathcal{L}}{\partial \dot{x}}} \right) &=& m\ddot{x} \end{array}$$

And the Lagrange's equation (the equation of motion) is:

$$m\ddot{x} + kx = 0$$

## Generalized coordinates¶

The direct application of Newton's laws to mechanical systems results in a set of equations of motion in terms of Cartesian coordinates of each of the particles that make up the system. In many cases, this is not the most convenient coordinate system to solve the problem or describe the movement of the system. For example, in problems involving many particles, it may be convenient to choose a system that includes the coordinate of the center of mass. Another example is a serial chain of rigid links, such as a member of the human body or from a robot manipulator, it may be simpler to describe the positions of each link by the angles between links.

Coordinate systems such as these are referred as generalized coordinates. Generalized coordinates uniquely specify the positions of the particles in a system. Although there may be several generalized coordinates to describe a system, usually a judicious choice of generalized coordinates provides the minimum number of independent coordinates that define the configuration of a system (which is the number of degrees of freedom of the system), turning the problem simpler to solve.

Being a little more technical, according to Wikipedia):
"In classical mechanics, the parameters that define the configuration of a system are called generalized coordinates, and the vector space defined by these coordinates is called the configuration space of the physical system. It is often the case that these parameters satisfy mathematical constraints, such that the set of actual configurations of the system is a manifold in the space of generalized coordinates. This manifold is called the configuration manifold of the system."

In problems where it is desired to use generalized coordinates, one can write Newton's equations of motion in terms of Cartesian coordinates and then transform them into generalized coordinates. However, it would be desirable and convenient to have a general method that would directly establish the equations of motion in terms of a set of convenient generalized coordinates. In addition, general methods for writing, and perhaps solving, the equations of motion in terms of any coordinate system would also be desirable. The Lagrangian mechanics is such a method.

### Lagrange's equation in generalized coordinates¶

We have deduced the Lagrange's equation in Cartesian Coordinates from the Newton's law just because it was a simple form of getting to the final equations. But, by no means the Lagrangian Mechanics should be viewed as a consequence of Newton's laws and specific to Cartesian Coordinates. The Lagrangian Mechanics could be deduced completely independent of Newton's law.

The Lagrange's equation can be expressed in terms of generalized coordinates what makes the Lagrangian formalism even more powerful. In fact, we will have the same equation as we deduced before (the only explicit difference will be that we will use $q_i$ instead of the Cartesian coordinate).
See this notebook for a deduction of the Lagrange's equation in generalized coordinates.

Defining the Lagrange or Lagrangian function of a system with $N$ generalized coordinates:

$$\mathcal{L} \equiv \mathcal{L}(q_1,\dotsc,q_{N} ,\dot{q}_1,\dotsc,\dot{q}_{N} ) = T - V \label{eq46}$$

We have the Lagrange's equation:

$$\frac{\mathrm d }{\mathrm d t}\left( {\frac{\partial \mathcal{L}}{\partial \dot{q}_i }} \right)-\frac{\partial \mathcal{L}}{\partial q_i } = Q_{NCi} \quad i=1,\dotsc,N \label{eq47}$$

Where $Q_{NC}$ is a term representing a non-conservative force, such as a frictional force.

Once all derivatives of the Lagrangian function are calculated, this equation will be the equation of motion for each particle. If there are $N$ generalized coordinates to define the configuration of a system, there will be $N$ equations for the system.

The set of equations above for a system are known as Euler–Lagrange equations, or Lagrange's equations of the second kind.

### Example 3: Simple pendulum under the influence of gravity¶

Consider a pendulum with a massless rod of length $d$ and a mass $m$ at the extremity swinging in a plane forming the angle $\theta$ with vertical.
Although the pendulum moves at the plane, it only has one degree of freedom, which can be described by the angle $\theta$, the generalized coordinate. It is not difficult to find the equation of motion using Newton's law, but let's find it using the Lagrangian mechanics.

The kinetic energy is:

$$T = \frac{1}{2}md^2\dot\theta^2$$

And the potential energy is:

$$V = -mgd\cos\theta$$

The Lagrangian function is:

$$\mathcal{L} = \frac{1}{2}md^2\dot\theta^2 + mgd\cos\theta$$

And the derivatives are given by:

$$\begin{array}{rcl} &\dfrac{\partial \mathcal{L}}{\partial \theta} &=& -mgd\sin\theta \\ &\dfrac{\partial \mathcal{L}}{\partial \dot{\theta}} &=& md^2\dot{\theta} \\ &\dfrac{\mathrm d }{\mathrm d t}\left( {\dfrac{\partial \mathcal{L}}{\partial \dot{\theta}}} \right) &=& md^2\ddot{\theta} \end{array}$$

Finally, the Lagrange's equation (the equation of motion) is:

$$md^2\ddot\theta + mgd\sin\theta = 0$$

### Example 4: Double pendulum under the influence of gravity¶

Consider a double pendulum (one pendulum attached to another) with massless rods of length $d_1$ and $d_2$ and masses $m_1$ and $m_2$ at the extremities of each rod swinging in a plane forming the angles $\theta_1$ and $\theta_2$ with vertical.
This case could be solved using Newtonian mechanics, but it's not simple (e.g., see this link). Instead, let's use the Lagrangian mechanics to derive the equations of motion for each pendulum.

The position of masses $m_1$ and $m_2$ are:

$x_1 = d_1\sin\theta_1$
$y_1 = -d_1\cos\theta_1$

$x_2 = d_1\sin\theta_1 + d_2\sin\theta_2$
$y_2 = -d_1\cos\theta_1 - d_2\cos\theta_2$

The kinetic and potential energies of the system are:

$T = \frac{1}{2}m_1(\dot x_1^2 + \dot y_1^2) + \frac{1}{2}m_2(\dot x_2^2 + \dot y_2^2)$

$V = m_1gy_1 + m_2gy_2$

Let's use Sympy to help us; in fact we could solve this problem entirely in Sympy, see Lagrange’s method in Sympy, but for now let's do just the derivatives.

Let's import Sympy libraries and define some variables:

In [1]:
from sympy import Symbol, symbols, cos, sin, Matrix, simplify, Eq, latex
from sympy.physics.mechanics import dynamicsymbols, mlatex, init_vprinting
init_vprinting()
from IPython.display import display, Math

t = Symbol('t')
d1, d2, m1, m2, g = symbols('d1 d2 m1 m2 g', positive=True)
a1, a2 = dynamicsymbols('theta1 theta2')


The positions and velocities of masses $m_1$ and $m_2$ are:

In [2]:
x1, y1 = d1*sin(a1), -d1*cos(a1)
x2, y2 = d1*sin(a1) + d2*sin(a2), -d1*cos(a1) - d2*cos(a2)
x1d, y1d = x1.diff(t), y1.diff(t)
x2d, y2d = x2.diff(t), y2.diff(t)


$$x_1=d_{1} \operatorname{sin}\left(\theta_{1}\right)\quad \text{and} \quad \dot{x}_1=d_{1} \operatorname{cos}\left(\theta_{1}\right) \dot{\theta}_{1}$$
$$y_1=- d_{1} \operatorname{cos}\left(\theta_{1}\right)\quad \text{and} \quad \dot{y}_1=d_{1} \operatorname{sin}\left(\theta_{1}\right) \dot{\theta}_{1}$$
$$x_2=d_{1} \operatorname{sin}\left(\theta_{1}\right) + d_{2} \operatorname{sin}\left(\theta_{2}\right)\quad \text{and} \quad \dot{x}_2=d_{1} \operatorname{cos}\left(\theta_{1}\right) \dot{\theta}_{1} + d_{2} \operatorname{cos}\left(\theta_{2}\right) \dot{\theta}_{2}$$
$$y_2=- d_{1} \operatorname{cos}\left(\theta_{1}\right) - d_{2} \operatorname{cos}\left(\theta_{2}\right)\quad \text{and} \quad \dot{y}_2=d_{1} \operatorname{sin}\left(\theta_{1}\right) \dot{\theta}_{1} + d_{2} \operatorname{sin}\left(\theta_{2}\right) \dot{\theta}_{2}$$

The kinetic and potential energies of the system are:

In [3]:
T = m1*(x1d**2 + y1d**2)/2 + m2*(x2d**2 + y2d**2)/2
V = m1*g*y1 + m2*g*y2

display(Math(r'T=' + mlatex(simplify(T))))
display(Math(r'V=' + mlatex(simplify(V))))

$$T=\frac{d_{1}^{2} m_{1}}{2} \dot{\theta}_{1}^{2} + \frac{m_{2}}{2} \left(d_{1}^{2} \dot{\theta}_{1}^{2} + 2 d_{1} d_{2} \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} \dot{\theta}_{2} + d_{2}^{2} \dot{\theta}_{2}^{2}\right)$$
$$V=- g \left(d_{1} m_{1} \operatorname{cos}\left(\theta_{1}\right) + d_{1} m_{2} \operatorname{cos}\left(\theta_{1}\right) + d_{2} m_{2} \operatorname{cos}\left(\theta_{2}\right)\right)$$

The Lagrangian function is:

In [4]:
L = T - V

display(Math(r'\mathcal{L}=' + mlatex(simplify(L))))

$$\mathcal{L}=\frac{d_{1}^{2} m_{1}}{2} \dot{\theta}_{1}^{2} + d_{1} g m_{1} \operatorname{cos}\left(\theta_{1}\right) + g m_{2} \left(d_{1} \operatorname{cos}\left(\theta_{1}\right) + d_{2} \operatorname{cos}\left(\theta_{2}\right)\right) + \frac{m_{2}}{2} \left(d_{1}^{2} \dot{\theta}_{1}^{2} + 2 d_{1} d_{2} \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} \dot{\theta}_{2} + d_{2}^{2} \dot{\theta}_{2}^{2}\right)$$

And the derivatives are (let's write a function to automate this process):

In [5]:
def lagrange_terms(L, *q, show=True):
"""Calculate terms of Lagrange equations given the Lagrangian and q's.
"""
Lterms = []
for qi in q:
dLdqi = simplify(L.diff(qi))
Lterms.append(dLdqi)
dLdqdi = simplify(L.diff(qi.diff(t)))
Lterms.append(dLdqdi)
dtdLdqdi = simplify(dLdqdi.diff(t))
Lterms.append(dtdLdqdi)
if show:
display(Math(r'w.r.t.\;%s:'%latex(qi.func)))
display(Math(r'\dfrac{\partial\mathcal{L}}{\partial %s}='
%latex(qi.func) + mlatex(dLdqi)))
display(Math(r'\dfrac{\partial\mathcal{L}}{\partial\dot{%s}}='
%latex(qi.func) + mlatex(dLdqdi)))
display(Math(r'\dfrac{\mathrm d}{\mathrm{dt}}\left({\dfrac{'+
r'\partial\mathcal{L}}{\partial\dot{%s}}}\right)='
%latex(qi.func) + mlatex(dtdLdqdi)))
return Lterms

Lterms = lagrange_terms(L, a1, a2)

$$w.r.t.\;\theta_{1}:$$
$$\dfrac{\partial\mathcal{L}}{\partial \theta_{1}}=- d_{1} \left(d_{2} m_{2} \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} \dot{\theta}_{2} + g m_{1} \operatorname{sin}\left(\theta_{1}\right) + g m_{2} \operatorname{sin}\left(\theta_{1}\right)\right)$$
$$\dfrac{\partial\mathcal{L}}{\partial\dot{\theta_{1}}}=d_{1} \left(d_{1} m_{1} \dot{\theta}_{1} + m_{2} \left(d_{1} \dot{\theta}_{1} + d_{2} \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{2}\right)\right)$$
$$\dfrac{\mathrm d}{\mathrm{dt}}\left({\dfrac{\partial\mathcal{L}}{\partial\dot{\theta_{1}}}}\right)=d_{1} \left(d_{1} m_{1} \ddot{\theta}_{1} + m_{2} \left(d_{1} \ddot{\theta}_{1} - d_{2} \left(\dot{\theta}_{1} - \dot{\theta}_{2}\right) \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{2} + d_{2} \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \ddot{\theta}_{2}\right)\right)$$
$$w.r.t.\;\theta_{2}:$$
$$\dfrac{\partial\mathcal{L}}{\partial \theta_{2}}=d_{2} m_{2} \left(d_{1} \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} \dot{\theta}_{2} - g \operatorname{sin}\left(\theta_{2}\right)\right)$$
$$\dfrac{\partial\mathcal{L}}{\partial\dot{\theta_{2}}}=d_{2} m_{2} \left(d_{1} \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} + d_{2} \dot{\theta}_{2}\right)$$
$$\dfrac{\mathrm d}{\mathrm{dt}}\left({\dfrac{\partial\mathcal{L}}{\partial\dot{\theta_{2}}}}\right)=d_{2} m_{2} \left(- d_{1} \left(\dot{\theta}_{1} - \dot{\theta}_{2}\right) \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} + d_{1} \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \ddot{\theta}_{1} + d_{2} \ddot{\theta}_{2}\right)$$

Finally, the Lagrange's equations (the equations of motion) are:

In [6]:
for i in range(int(len(Lterms)/3)):
display(Eq(simplify(Lterms[3*i+2]-Lterms[3*i]), 0))

$$d_{1} \left(d_{1} m_{1} \ddot{\theta}_{1} + d_{1} m_{2} \ddot{\theta}_{1} + d_{2} m_{2} \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{2}^{2} + d_{2} m_{2} \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \ddot{\theta}_{2} + g m_{1} \operatorname{sin}\left(\theta_{1}\right) + g m_{2} \operatorname{sin}\left(\theta_{1}\right)\right) = 0$$
$$d_{2} m_{2} \left(- d_{1} \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1}^{2} + d_{1} \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \ddot{\theta}_{1} + d_{2} \ddot{\theta}_{2} + g \operatorname{sin}\left(\theta_{2}\right)\right) = 0$$

The motion of a double pendulum is very interesting; most of times it presents a chaotic behavior, see for example https://www.myphysicslab.com/pendulum/double-pendulum-en.html.

### Example 5: Double compound pendulum under the influence of gravity¶

Consider the double compound pendulum shown on the the right with length $d$ and mass $m$ of each rod swinging in a plane forming the angles $\theta_1$ and $\theta_2$ with vertical.
Let's use the Lagrangian mechanics to derive the equations of motion for each pendulum.

To calculate the potential and kinetic energy of the system, we will need to calculate the position and velocity of each pendulum. Now each pendulum is a rod with distributed mass and we will have to calculate the moment of rotational inertia of the rod. In this case, the kinetic energy of each pendulum will be given as the kinetic energy due to rotation of the pendulum plus the kinetic energy due to the speed of the center of mass of the pendulum, such that the total kinetic energy of the system is:

$T = \overbrace{\underbrace{\frac{1}{2}I_{cm}\dot\theta_1^2}_{\text{rotation}} + \underbrace{\frac{1}{2}m(\dot x_{1,cm}^2 + \dot y_{1,cm}^2)}_{\text{translation}}}^{\text{pendulum 1}} + \overbrace{\underbrace{\frac{1}{2}I_{cm}\dot\theta_2^2}_{\text{rotation}} + \underbrace{\frac{1}{2}m(\dot x_{2,cm}^2 + \dot y_{2,cm}^2)}_{\text{translation}}}^{\text{pendulum 2}}$

And the potential energy of the system is:

$V = mg\big(y_{1,cm} + y_{2,cm}\big)$

Once again, Sympy is on our side.

The position and velocity of the center of mass of the rods $1$ and $2$ are:

In [7]:
d, m, g = symbols('d m g', positive=True)
a1, a2 = dynamicsymbols('theta1 theta2')
I = m*d*d/12

x1, y1 = d*sin(a1)/2, -d*cos(a1)/2
x2, y2 = d*sin(a1) + d*sin(a2)/2, -d*cos(a1) - d*cos(a2)/2
x1d, y1d = x1.diff(t), y1.diff(t)
x2d, y2d = x2.diff(t), y2.diff(t)


$$x_1=\frac{d \operatorname{sin}\left(\theta_{1}\right)}{2}\quad \text{and} \quad \dot{x}_1=\frac{d \dot{\theta}_{1}}{2} \operatorname{cos}\left(\theta_{1}\right)$$
$$y_1=- \frac{d \operatorname{cos}\left(\theta_{1}\right)}{2}\quad \text{and} \quad \dot{y}_1=\frac{d \dot{\theta}_{1}}{2} \operatorname{sin}\left(\theta_{1}\right)$$
$$x_2=d \operatorname{sin}\left(\theta_{1}\right) + \frac{d \operatorname{sin}\left(\theta_{2}\right)}{2}\quad \text{and} \quad \dot{x}_2=d \operatorname{cos}\left(\theta_{1}\right) \dot{\theta}_{1} + \frac{d \dot{\theta}_{2}}{2} \operatorname{cos}\left(\theta_{2}\right)$$
$$y_2=- d \operatorname{cos}\left(\theta_{1}\right) - \frac{d \operatorname{cos}\left(\theta_{2}\right)}{2}\quad \text{and} \quad \dot{y}_2=d \operatorname{sin}\left(\theta_{1}\right) \dot{\theta}_{1} + \frac{d \dot{\theta}_{2}}{2} \operatorname{sin}\left(\theta_{2}\right)$$

The kinetic and potential energies of the system are:

In [8]:
T = I/2*(a1.diff(t))**2 + m*(x1d**2+y1d**2)/2 + I/2*(a2.diff(t))**2 + m*(x2d**2+y2d**2)/2
V = m*g*y1 + m*g*y2

display(Math(r'T=' + mlatex(simplify(T))))
display(Math(r'V=' + mlatex(simplify(V))))

$$T=\frac{d^{2} m}{6} \left(3 \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} \dot{\theta}_{2} + 4 \dot{\theta}_{1}^{2} + \dot{\theta}_{2}^{2}\right)$$
$$V=- \frac{d g}{2} m \left(3 \operatorname{cos}\left(\theta_{1}\right) + \operatorname{cos}\left(\theta_{2}\right)\right)$$

The Lagrangian function is:

In [9]:
L = T - V

display(Math(r'\mathcal{L}=' + mlatex(simplify(L))))

$$\mathcal{L}=\frac{d m}{6} \left(3 d \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} \dot{\theta}_{2} + 4 d \dot{\theta}_{1}^{2} + d \dot{\theta}_{2}^{2} + 9 g \operatorname{cos}\left(\theta_{1}\right) + 3 g \operatorname{cos}\left(\theta_{2}\right)\right)$$

And the derivatives are (let's write a function to automate this process):

In [10]:
Lterms = lagrange_terms(L, a1, a2)

$$w.r.t.\;\theta_{1}:$$
$$\dfrac{\partial\mathcal{L}}{\partial \theta_{1}}=- \frac{d m}{2} \left(d \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} \dot{\theta}_{2} + 3 g \operatorname{sin}\left(\theta_{1}\right)\right)$$
$$\dfrac{\partial\mathcal{L}}{\partial\dot{\theta_{1}}}=\frac{d^{2} m}{12} \left(6 \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{2} + 16 \dot{\theta}_{1}\right)$$
$$\dfrac{\mathrm d}{\mathrm{dt}}\left({\dfrac{\partial\mathcal{L}}{\partial\dot{\theta_{1}}}}\right)=\frac{d^{2} m}{6} \left(- 3 \left(\dot{\theta}_{1} - \dot{\theta}_{2}\right) \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{2} + 3 \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \ddot{\theta}_{2} + 8 \ddot{\theta}_{1}\right)$$
$$w.r.t.\;\theta_{2}:$$
$$\dfrac{\partial\mathcal{L}}{\partial \theta_{2}}=\frac{d m}{2} \left(d \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} \dot{\theta}_{2} - g \operatorname{sin}\left(\theta_{2}\right)\right)$$
$$\dfrac{\partial\mathcal{L}}{\partial\dot{\theta_{2}}}=\frac{d^{2} m}{12} \left(6 \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} + 4 \dot{\theta}_{2}\right)$$
$$\dfrac{\mathrm d}{\mathrm{dt}}\left({\dfrac{\partial\mathcal{L}}{\partial\dot{\theta_{2}}}}\right)=\frac{d^{2} m}{6} \left(- 3 \left(\dot{\theta}_{1} - \dot{\theta}_{2}\right) \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1} + 3 \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \ddot{\theta}_{1} + 2 \ddot{\theta}_{2}\right)$$

Finally, the Lagrange's equations (the equations of motion) are:

In [11]:
for i in range(int(len(Lterms)/3)):
display(Eq(simplify(Lterms[3*i+2]-Lterms[3*i]), 0))

$$\frac{d m}{6} \left(3 d \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{2}^{2} + 3 d \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \ddot{\theta}_{2} + 8 d \ddot{\theta}_{1} + 9 g \operatorname{sin}\left(\theta_{1}\right)\right) = 0$$
$$\frac{d m}{6} \left(- 3 d \operatorname{sin}\left(\theta_{1} - \theta_{2}\right) \dot{\theta}_{1}^{2} + 3 d \operatorname{cos}\left(\theta_{1} - \theta_{2}\right) \ddot{\theta}_{1} + 2 d \ddot{\theta}_{2} + 3 g \operatorname{sin}\left(\theta_{2}\right)\right) = 0$$

### Example 6: Simple pendulum on moving cart¶

Consider a simple pendulum with massless rod of length $d$ and mass $m$ at the extremity of the rod forming an angle $\theta$ with the vertical direction under the action of gravity. The pendulum swings freely from a cart with mass $M$ that moves at the horizontal direction pushed by a force $F_x$.
Let's use the Lagrangian mechanics to derive the equations of motion for the system

From the figure on the right, because of the constraints introduced by the constant length of the rod and the motion the cart can perform, good generalized coordinates to describe the configuration of the system are $x$ and $\theta$. Let's use these coordinates.

The positions of the cart (c) and of the pendulum tip (p) are:

$x_c = x$
$y_c = 0$

$x_p = x + d \sin \theta$
$y_p = -d \cos \theta$

The velocities of the cart and of the pendulum are:

$\dot{x}_c = \dot{x}$
$\dot{y}_c = 0$

$\dot{x}_p = \dot{x} + d \dot{\theta} \cos \theta$
$\dot{y}_p = d \dot{\theta} \sin \theta$

The kinetic energy of the system is:

$$T = \frac{1}{2} M \big(\dot x_c^2 + \dot y_c^2 \big) + \frac{1}{2}m\,\big( \dot x_p^2 + \dot y_p^2 \big)$$

And the potential energy of the system is:

$$V = M g y_c + m g y_p$$

The Lagrangian function is:

$$\begin{array}{rcl} \mathcal{L} &=& \frac{1}{2} M \dot x^2 + \frac{1}{2}m\,\bigg[ \big(\dot{x} + d \dot{\theta} \cos\theta \big)^2 + \big(d \dot{\theta} \sin \theta \big)^2 \bigg] + m g d \cos \theta \\ &=& \frac{1}{2} (M+m) \dot x^2 + m\dot{x}d\dot{\theta}\cos\theta + \frac{1}{2}md^2\dot{\theta}^2 + m g d \cos \theta \end{array}$$

The derivatives w.r.t. $x$ are:

$$\begin{array}{rcl} \dfrac{\partial \mathcal{L}}{\partial x} &=& 0 \\ \dfrac{\partial \mathcal{L}}{\partial \dot{x}} &=& (M+m) \dot{x} + m d \dot{\theta} \cos \theta \\ \dfrac{\mathrm d }{\mathrm d t}\left( {\dfrac{\partial \mathcal{L}}{\partial \dot{x}}} \right) &=& (M+m)\ddot{x} + m d \ddot{\theta} \cos \theta - m d \dot{\theta}^2 \sin \theta \end{array}$$

The derivatives w.r.t. $\theta$ are:

$$\begin{array}{rcl} \dfrac{\partial \mathcal{L}}{\partial \theta} &=& -m\dot{x}d\dot{\theta}\sin\theta - mgd\sin\theta \\ \dfrac{\partial \mathcal{L}}{\partial \dot{\theta}} &=& m\dot{x}d\cos\theta + md^2\dot{\theta} \\ \dfrac{\mathrm d }{\mathrm d t}\left( {\dfrac{\partial \mathcal{L}}{\partial \dot{\theta}}} \right) &=& m\ddot{x}d\cos\theta - m\dot{x}d\dot{\theta}\sin\theta + md^2\ddot{\theta} \end{array}$$

Finally, the Lagrange's equations (the equations of motion) are:

$$\begin{array}{rcl} (M+m)\ddot{x} + md\big(\ddot{\theta} \cos\theta - \dot{\theta}^2 \sin\theta\big) = F_x \\ \ddot{\theta} + \dfrac{\ddot{x}}{d}\cos\theta + \dfrac{g}{d}\sin\theta = 0 \end{array}$$

## Considerations on the Lagrangian mechanics¶

The Lagrangian mechanics does not constitute a new theory in classical mechanics; the results of a Lagrangian or Newtonian analysis must be the same for any mechanical system, only the method used to obtain the results is different.

We are accustomed to think of mechanical systems in terms of vector quantities such as force, velocity, angular momentum, torque, etc., but in the Lagrangian formalism the equations of motion are obtained entirely in terms of the kinetic and potential energies (scalar operations) in the configuration space. Another important aspect of the force vs. energy analogy is that in situations where it is not possible to make explicit all the forces acting on the body, it is still possible to obtain expressions for the kinetic and potential energies.

In fact, the concept of force does not enter into Lagrangian mechanics. This is an important property of the method. Since energy is a scalar quantity, the Lagrangian function for a system is invariant for coordinate transformations. Therefore, it is possible to move from a certain configuration space (in which the equations of motion can be somewhat complicated) to a space that can be chosen to allow maximum simplification of the problem.

## Problems¶

1. Derive the Lagrange's equation for a mass-spring system where the spring is attached to the ceiling and the mass in hanging in the vertical.
2. Derive the Lagrange's equation for an inverted pendulum in the vertical.
3. Derive the Lagrange's equation for the following system:
4. Derive the Lagrange's equation for a spring pendulum, a simple pendulum where a mass $m$ is attached to a massless spring with spring constant $k$ and length at rest $d_0$.