Thermodynamics of a single spring in gravitational potential I'm trying to solve this thermodynamics problem. There's a spring of elastic constant $k$ attached to a mass $m$ in it's equilibrium position 
\begin{equation}
x_{0}=\frac{mg}{k}
\end{equation}
Suddenly we add a new mass $M$ in the end of the spring so the spring oscillates for a while until it settles in a new equilibrium position. This happens at a constant temperature $T$ in contact with the enviroment.
\begin{equation}
x_f=\frac{(m+M)g}{k}
\end{equation}
The question is: How much heat was released or absorbed by the spring during that process and how much did the entropy of the universe increase?
My problem is that I have two alternative methods for this and they give two results. Furthermore, there are certain subtleties of thermodynamics that are confusing me. Let me explain the two methods:
First Method:
The energy that the system has lost is equal to:
\begin{equation}
\begin{aligned}
\Delta E &= E_f-E_i \\ &= -\frac{g^2(m+M)^2}{k}+\frac{g^2m^2}{k}
\end{aligned}
\end{equation}
since we know that this energy was lost due to friction it had to be released in the form of heat so we get
\begin{equation}
Q_{out}=-\Delta E >0
\end{equation}
If we write down the first law of thermodynamics for this system and we use the fact that the internal energy of an harmonic oscillator is $U=Nk_bT$ we get
\begin{equation}
\begin{aligned}
dU&=\delta Q- \delta W \\ 0 &= \delta Q - F dx \\ \rightarrow \delta Q &=kx dx
\end{aligned}
\end{equation}
where I'm using that the work done by the system is $\delta W = kxdx$. Integrating this we get
\begin{equation}
\begin{aligned}
Q&=k \int_{x_i}^{x_f}xdx \\ &=-\Delta E
\end{aligned}
\end{equation}
which agrees with the prediction that the heat should we equal to the change in energy. 
Second Method:
Instead of using the energy of a thermal harmonic oscillator let's use the mechanical energy plus something that might depend on temperature (which is not important since the temperature remains constant):
\begin{equation}
dU=kx dx+CdT
\end{equation}
and let's think of the masses as external agents applying a force on the spring. Then the first law takes the form
\begin{equation}
\begin{aligned}
dU&=\delta Q - \delta W \\kx dx &= \delta Q + (m+M)g dx 
\end{aligned}
\end{equation}
Here comes an important doubt: If the system and it's enviroment are doing unequal forces, which one should we take into account when we write the work? In the first method I was using the force that the spring does, here I'm using the force that the masses do. Now we can integrate the heat to get
\begin{equation}
\begin{aligned}
Q&=\int \Big[kx-(m+M)g\Big] dx \\ &= -\frac{g^2M^2}{2k}
\end{aligned}
\end{equation}
On one hand I like this equation because when we take the limit $M\rightarrow 0 $ the heat goes straight to zero. Also, I like this method because in the first law it's clear that if we do the expansion cuasistationarily (meaning that the external force is always equal to the Hooke force) then there's no heat exchange. However, I still don't see why the heat should be less than the energy lost in the expansion. 
Regarding the entropy question, I'm just not sure how to calculate it.
 A: In my judgment, both methods are incorrect (although your second method is closer to being correct).  The form of the first law that you should be using for the universe (masses plus surroundings) is the more general form:
$$\Delta E=\Delta U+\Delta(PE)+\Delta(EE)+\Delta(KE)=Q-W$$where $\Delta U$ is the change in internal energy of the universe (masses plus surroundings), $\Delta (PE)$ is the change in potential energy of the masses, $\Delta (EE)$ is the change in stored elastic energy of the spring, and $\Delta (KE)$ is the change in kinetic energy of the masses.  The universe (masses plus surroundings) is isolated, so this combined system does no work W and receives no heat Q. And the change in kinetic energy of the masses is zero.  So the above equation reduces to:  $$\Delta U+\Delta(PE)+\Delta(EE)=0$$
The change in stored elastic energy of the spring is $$\Delta (EE)=\frac{1}{2}k(x_f^2-x_i^2)=\frac{g^2}{k}\left(mM+\frac{M^2}{2}\right)$$The change in potential energy of the masses is given by
$$\Delta (PE)=-(M+m)g(x_f-x_i)=-\frac{g^2}{k}(M^2+Mm)$$So the change in internal energy of the universe between its initial and final states is given by:
$$\Delta U=\frac{M^2g^2}{2k}$$
If we devised an alternative reversible process to bring about this same change in internal energy of the universe, we would have to transfer an amount of heat to the universe (from some unspecified source) equal to $\Delta U$.  Since the temperature would be virtually unchanged, the entropy change of the universe would be $$\Delta E=\frac{\Delta U}{T}=\frac{M^2g^2}{2kT}$$
A: Here is an alternate approach to the same problem.
Call the masses and spring the "system," and call everything else the "surroundings."  If we do a force balance on the mass and spring, we obtain:
$$(M+m)g-kx-F_D=(M+m)\frac{d^2x}{dt^2}$$ where $F_D$ is the (damping) air drag force exerted by the surroundings on the system.  Note that $F_D$ always has the same sign as the downward velocity of the masses.  If we multiply this equation by the velocity dx/dt, and integrate between time equal to zero and infinite time (when the masses have stopped moving), we obtain $$-\Delta (PE)-\Delta (EE)-W_D=0$$So the work done by the surroundings on the system to damp out the motion of the masses is $$W_D=-\Delta (PE)-\Delta (EE)=-\frac{M^2g^2}{2k}$$Therefore, the air drag work done by the system on its surroundings is minus this:
$$W=-W_D=\Delta (PE)+\Delta (EE)=\frac{M^2g^2}{2k}\tag{1}$$If we now apply the first law of thermodynamics to the spring-mass system, we obtain:$$\Delta U_{syst}+\Delta (PE)+\Delta (EE)=Q-W\tag{2}$$where here $\Delta U_{syst}$ represents the change in internal energy of just the spring-mass system (not like in the previous development where it represented the internal energy change for the universe).  If we combine Eqns. 1 and 2, we now obtain:  $$\Delta U_{syst}=Q$$where Q represents the typically tiny amount of heat flow from the surroundings to the system.  This heat flow is typically tiny because both the system and surroundings experience the same equilibrium temperature change, and the mass times heat capacity of the system is typically much smaller than the mass times heat capacity of the surroundings.  
If we now apply the first law of thermodynamics to the surroundings, we obtain:
$$\Delta U_{surr}=-Q-W_D=-Q+\frac{M^2g^2}{2k}$$So the internal energy change of the universe is $$\Delta U=\Delta U_{syst}+\Delta U_{surr}=\frac{M^2g^2}{2k}$$
A: In method 1 you didn't take gravitational potential energy loss into account. You also forgot a minus signal on the definitions of $x_0$ and $x_f$ (remember we must have $x_f<x_0$). By correcting that we get:
\begin{align}
\Delta E 
&= \Delta E_g + \Delta E_s \\
&= (M+m)g(x_f - x_0) + \frac{k}{2} (x_f^2 - x_0^2) \\
&= g^2\frac{-M(M+m)}{k} + \frac{k}{2} \frac{g^2}{k^2} \left[ (M+m)^2 - m^2 \right] \\
&= \frac{g^2}{2k}\left[ (M+m)(m-M) - m^2 \right] \\
&= - \frac{M^2g^2}{2k},
\end{align}
Which is in agreement with the results of the second method.
