In classical electrodynamics, a magnetic dipole moment \(\mu\) is associated with a current loop (see diagram below), and represents the magnitude and orientation of a magnetic dipole. It is a pseudo-vector, whose direction is perpendicular to the plane of the loop and given by the right hand thumb rule.

The magnitude of a magnetic dipole moment of a current loop is defined as
$$\mu=IA\; \; \; \; \; \; \; \; 60$$
where \(I\) is current, and \(A\) is the area of the loop.
Rewriting eq60 in terms of \(I=\frac{q}{t}=\frac{qv_{\perp}}{2\pi r}\) (where \(q\) is charge, \(t\) is time and \(v_{\perp}\) is the tangential velocity of the charged particle) and using \(A=\pi r^{2}\), we have \(\mu=\frac{q}{2m}rmv_{\perp}\), where \(m\) is the mass of the charged particle. Substitute eq59 in \(\mu=\frac{q}{2m}rmv_{\perp}\), we have the relation between magnetic dipole moment and angular momentum:
$$\boldsymbol{\mu}=\gamma\boldsymbol{L}\; \; \; \; \; \; \; \; 61$$
where \(\gamma=\frac{q}{2m}\) is the classical gyromagnetic ratio.
When placed in an external magnetic field , the magnetic dipole moment experiences a torque, whose energy is
$$U=-\boldsymbol{\mu}\cdot\boldsymbol{B}\; \; \; \; \; \; \; \; 62$$

Question
How is eq62 derived?
Answer
A magnetic field exerts a force on a charge given by \(\boldsymbol{F}=q\boldsymbol{v}\times\boldsymbol{B}\), where \(\boldsymbol{v}\) is the velocity of the charge. Therefore, a current-carrying loop in a magnetic field experiences magnetic forces on its different sides. In a uniform magnetic field, these forces may oppose each other while producing a torque that tends to rotate the loop.
In order to rotate a current loop, we must do work \(W\) against the torque \(\tau\) due to the magnetic field \(\boldsymbol{B}\). For a rotating system (see diagram I below), \(W=-\int F_{\perp}ds\), where according to convention, the negative sign is added so that work on the system by the field is positive.
For small \(\theta\), \(ds=rd\theta\). Since \(W=-\Delta U\) and the definition of torque acting at at point relative to the origin is \(\boldsymbol{\tau}=\boldsymbol{r}\times\boldsymbol{F}\), we have
$$U_f-U_i=\int F_{\perp}ds=\int_{\theta_i}^{\theta_f}Fsin\theta rd\theta=\int_{\theta_i}^{\theta_f}\tau d\theta\; \; \; \; \; \; \; \; 62a$$

Diagram II below shows a square current loop with side 1 parallel to side 3 and side 2 parallel to side 4 (not shown in diagram). The length of each side is \(b\).

Since \(\tau=r\times F=\frac{b}{2}Fsin\theta+\frac{b}{2}Fsin\theta\) and \(F=BIb\) (\(\theta=90^{\circ}\)),
$$U_f-U_i=\int_{\theta_i}^{\theta_f}IBb^{2}sin\theta\, d\theta=IA B\left (- cos\theta_f+cos\theta_i \right)\; \; \; \; \; \; \; \; 62b$$
where \(A\) is the area of the loop.
Substitute eq60 in the above equation,
$$U_f-U_i=\mu B\left (cos\theta_i-cos\theta_f \right)\; \; \; \; \; \; \; \; 63$$
Comparing eq62a and eq62b, torque can also be expressed as
$$\boldsymbol{\tau}=BIAsin\theta=\boldsymbol{\mu}\times\boldsymbol{B}\; \; \; \; \; \; \; \; 64$$
The torque exerted by the magnetic field on the magnetic dipole tends to rotate the dipole towards a lower energy state. So, we let \(\theta_i=90^{\circ}\) with \(U_i=0\) and eq63 becomes \(U_f=-\mu Bcos\theta_f\), or simply:
$$U=-\boldsymbol{\mu}\cdot \boldsymbol{B}\; \; \; \; \; \; \; \; 65$$
Other than a torque, the external magnetic field may exert another force on the current loop. For a magnetic field pointing in the \(z\)-direction, \(\boldsymbol{B}=B_z(x,y,z)\boldsymbol{k}\), where \(B_z(x,y,z)\) is a scalar function. We substitute eq65 in \(F_z=-\frac{\partial U}{\partial z}\) to give
$$\boldsymbol{F_z}=\frac{\partial(\boldsymbol{\mu}\cdot\boldsymbol{B})}{\partial z}=\frac{\partial[(\mu_x\boldsymbol{i}+\mu_y\boldsymbol{j}+\mu_z\boldsymbol{k})\cdot B_z(x,y,z)\boldsymbol{k}]}{\partial z}=\mu_z\frac{\partial B_z(x,y,z)}{\partial z}\; \; \; \; \; \; \; \; 66$$
where \(\boldsymbol{i}\), \(\boldsymbol{j}\) and \(\boldsymbol{k}\) are unit vectors, and \(\frac{\partial B_z(x,y,z)}{\partial z}\) is the gradient of the external magnetic field along the \(z\)-direction.
For a uniform field, \(B_z(x,y,z)=B_0\) (i.e. a constant) and \(\boldsymbol{F_z}=0\); while for an inhomogenous field, \(B_z(x,y,z)=B_0+\alpha z\), where \(\alpha\) is a scalar representing the change in \(B_0\) along the \(z\)-axis, and \(\boldsymbol{F_z}\neq 0\).
Substituting eq61 in eq62,
$$U=-\gamma\boldsymbol{B}\cdot\boldsymbol{L}\; \; \; \; \; \; \; \; 67$$
For an inhomogenous magnetic field pointing in the \(z\)-direction, the above equation becomes
$$U=-\gamma(B_0+\alpha z)\boldsymbol{k}\cdot(L_x\boldsymbol{i}+L_y\boldsymbol{j}+L_z\boldsymbol{k})=-\gamma(B_0+\alpha z)L_z\; \; \; \; \; \; \; \; 68$$
Eq68 is used as a starting point in analysing results from the Stern-Gerlach experiment.

Question
Does the magnetic field \(\boldsymbol{B}=(B_0+\alpha z)\boldsymbol{k}\) violate Maxwell’s 2nd equation of \(\nabla\cdot\boldsymbol{B}=0\) where \(\nabla=\frac{\partial}{\partial x}\boldsymbol{i}+\frac{\partial}{\partial y}\boldsymbol{j}+\frac{\partial}{\partial z}\boldsymbol{k}\)?
Answer
The field should be \(\boldsymbol{B}=-\alpha x\boldsymbol{i}+(B_0+\alpha z)\boldsymbol{k}\), which satisfies \(\nabla\cdot\boldsymbol{B}\). However, the precession of the spin magnetic moment of a silver atom around \(B_0\) in the Stern-Gerlach experiment is so fast that the \(x\)-component of the spin moment averages to zero, resulting in an effective field of \(\boldsymbol{B}=(B_0+\alpha z)\boldsymbol{k}\) interacting with the atom.

Question
Why is \(\nabla\cdot\boldsymbol{B}=0\)?
Answer
A simple way of showing \(\nabla\cdot\boldsymbol{B}=0\) is to consider a two-dimensional diagrammatic representation of the vector function \(\boldsymbol{B}=-y\boldsymbol{i}+x\boldsymbol{j}\) below, where each point in the two-dimensional space is associated with a vector.

When \(y=0\), the successive points in the negative \(x\) direction and the positive \(x\) direction are associated with the vectors \(-\boldsymbol{j},-2\boldsymbol{j},\cdots\) and \(\boldsymbol{j},2\boldsymbol{j},\cdots\) respectively. Similarly, when \(x=0\), the successive points in the negative \(y\) direction and the positive \(y\) direction are associated with the vectors \(\boldsymbol{i},2\boldsymbol{i},\cdots\) and \(-\boldsymbol{i},-2\boldsymbol{i},\cdots\) respectively. Clearly, \(\nabla\cdot\boldsymbol{B}=0\).
