# Why does $U = T S - P V + \sum_i \mu_i N_i$?

From the first law of thermodynamics:

$$dU = TdS - PdV + \sum_i\mu_i dN_i.$$

Quoting Wikipedia:

As conjugate variables to the composition $N_{i}$ the chemical potentials are intensive properties, intrinsically characteristic of the system, and not dependent on its extent. Because of the extensive nature of $U$ and its variables, the differential $dU$ may be integrated and yields an expression for the internal energy:

$$U = T S - P V + \sum_i \mu_i N_i.$$

Emphasis mine. $U$, $S$, $V$, and $N$ are extensive while $T$, $P$, and $\mu$ are intensive. How does this fact allow $U$ to be integrated this way?

-

I'm going to suppress the sum over different chemical potentials for simplicity as it doesn't materially affect the argument. If we write $U$ as a function of $S,V,N$, then extensivity of $U$ is mathematically defined as follows $$U(\lambda S, \lambda V, \lambda N) = \lambda U(S, V, N)$$ Physically, this is saying that if you scale the quanities that characterize your physical system by a certain amount, then the energy scales by the same amount. The mathematical terminology for this is that $U$ is a homogeneous function of degree $1$ in $S$, $V$, and $N$. Now, there is a theorem on homogeneous functions called Euler's homogeneous function theorem which (up to a technical assumption or two) states that a function $f:\mathbb R^n \to\mathbb R$ is homogeneous of degree $k>0$, namely $$f(\lambda x) = \lambda^kf(x)$$ if and only if $$k f(x)=x\cdot \nabla f(x)$$ Since the energy is a homogeneous function of degree 1, this theorem tells us that \begin{align} U(S,V,N) &= (S,V,N)\cdot \nabla U(S,V,N) \\ &= S\left(\frac{\partial U}{\partial S}\right)_{V,N} + V\left(\frac{\partial U}{\partial V}\right)_{S,N} + N\left(\frac{\partial U}{\partial N}\right)_{S,V} \end{align} On the other hand, the fundamental thermodynamic relation you wrote (as the first equation) above allows us to identify \begin{align} \left(\frac{\partial U}{\partial S}\right)_{V,N} &= T\\ \left(\frac{\partial U}{\partial V}\right)_{S,N} &= -P\\ \left(\frac{\partial U}{\partial N}\right)_{S,V} &= \mu \end{align} so that we get the desired result: $$U(S,V N) = TS - PV + \mu N$$
It's almost the same as one of the previous answers, but if you allow the Gibbs free energy to be $$\Phi=U-TS+pV,$$ thus $$d\Phi=-SdT+Vdp+\mu dN.$$ $\Phi$ is extensive but among its natural variables only $N$ is extensive too, so you can impose $\Phi(T,p,N)=N\varphi(T,p)$ for some $\varphi.$ Then you get $$\varphi=\left(\frac{\partial \Phi}{\partial N}\right)_{T,p}=\mu \implies \Phi=\mu N.$$ By substitution in the first formula: $\mu N =U -TS +pV$ implies $$U=TS-pV+\mu N.$$ A similar argument can be used thanks to the grandpotential $$\Omega = U-TS-\mu N$$ having $$d\Omega = -SdT-pdV-Nd\mu.$$ Its only extensive variable is $V$ then $\Omega(T,V,\mu) = V\omega(T,\mu).$ Then $$\omega =\left(\frac{\partial \Omega}{\partial V}\right)_{T,\mu}=-p$$ then $\Omega = -pV$ and again $-pV = U-TS-\mu N.$