Solution (source code)

= Solution

Allowing azimuthal motion makes the endpoint speed satisfy $v^2=l^2(\dot\theta^2+\sin^2\theta\,\dot\varphi^2)$. The light-rod <spherical pendulum> therefore has
$$
\boxed{\mathcal L=\frac12ml^2\left(\dot\theta^2+\sin^2\theta\,\dot\varphi^2\right)+mgl\cos\theta.}
$$
The azimuth $\varphi$ is a <cyclic coordinate>, since the <Lagrangian> is independent of it. Its conjugate <canonical momentum> is the conserved vertical <angular momentum>:
$$
\boxed{p_\varphi=ml^2\sin^2\theta\,\dot\varphi=J_z=\text{constant}.}
$$
For a finite uniform rod, replace $ml^2$ by $I$ and $mgl$ by $A$ from <8/a/i>. The same azimuthal symmetry then gives $p_\varphi=I\sin^2\theta\,\dot\varphi$.

The <Hamiltonian> is the <Legendre transform> of this <Lagrangian>. With $p_\theta=I\dot\theta$,
$$
H=\frac{p_\theta^2}{2I}+\frac{p_\varphi^2}{2I\sin^2\theta}-A\cos\theta
=\frac I2\left(\dot\theta^2+\sin^2\theta\,\dot\varphi^2\right)-A\cos\theta.
$$
There is no explicit time dependence, so $dH/dt=\partial H/\partial t=0$. Here $H$ also equals the total mechanical <energy>: the constraint is stationary, the <kinetic energy> is quadratic in generalized velocities, and gravity is conservative. The apparent singularity of the momentum expression at $\sin\theta=0$ is a spherical-coordinate singularity; trajectories reaching a pole must be described consistently, and nonzero $J_z$ prevents reaching it at finite <energy>.