Q:

Position of two blocks connected by a spring as a function of time

Two blocks A and B are connected by a spring of spring constant $k$. $A$ is imparted an initial velocity $u$ towards the right along positive x-axis. If $B$ were fixed then the motion of $A$ could be described using simple harmonic motion equation $x=\sqrt{\frac{m}{k}}u\sin(\omega t)$. But here $B$ isn't fixed and can move freely on the frictionless surface. So, while velocity of $A$ reduces, velocity of $B$ increases. So, how can we find the position of $A$ and $B$ as a function of time in this situtation ?
MY ATTEMPT :
$$mu-kx dt = mv_{A}$$
$$kx=mv_{B}$$
$$\frac{1}{2}mu^2=\frac{1}{2}kx^2+\frac{1}{2}mv^2_{A}+\frac{1}{2}mv^2_{B}$$
($x$ is compression of spring at any time $t$)
But after this I don't know how to find position of $A$ and $B$.
Please note that I have studied only Newtonian Mechanics and Simple Harmonic Motion till now. So do not use very high level concepts. Also, please let me know if I have violated any homework policy. I read through the homework questions policy and then wrote this question. I hope it doesn't appear as a homework question. Still, if it does let me know if I can edit it somehow. Hints and suggestions are appreciated.

A:

In this question, it is very easy if we shift to the center of mass (COM) frame (If you don't know about COM, go and read about it and then continue reading).

The velocity of the COM frame is
$$v_{cm}=\frac{mu_a + mu_b}{m+m}= \frac{u_a + u_b}{2} = \frac{u}{2}$$
Since there are no external forces, $v_{cm}$ is constant.
Let the velocities of $A$ and $B$ in the centre of mass frame be $v_a$ and $v_b$. Thus,
$$v_a = u_a-v_{cm}= \frac{u_a-u_b}{2}$$
$$v_b = u_b-v_{cm}= \frac{u_b-u_a}{2}$$
This means that $v_a=-v_b$, which is wonderful! If you stare at it for a while, you will realize that that this means the bodies oscillate in phase and that their displacements are equal and opposite in the COM frame. This is how it would look like:

Now, let us assume that the positions of $A$ and $B$ in the COM frame are:
$$x_a= -\alpha \sin(\omega t)$$
$$x_b= \alpha \sin(\omega t)$$
Writing the force equation in the COM,
$$m\ddot x_a = -k(x_a-x_b)$$
$$\Longrightarrow m\omega^2 \alpha \sin(\omega t)= 2k\alpha \sin(\omega t)$$
$$\Longrightarrow \omega=\sqrt{\frac{2k}{m}}$$

Now, let us try to find $\alpha$ by applying energy conservation in the rest frame. The maximum extension in the spring is $2\alpha$ which happens when the velocities of A and B are equal to  $\frac{u}{2}$ (i.e, they are at rest in the COM frame). Applying energy conservation, we get
$$\frac{k(2\alpha)^2}{2} + 2\frac{mv_{cm}^2}{2}=\frac{mu^2}{2}$$
$$\Longrightarrow 2k\alpha^2 = \frac{mu^2}{4}$$
$$\Longrightarrow \alpha = \frac{u}{2}\sqrt{\frac{m}{2k}}$$

Now, the position of the centre of mass is given by $x_{cm} =v_{cm}t=\frac{u t}{2}$. Thus, the position of A and B are:
$$X_a(t)= \frac{u t}{2} -\alpha \sin(\omega t) $$
$$X_b(t)= \frac{u t}{2} + \alpha \sin(\omega t)$$
Where
$$\omega=\sqrt{\frac{2k}{m}}$$
$$\alpha = \frac{u}{2}\sqrt{\frac{m}{2k}}$$

