The demagnetizing field is the magnetic field created by the magnetic sample upon itself. The associated energy is:
where H is the demagnetizing field. This field depends on the magnetic configuration itself, and it can be found by solving:
where −∇·M is sometimes called magnetic charge density. The solution of these equations (c.f. magnetostatics) is:
where r is the vector going from the current integration point to the point where H is being calculated.
It is worth noting that the magnetic charge density can be infinite at the edges of the sample, due to M changing discontinuously from a finite value inside to zero outside of the sample. This is usually dealt with by using suitable boundary conditions on the edge of the sample.
The energy of the demagnetizing field favors magnetic configurations that minimize magnetic charges. In particular, on the edges of the sample, the magnetization tends to run parallel to the surface. In most cases it is not possible to minimize this energy term at the same time as the others. The static equilibrium then is a compromise that minimizes the total magnetic energy, although it may not minimize individually any particular term.