# Enthalpy of mixing in a regular solution

In a mixture of $A$ and $B$ where $A$ and $B$ are randomly mixed and the average coordination number is $z$. If there are $n_B$ molecules of $B$ in the mixture these will be in contact with a total of $zn_B$ molecules, of which $x_Azn_B$ contacts are with molecules of A. This is the total number of BA contacts. Before mixing, these contacts would have been BB contacts in pure B and so the change in energy on mixing is, for the B molecules: $$x_Azn_B(H_{AB}-H_{BB})$$ Similarly for A: $$x_Bzn_A(H_{AB}-H_{AA})$$ This is the point where I get confused. Upon adding together the following expression is somehow achieved: $$\Delta H_m=\frac{zn_An_B}{2(n_A+n_B)}(2H_{AB}-H_{AA}-H_{BB})$$ Its really just the fraction at the start of that expression I am confused by. However, now the bit I really do not understand. Differentiating with respect to $n_i$ gives $\mu_i$? And apparently this gives $\beta x_j^2$. I have absolutely no idea how. Please can someone explain what's going on here?