\[
U=\frac{1}{2} L I^{2}=\frac{1}{2} \Phi I=\frac{\Phi^{2}}{2 L} .
\]
Now let's express this magnetic energy in terms of the magnetic field \( \mathbf{B}(\mathbf{x}) \) inside the coil. The magnetic flux though the coil can be expressed in terms of the vector potential as
\[
\Phi=\oint_{\text {coil }} d \mathbf{x} \cdot \mathbf{A}(\mathbf{x})
\]
hence the magnetic energy
\[
U=\frac{1}{2} I \Phi=\frac{1}{2} \oint_{\text {coil }} I d \mathbf{x} \cdot \mathbf{A}(\mathbf{x}) \text {. }
\]
This formula assumes a coil made of thin wires; if we replace them with thicker conductors carrying some volume currents \( \mathbf{J}(\mathbf{x}) \), then in the integral (8) we replace \( I d \mathbf{x} \rightarrow d^{3} \mathbf{x} \mathbf{J}(\mathbf{x}) \), hence
\[
U=\frac{1}{2} \iiint d \mathbf{x} \mathbf{J}(\mathbf{x}) \cdot \mathbf{A}(\mathbf{x})
\]
Now let's integrate by parts using \( \nabla \times \mathbf{A}=\mathbf{B} \) and the Ampere's Law \( \nabla \times \mathbf{H}=\mathbf{J} \). For any vector fields \( \mathbf{f} \) and \( \mathbf{g} \), we have
\[
\nabla \cdot(\mathbf{f} \times \mathbf{g})=\mathrm{g} \cdot(\nabla \times \mathbf{f})-\mathbf{f} \cdot(\nabla \times \mathbf{g}),
\]
2
hence
\[
\nabla \cdot(\mathbf{H} \times \mathbf{A})=\mathbf{A} \cdot(\nabla \times \mathbf{H})-\mathbf{H} \cdot(\nabla \times \mathbf{A})=\mathbf{A} \cdot \mathbf{J}-\mathbf{H} \cdot \mathbf{B}
\]
and therefore
\[
\iiint_{\mathcal{V}} d^{3} \mathbf{x} \mathbf{J} \cdot \mathbf{A}=\oiint(\mathbf{H} \times \mathbf{A}) \cdot d^{2} \mathbf{a}+\iiint_{\mathcal{V}} d^{3} \mathbf{x} \mathbf{H} \cdot \mathbf{B} .
\]
for any integration volume \( \mathcal{V} \) and its surface \( \mathcal{S} \). In eq. (9), we integrate over the conductor's volume, but we may just as well extend the integration volume \( \mathcal{V} \) to the whole space. Consequently, in eq. (12) the surface integral on the RHS goes away, and the magnetic energy (9) becomes
\[
U=\frac{1}{2} \iiint_{\substack{\text { whole } \\ \text { space }}} d^{3} \mathrm{x} \mathbf{H} \cdot \mathbf{B} .
\]