# How to obtain differential equations of motion using Lagrangian dynamics?

I'm confused to find a set of differential equations of motion of a pair of masses, m1 and m2 joined by a spring of constant k.

The unstretched length of the spring is L, and the initial conditions are; x1[0]=0, x2[0]=L+a, x1'[t]=0, x2'[t]=0. x1[t] and x2[t] are the positions of the masses m1 and m2 at any time t.

I used Lagrangian dynamics to find the differential equations of motion, but I'm not sure my codes are correct. And I have no idea where I need to put the initial conditions to find the differential equations. Can someone help me?

Needs["VariationalMethods"];
T = 0.5*m1*x1'[t]^2 + 0.5*m2*x2'[t]^2;
V = 0.5*k*(x2[t] - x1[t] - L);
lag = T - V;
eqn[n_] = EulerEquations[lag, {x1[t], x2[t]}, t];

-
You do not really count the unstretched length of the spring is L` in these problems, since one always starts from the relaxed position. The initial stretched length of the spring is already account for. also the PE in spring is $\frac{1}{2} k x^2$ ? And do you really need a package to do this? You asked same question mathematica.stackexchange.com/questions/43256/… you just need to use the text book formula. One line per one equation. (if you use the textbook formula, I think you'll understand more than using blackbox function) – Nasser Mar 4 '14 at 6:48
Here is a demo demonstrations.wolfram.com/… that uses 3 springs and 2 masses. It has a link to detailed Lagrangian derivation of equations of motion which is similar to your problem (the link is below the demo, in the detailed section) – Nasser Mar 4 '14 at 6:57
@Nasser Thank you for your comments! – ailee Mar 4 '14 at 7:06
@Nasser: "You do not really count the unstretched length of the spring is L in these problems, since one always starts from the relaxed position. The initial stretched length of the spring is already account for." This is quite incorrect. Only if the problem is one-dimensional, and one end of the spring is fixed, and you choose the origin of the coordinate system at the relaxed position of the other end, then your statement is valid. This is not the case for this problem. – Rahul Mar 4 '14 at 18:40