## deriving the energy of a magnetic dipole in a magnetic field

For an undergraduate electrodynamics homework problem we were asked to derive the following:

(griffiths 6.21)

show that the energy of a magnetic dipole in a magnetic field B is given by -

U = -m.B

where
B is the magnetic field
and
m is the dipole moment.

I went about this by saying that the dipole will experiance a torque when its in the magnetic field that will effectivly move it through an angular displacement such that it points in the same direction as the magnetic field (the effective angle between them is 0 as they are now parallel to eachother)

so we get the following

N(torque) = m.B = m.B.sin(theta)

U = (intergral){B.m.sin(theta) d(theta)}
= -B.m.cos(theta) + C

or we can assume that its a definite integral from the bounds theta to pi/2
if we were to substitute pi/2 into the above solution we'd get

(bounds for the integral 0 to pi/2)

= -B.m.cos (0) + B.m.cos(pi/2)
= -B.m

However this would not be the electroynamics approach as i didn't use an of maxwell's equations, so how would i use the other approach?