Step 1: Torque on a magnetic dipole.
A magnetic dipole of moment \( M \) placed in a uniform magnetic field \( B \), making an angle \( \theta \) with the field, experiences a restoring torque \( \tau = MB\sin\theta \).
Step 2: Work done for a small rotation.
To rotate the dipole further by a small angle \( d\theta \) against this torque, the work done is \( dW = \tau\, d\theta = MB\sin\theta\, d\theta \).
Step 3: Total work from \( \theta_1 \) to \( \theta_2 \).
\[ W = \int_{\theta_1}^{\theta_2} MB\sin\theta\, d\theta = MB\left[-\cos\theta\right]_{\theta_1}^{\theta_2} = MB(\cos\theta_1 - \cos\theta_2) \]
Step 4: Measuring the angle from the field direction.
If the dipole starts parallel to the field (\( \theta_1 = 0 \)) and is turned to angle \( \theta \), then \( W = MB(\cos 0 - \cos\theta) = MB(1 - \cos\theta) \).
Step 5: Rotation by \( 180^\circ \) from the parallel position.
Here \( \theta_1 = 0^\circ \) and \( \theta_2 = 180^\circ \).
\( W = MB(\cos 0^\circ - \cos 180^\circ) = MB(1 - (-1)) = 2MB \).
\[\boxed{W = MB(\cos\theta_1 - \cos\theta_2); \quad W_{180^\circ} = 2MB}\]