Here is another delightful question that I ran across on Quora: Someone was wondering how the constraint of a rigid pendulum, $x^2+y^2=r^2$, can be incorporated into a Lagrangian derivation of its equation of motion.

It can be, of course, but my first suggestion is that it is easier to use polar coordinates, as the one-dimensional nature of a (rigid) pendulum can be managed much more easily there.

So let the length of the pendulum arm be $l$, its mass $m$, and its angular displacement be $\theta$. Then, its kinetic energy is given by
$$
K=\textstyle\frac{1}{2}m(l\dot\theta)^2,
$$(where I use the overdot to represent the time derivative) and its potential energy is
$$
U=-mgl\cos\theta.
$$So the Lagrangian is
$$
L=K-U=\textstyle\frac{1}{2}m(l\dot\theta)^2+mgl\cos\theta.
$$The corresponding Euler--Lagrange equation is
$$
\frac{\partial L}{\partial\theta}-\frac{d}{dt}\frac{\partial L}{\partial\dot\theta}=0,
$$or
$$
-mgl\sin\theta-ml^2\ddot\theta=0.
$$For small displacements, $\sin\theta\simeq\theta$, and we can solve this equation analytically:
$$
\theta=C_1\sin\left(\sqrt{\frac{g}{l}}~t\right)+C_2\cos\left(\sqrt{\frac{g}{l}}~t\right),
$$which, of course, is the standard result.

So how would we do this using Cartesian coordinates? Which is a bit of an overkill, of course, since we are now making the problem two-dimensional. The answer is that we need to use a Lagrange-multiplier, which is the standard way to introduce a constraint into a problem of Lagrangian mechanics.

The kinetic energy in Cartesian coordinates is, of course
$$
K=\textstyle\frac{1}{2}m(\dot{x}^2+\dot{y}^2),
$$whereas the potential energy is
$$
U=mgy.
$$The unconstrained Lagrangian would therefore be $K-U=\textstyle\frac{1}{2}m(\dot{x}^2+\dot{y}^2)-mgy$. But now we introduce the constraint, in the form $mu(x^2+y^2-r^2)$, where we treat $u$ as a third, dummy, dynamical variable. (The factor of $m$ is not essential, I am just using it for later convenience.) The full Lagrangian, therefore, reads
$$
L=\textstyle\frac{1}{2}m(\dot{x}^2+\dot{y}^2)-mgy+mu(x^2+y^2-r^2).
$$You can see right away what the constraint does. The Euler--Lagrange equation with respect to $u$ simply yields the constraint equation:
$$
\frac{\partial L}{\partial u}-\frac{d}{dt}\frac{\partial L}{\partial\dot{u}}=m(x^2+y^2-r^2)=0.
$$The other two Euler--Lagrange equations are
\begin{align*}
\frac{\partial L}{\partial x}-\frac{d}{dt}\frac{\partial L}{\partial\dot{x}}&=2mux-m\ddot{x}=0,\\
\frac{\partial L}{\partial y}-\frac{d}{dt}\frac{\partial L}{\partial\dot{y}}&=2muy-mg-m\ddot{y}=0.
\end{align*}(NB: Without the constraint, that is, with $u=0$, we just get the equations $\ddot{x}=0$, $\ddot{y}=-g$, i.e., the equation of a vertically falling object.) We can divide through by $m$ (now you know why I included $m$ in the Lagrange-multiplier term) and rearrange:
\begin{align*}
y&=-\sqrt{r^2-x^2},\\
\ddot{x}-2ux&=0,\\
\ddot{y}-2uy+g&=0.
\end{align*}We can express both $y$ (along with its time derivatives) and $u$ in terms of $x$ (note the choice of a minus sign for $y$, indicating that we are at the bottom of the pendulum):
\begin{align*}
y&=-\sqrt{r^2-x^2},\\
\dot{y}&=\frac{x\dot{x}}{\sqrt{r^2-x^2}},\\
\ddot{y}&=\frac{x\ddot{x}}{\sqrt{r^2-x^2}}+\frac{r^2\dot{x}^2}{(r^2-x^2)^{3/2}},\\
u&=\frac{\ddot{x}}{2x}.
\end{align*}Substituting these into the differential equation for $y$, we get
$$
\frac{x\ddot{x}}{\sqrt{r^2-x^2}}+\frac{r^2\dot{x}^2}{(r^2-x^2)^{3/2}}+\frac{\ddot{x}}{x}\sqrt{r^2-x^2}+g=0.
$$Rearranging, we get
$$
\frac{r}{\sqrt{r^2-x^2}}\ddot{x}+\frac{r}{(r^2-x^2)^{3/2}}\dot{x}^2x+\frac{g}{r}x=0.
$$When the displacement of the pendulum is small, i.e., $x\ll r$, then $\sqrt{r^2-x^2}\simeq r$ and the second term in the equation above vanishes altogether, leaving us with
$$
\ddot{x}+\frac{g}{r}x=0,
$$which is solved trivially by
$$
x=C_1\sin\left(\sqrt{\frac{g}{r}}~t\right)+C_2\cos\left(\sqrt{\frac{g}{r}}~t\right).
$$I think this problem neatly demonstrates not just the power of the Lagrange-multiplier method but also the importance of picking a coordinate system that is most suitable for the problem at hand. Cartesian coordinates are not always the best choice.