Solution (source code)

= Solution

For a fixed <magnetic dipole moment> in an externally imposed uniform <magnetic field>, the <torque> is $\boldsymbol\tau=\mathbf m\times\mathbf B$. Its component in the direction of increasing $\theta$ is $-mB\sin\theta$. Since $\tau_\theta=-dU/d\theta$, integration gives
$$
\boxed{U(\theta)=-\mathbf m\cdot\mathbf B=-mB\cos\theta,}
$$
where the arbitrary additive constant has been chosen so that $U=0$ at $\theta=\pi/2$.

\Image[/past-exam-of-the-physics-b-course-of-the-university-of-cambridge/2021/paper-2-a4-dipole-energy.png]
{title=Potential energy of a permanent magnetic dipole in a uniform magnetic field}

The aligned orientation $\theta=0$ has the minimum $U=-mB$ and is stable against small rotations; the anti-aligned orientation $\theta=\pi$ has the maximum $U=+mB$ and is unstable. The graph repeats every $2\pi$. Rotating quasistatically from alignment to anti-alignment requires external work $\Delta U=2mB$.