Lagrange Examples

With Piwik, I noticed that my article about the Euler Lagrange equation is one of the most popular pages on my site. During the Numerik course, I created an animation for the double pendulum. Using that source code, I went through some other simple mechanical problems that were covered in my classical mechanics lecture and made animations for those. The derivation of the differential equations are on the pages as well.

You can find the source code for both the numerical integration of the differential equations, as well as the animation itself, on the project page: https://github.com/martin-ueding/lagrange-simulator.

Double pendulum

See this Wikipedia article for the derivation.

Spring pendulum

A mass is attached to a spring. It can swing (angle $\phi$) and change the distance to the anchor (radius $r$). The spring is assumed to have no mass and can be neglected in terms of kinetic energy.

Derivation

The spring has an equlibrium position when $r = l$.

Kinetic and potential energy:

$$T = \frac m2 \left[\dot r^2 + r^2 \dot\phi^2 \right]$$$$V = - mg r \cos(\phi) + \frac k2 [r - l]^2$$

Lagrangian:

$$L = \frac m2 \left[\dot r^2 + r^2 \dot\phi^2\right] + mg r \cos(\phi) - \frac k2 [r - l]^2$$

Euler Lagrange equation for `r`:

$$\frac{\partial L}{\partial r} = \frac{\mathrm d}{\mathrm dt} \frac{\partial L}{\partial \dot r}$$$$\frac{\partial L}{\partial r} = m r \dot\phi^2 + mg \cos(\phi) - k [r - l]$$$$\frac{\mathrm d}{\mathrm dt} \frac{\partial L}{\partial \dot r} = \frac{\mathrm d}{\mathrm dt} m \dot r = m \ddot r$$

Euler Lagrange equation for `phi`:

$$\frac{\partial L}{\partial \phi} = \frac{\mathrm d}{\mathrm dt} \frac{\partial L}{\partial \dot\phi}$$$$\frac{\partial L}{\partial \phi} = - mg r \sin(\phi)$$$$\frac{\mathrm d}{\mathrm dt} \frac{\partial L}{\partial \dot\phi} = \frac{\mathrm d}{\mathrm dt} m r^2 \dot\phi = m \left[2 r \dot r \dot \phi + r^2 \ddot \phi\right]$$

Bring it into the form of `y' = f(y)`:

$$\ddot r = r \dot \phi^2 + g \cos(\phi) - \frac km [r - l]$$$$\ddot \phi = - \frac gr \sin(\phi) - 2 \frac{\dot r}r \dot\phi$$

The input for the ODE solver is:

$$\begin{aligned} \frac{\mathrm d}{\mathrm dt} \begin{pmatrix} r \ \phi \ \dot r \ \dot \phi \end{pmatrix} = \begin{pmatrix} \dot r \ \dot \phi \ r \dot \phi^2 + g \cos(\phi) - \frac km [r - l] \ - \frac gr \sin(\phi) - 2 \frac{\dot r}r \dot\phi \end{pmatrix} \end{aligned}$$

Ball in cone

A ball rolls within a circular cone. Friction and the moment of inertia of the ball is neglected completely. You can think of the ball as a point mass.

Diagram of the ball in a cone
Diagram of the ball in a cone

Derivation

Kinetic and potential energy:

$$T = \frac m2 \cdot (\dot r^2 + r^2 \dot\phi^2 + \dot z^2)$$$$V = mgz$$

Holonomic constraint:

$$r - z \tan(\alpha) = 0$$

To increase readability, I will use $\beta := \tan(\alpha)$.

I put that constraint into the energies and yield a Lagrangian without $r$:

$$L = \frac m2 \cdot \left((\beta^2 + 1) \dot z^2 + \beta^2 z^2 \dot\phi^2 \right) - mgz$$

Euler Lagrange equation:

$$\frac{\partial L}{\partial z} = \frac{\mathrm d}{\mathrm dt} \frac{\partial L}{\partial \dot z}$$$$\frac{\partial L}{\partial z} = m \beta^2 z \dot\phi^2 - mg$$$$\frac{\mathrm d}{\mathrm dt} \frac{\partial L}{\partial \dot z} = \frac{\mathrm d}{\mathrm dt} m \cdot (\beta^2 + 1) \dot z = m \cdot (\beta^2 + 1) \ddot z$$$$m \beta^2 z \dot\phi^2 - mg = m \cdot (\beta^2 + 1) \ddot z$$$$\frac{\partial L}{\partial \phi} = \frac{\mathrm d}{\mathrm dt} \frac{\partial L}{\partial \dot\phi}$$$$\frac{\partial L}{\partial \phi} = 0$$$$\frac{\mathrm d}{\mathrm dt} \frac{\partial L}{\partial \dot\phi} = \frac{\mathrm d}{\mathrm dt} m \beta^2 z^2 \dot\phi = m \beta^2 \cdot (2 z \dot z \dot\phi + z^2 \ddot\phi)$$$$0 = m \beta^2 \cdot (2 z \dot z \dot\phi + z^2 \ddot\phi)$$

Bring it into the form of $y' = f(y)$:

$$\ddot z = \frac{\beta^2 z \dot\phi^2 - g}{\beta^2 + 1}$$$$\ddot \phi = - 2 \frac{\dot z}{z}$$

The input for the ODE solver is:

$$\begin{aligned} \frac{\mathrm d}{\mathrm dt} \begin{pmatrix} z \ \phi \ \dot z \ \dot \phi \end{pmatrix} = \begin{pmatrix} \dot z \ \dot \phi \ \frac{\beta^2 z \dot\phi^2 - g}{\beta^2 + 1} \ - 2 \frac{\dot z}{z} \end{pmatrix} \end{aligned}$$

Simple pendulum

A mass is attached to a string, which is anchored in the ceiling. The string is assumed to be massless. Also, there shall be no friction.

Derivation

Kinetic and potential energy:

$$T = \frac m2 l^2 \dot\phi^2$$$$V = - mg l \cos(\phi)$$

Lagrangian:

$$L = \frac m2 l^2 \dot\phi^2 + mg l \cos(\phi)$$

Euler Lagrange equation:

$$\frac{\partial L}{\partial \phi} = \frac{\mathrm d}{\mathrm dt} \frac{\partial L}{\partial \dot\phi}$$$$- mg l \sin(\phi) = ml^2 \ddot \phi$$

Bring it into the form of $y' = f(y)$:

$$\ddot \phi = - \frac gl \sin(\phi)$$

The input for the ODE solver is:

$$\begin{aligned} \frac{\mathrm d}{\mathrm dt} \begin{pmatrix} \phi \ \dot \phi \end{pmatrix} = \begin{pmatrix} \dot \phi \ - \frac gl \sin(\phi) \end{pmatrix} \end{aligned}$$