It is shown that the internal energy, can be computed from the canonical partition function using
\begin{align*}U=-\frac{\partial}{\partial\beta} \log Z.\end{align*}
We will obtain an expression for the internal energy of a system in terms of the partition function.
The average energy of a system in contact with a heat reservoir at temperature \(T\) is \begin{align*}U =\bar {E}= & \sum_k E_kP(E_k)=Z^{-1}\sum E_{\text{micro states}\ k}e^{-\beta E_k}\end{align*}where \(Z\) is the partition function
\begin{equation}Z=\sum_ke^{-\beta E_k}\end{equation}Differentiating w.r.t. \(\beta\) gives \begin{align*}\frac{\partial Z}{\partial\beta} =& - \sum_k E_k e^{-\beta E_k}\\\therefore \quad U=&-\frac{\partial}{\partial\beta} \log Z.\end{align*}where $\beta=$ depends on temperature and is independent of the system.