When a coil is connected to a voltage source, a counter-voltage is induced. Work must be done against this counter-voltage in order to move charges and establish a current. As with the capacitor, we first consider an infinitesimal change in energy $\mathrm d W$, caused by moving a charge $\mathrm d Q$:
\[
\mathrm{d} W = U(Q)\,\mathrm d Q
\]
Substituting into the induction law gives the following integral over the current:
\[
W = \int_0^{Q_0} -L \frac{\mathrm d I}{\mathrm d t}\,\mathrm d Q = \int_0^{I_0} -LI\,\mathrm d I
\]
In the last step, we used the relation $I = \mathrm d Q/\mathrm d t$. Integration and evaluation of the limits yields the energy stored in the magnetic field:
\[
\boxed{E_\mathrm{mag} = \tfrac{1}{2} L I_0^2}
\]
This expression has exactly the same form as the capacitor energy formula, with inductance $L$ taking the role of capacitance $C$.
Example problem: A coil with inductance $20\,\text{mH}$ carries a current of $5\,\text{A}$. How much energy is stored in the magnetic field and available again after the voltage source is disconnected? Result: 250 mJ