How do I calculate the lagrangian of a planet orbiting a star and find its equation of motion? So, it's basically what the title says. I was messing around with some systems that I found would be interesting to calculate their lagrangians, and one of these systems is a planet in an elliptical orbit around a star.
When I was trying to solve this problem, I started by just writing the potential energy $$U=-\frac{GMm}{r}$$ and then the kinetic energy. But then I remembered that the velocity of a body in an elliptical orbit can be written as $$v=\sqrt{GMm\left(\frac{2}{r}-\frac{1}{a}\right)}$$ where $a$ is the semimajor axis of the ellipse. And so the kinetic energy is just that squared times $m$.
So my final lagrangian came out as $$L=\frac{1}{2}GMm^{2}\left(\frac{2}{r}-\frac{1}{a}\right)+\frac{GMm}{r}$$ However, when I tried to solve for the equation of motion, I got to some nonsensical conclusions. I know I probably blundered along the way, but I don't know where. I would be grateful with any help or hints towards solving this!
 A: The problem is that you've put the cart before the horse. The fact that the velocity of a body in an elliptical orbit can be written as an implicit function of position (essentially the equation of motion) should be the result of solving the Lagrange equations; taking that known end result and plugging in back in the beginning will lead to nonsense. More precisely, the Lagrangian is a function of position and velocity, and Lagrange's equations depend crucially on the partial derivatives of the Lagrangian with respect to these generalized coordinates in order to derive the equation of motion. Substituting back in implicit relationships between the position and momentum derived from the equation of motion gives you something that isn't the Lagrangian anymore.
To give a very simple example, consider a spring with potential $U=\frac{1}{2}kx^2$, and thus a Lagrangian
$$
\mathcal{L} = \frac{1}{2} mv^2 - \frac{1}{2} kx^2
$$
Solving Lagrange's equations will tell you that $ma = -kx$, from which you can derive that $mv^2 = k(A^2-x^2)$ for some amplitude $A$. Plugging that back in to the formal expression for the Lagrangian gives
$$
\frac{1}{2} k(A^2 - x^2) - \frac{1}{2} kx^2 = \frac{1}{2}kA^2 - kx^2
$$
As a Lagrangian, this would be nonsense, since the important dependence on $v$ has been eliminated. The expression is essentially the value of the Lagrangian itself along the path in phase space taken by the system.
