When a magnetic dipole of dipole moment M is rotated through an angle θ with respect to the direction of the field H, then work done is done. This work is stored as magnetic potential energy in the dipole. The potential energy of a magnetic dipole of magnetic moment M placed in magnetic field H is Uθ=−M⋅H=−MHcosθ
where θ is angle between the vector M and H. Initially the dipole possesses minimum potential energy U0,therefore work required to turn through angle θ is W=Uθ−U0=−MHcosθ−(−MHcosθ) =−MHcosθ+MH ∴W=MH(1−cosθ)