Solution (source code)

= Solution

Use <spherical coordinates> about the fixed pivot, with fixed rod <length> $l$, <polar angle> $\theta$ measured from the downward vertical, and <azimuth> $\phi$. The bob's <position> is
$$
\mathbf r=l(\sin\theta\cos\phi,\ \sin\theta\sin\phi,\ -\cos\theta)
$$
when the Cartesian $z$ axis points upward. Differentiation gives <kinetic energy> $T=ml^2(\dot\theta^2+\sin^2\theta\dot\phi^2)/2$. Taking zero <gravitational potential energy> at pivot height gives $V=-mgl\cos\theta$. The <spherical pendulum>'s <Lagrangian> is therefore
$$
\boxed{\mathcal L=T-V=\frac12ml^2(\dot\theta^2+\sin^2\theta\dot\phi^2)+mgl\cos\theta.}
$$
The rod is light, rigid and of fixed <length>; pivot <friction> and air <drag> are neglected. A rigid rod, unlike a string, may sustain either tensile or compressive constraint <force>.