(N/A) As shown in the figure,a bar magnet is held at an angle $\theta$ with the direction of a uniform magnetic field $\overrightarrow{B}$.
The torque acting on the bar magnet is $\tau = m B \sin \theta$.
The potential energy $U_m$ of the bar magnet is the work done in rotating it against the magnetic torque:
$U_m = \int \tau(\theta) d\theta = \int m B \sin \theta d\theta = -m B \cos \theta$
Thus,the potential energy is given by the dot product:
$U_m = -\vec{m} \cdot \overrightarrow{B}$
Special cases:
$(1)$ If the bar magnet makes an angle $\theta = 0^{\circ}$ with the magnetic field $\overrightarrow{B}$:
$U_m = -m B \cos 0^{\circ} = -m B$. This is the minimum potential energy,and the magnet is in its most stable position.
$(2)$ If the bar magnet makes an angle $\theta = 180^{\circ}$ with the magnetic field $\overrightarrow{B}$:
$U_m = -m B \cos 180^{\circ} = +m B$. This is the maximum potential energy,and the magnet is in its most unstable position.
$(3)$ If the bar magnet is perpendicular to $\overrightarrow{B}$ $(\theta = 90^{\circ})$:
$U_m = -m B \cos 90^{\circ} = 0$.