Solution (source code)

= Solution

Define the <canonical momentum> $p_i=\partial L/\partial\dot q_i$ and the <energy function> $E=\sum_i p_i\dot q_i-L$. Along an <Euler-Lagrange equation> solution,
$$
\frac{dE}{dt}=\sum_i\left(\dot p_i-\frac{\partial L}{\partial q_i}\right)\dot q_i-\frac{\partial L}{\partial t}=-\frac{\partial L}{\partial t}.
$$
Thus an autonomous <Lagrangian> has conserved $E$. For a <simple harmonic oscillator>, $L=m\dot x^2/2-kx^2/2$ and $\boxed{E=m\dot x^2/2+kx^2/2}$ is constant. For ordinary time-independent mechanical coordinates this is the total <mechanical energy>.