Solution (source code)

= Solution

Take $\theta$ from the downward vertical. Under the conventional light-rod idealization, only the endpoint <point mass> contributes appreciably. Its speed is $l\dot\theta$, and its gravitational <potential energy> is $-mgl\cos\theta$. Therefore
$$
\boxed{\mathcal L=\frac12ml^2\dot\theta^2+mgl\cos\theta.}
$$
An additive constant in the <potential energy> has no effect on the <Euler-Lagrange equation>.

\b[Rod-mass qualification:] “thin” and “uniform” do not imply negligible <mass>. If the uniform rod has a finite unspecified <mass> $M_r$, its pivot <moment of inertia> is $M_rl^2/3$ and its <centre of mass> lies $l/2$ from the pivot. For a <pendulum with a uniform rod and an endpoint mass>, the general answer is
$$
\mathcal L=\frac12I\dot\theta^2+A\cos\theta,\qquad
I=l^2\left(m+\frac{M_r}{3}\right),\qquad
A=gl\left(m+\frac{M_r}{2}\right).
$$
This formula covers either convention without inventing a rod <mass>. A result involving only the specified $m,l,g$ uses $M_r=0$ unless an additional relation for $M_r$ is stated.