Lagrangian function for two swivelling masses attached by a spring I am just having a hard time finding the Lagrangian for this question. There are two massless rigid rods lengths R (connected to mass M) and r (connected to mass m) which both pivot around a fixed point and with respect to each other. The masses at the end of each rod are connected by a massless spring with elastic constant k and negligible length at rest. I am supposed to use the law of cosines to find the Lagrangian, but I am quite stuck.

 A: 
use polar coordinate to obtain the components of the vectors
$~\vec R_M~,\vec r_m~$ in intertial system $~x,y~$
from here you can obtain the kinetic energy. for the potential
energy of the spring you need the spring vector $~\vec R_s~$
A: To get the Lagrangian, we need to write both the kinetic energy and the potential energy of the system in terms of the generalized coordinates. Here, it is very useful to use the angles of the two fixed-length rods $\theta_1$ and $\theta_2$, or a similar combination such as the angle of the rod of length $R$, labeled as $\theta$ in your diagram, and the angle between the rods, labeled as $\varphi$. Both are ok, and I will use the second one as you implied that in your diagram.
You can derive the kinetic term from $E_k = mv^2/2 = m(\dot{x}^2+\dot{y}^2)/2$ and then use
$$ x_1 = R\cos\theta \\ y_1 = R\sin\theta \\ x_2 = r \cos(\theta+\varphi) \\ y_2 = r \sin(\theta+\varphi)$$
to get
$$\dot{x}_1^2 + \dot{y}_1^2 = R^2\dot{\theta}^2 \\
\dot{x}_2^2 + \dot{y}_2^2 = r^2 (\dot{\theta}+\dot{\varphi})^2$$
and finally
$$ T = \frac{1}{2}\left[M R^2 \dot{\theta}^2 + m r^2 \left(\dot{\theta} + \dot{\varphi}\right)^2\right]$$
The potential energy is given by Hook's law $E_p = k \Delta x^2/2$ with $\Delta x$ the elongation of the spring from rest. Here, the length at rest is negligible (i.e. zero), which means that it is just the distance between the two fixed masses. From the law of cosines that you gave we can see that the two rods create a triangle, with sides $R$, $r$ and the distance between the masses as the third side. So given that the angle between them is $\varphi$, we have
$$ \Delta x ^2 = R^2 + r^2 - 2R r \cos\varphi$$
giving us the potential energy as
$$ V = \frac{1}{2}k (R^2 + r^2 - 2R r \cos\varphi)$$
and the Lagrangian is $L = T - V$
