Magnetic energy and electrostatic potential energy are related by Maxwell's equations. The potential energy of a magnet or magnetic moment
m
{\displaystyle \mathbf {m} }
in a magnetic field
B
{\displaystyle \mathbf {B} }
is defined as the mechanical work of the magnetic force (actually magnetic torque) on the re-alignment of the vector of the magnetic dipole moment and is equal to:
E
p
,
m
=
−
m
⋅
B
{\displaystyle E_{\rm {p,m}}=-\mathbf {m} \cdot \mathbf {B} }
while the energy stored in an inductor (of inductance
L
{\displaystyle L}
) when a current
I
{\displaystyle I}
flows through it is given by:
E
p
,
m
=
1
2
L
I
2
.
{\displaystyle E_{\rm {p,m}}={\frac {1}{2}}LI^{2}.}
This second expression forms the basis for superconducting magnetic energy storage.
Energy is also stored in a magnetic field. The energy per unit volume in a region of space of permeability
μ
0
{\displaystyle \mu _{0}}
containing magnetic field
B
{\displaystyle \mathbf {B} }
is:
u
=
1
2
B
2
μ
0
{\displaystyle u={\frac {1}{2}}{\frac {B^{2}}{\mu _{0}}}}
More generally, if we assume that the medium is paramagnetic or diamagnetic so that a linear constitutive equation exists that relates
B
{\displaystyle \mathbf {B} }
and
H
{\displaystyle \mathbf {H} }
, then it can be shown that the magnetic field stores an energy of
E
=
1
2
∫
H
⋅
B
d
V
{\displaystyle E={\frac {1}{2}}\int \mathbf {H} \cdot \mathbf {B} \ \mathrm {d} V}
where the integral is evaluated over the entire region where the magnetic field exists.For a magnetostatic system of currents in free space, the stored energy can be found by imagining the process of linearly turning on the currents and their generated magnetic field, arriving at a total energy of:
{\displaystyle \mathbf {J} }
is the current density field and
A
{\displaystyle \mathbf {A} }
is the magnetic vector potential. This is analogous to the electrostatic energy expression
1
2
∫
ρ
ϕ
d
V
{\textstyle {\frac {1}{2}}\int \rho \phi \ \mathrm {d} V}
; note that neither of these static expressions do apply in the case of time-varying charge or current distributions.