Solution (source code)

= Solution

Let
$$
\omega=\sqrt{\frac{k}{m}},
\qquad
H(x,p)=\frac{p^2}{2m}+\frac12m\omega^2x^2.
$$
An orbit of energy $E>0$ is an ellipse in <phase space>, with intercepts $x=\pm\sqrt{2E/(m\omega^2)}$ and $p=\pm\sqrt{2mE}$. Its enclosed area is $2\pi E/\omega$, so the <action variable> is
$$
I=\frac1{2\pi}\oint p\,dx=\frac E\omega.
$$
The transformation
$$
\boxed{x=\sqrt{\frac{2I}{m\omega}}\sin\theta,
\qquad
p=\sqrt{2m\omega I}\cos\theta}
$$
is canonical and gives the <action-angle variables>. Substitution yields
$$
\boxed{H(I)=\omega I,\qquad \dot I=0,\qquad\dot\theta=\omega.}
$$