Two masses on a frictionless surface connected with a spring I have a problem with an assignment with two masses on a frictionless plane connected with a spring.
Both masses are 1 kg, and the distance between them (the length of the spring) is 0.4 m.
The spring constant k = 8 N/m. The first mass is given a initial velocity of 1 m/s.
The assignment is based on Walter Lewins lecture here at 48 minutes:
http://videolectures.net/mit801f99_lewin_lec15/
I'm asked to write a python program displaying the positions of both masses, but I'm struggling with the formulas.
This is what i have so far:
Acceleration for each object (m = 1 kg):
$$m1:\mathrm{d}^2x/\mathrm{d}t^2 = -k(x1-x2+L)t$$
$$m2:\mathrm{d}^2x/\mathrm{d}t^2 = -k(x2-x1-L)t$$
Equation: 
$$ω=\mathrm{sqrt}(k/m)=\mathrm{sqrt(8)}$$
$$x = A\cos(ωt)+B\sin(ωt)$$
for m1: t=0, pos=0 and v=1:
$$x1=(1/ω)\cos(ωt)/2$$
i divide by 2 to get the oscillation of just m1 (wrong?)
for m2: t=0, pos=0.4, v=0:
$$0.4\cos(ωt)$$
again,i divide by 2 to get the oscillation of just m2
Problem:
How do i make m2 dependent of m1's position? And how do i make them "move" along as they should? I have tried to create a function for center of mass, CM(t)=0.2*0.5t, to use as a reference of movement, but i cant get it right.
Can someone give me a pointer in the right direction?
 A: Once the mass is released, the center of mass will move at a constant velocity. Superposed on that is the relative motion of the two masses - first towards each other, then away. They will be in exact antiphase so the center of mass has constant velocity.
Your mistake was to set x up as a cosine function - that implies that it is at an extreme of position at t=0 when in fact it is at an extreme of velocity (so position is "in the middle" and you need to use a sin function).  That leads me to the following (you will need to check this and prove individual steps along the way which I did not give... it's not solving the differential equations but jumping to the answer - but I think that you should be able to work towards this answer with the above hint).
import numpy as np
import math
import matplotlib.pyplot as plt

k = 8
l = 0.4
m1 = 1.0
m2 = 1.0
m_reduced = (m1*m2)/(m1+m2) 
omega = np.sqrt(k / m_reduced)
t = np.linspace(0, 8.0 * math.pi / omega, 500)
a1 = 1.0 / (2*omega) # because we know dx/dt at t=0 - but half is due to c.o.m.
a2 = - a1
v_com = 0.5 # since one mass moves at 1 m/s and the other is initially stationary
x1 = a1 * np.sin(omega * t) + v_com * t
x2 = a2 * np.sin(omega * t) + v_com * t + l
plt.figure()
plt.plot(t, x1, label='x1')
plt.plot(t, x2, label='x2')
plt.xlabel('time (s)')
plt.ylabel('position (m)')
plt.legend()
plt.show()


