# Where does the energy $U(\theta)=-\vec M\cdot \vec B$ associated with a current carrying loop in a magnetic field come from?

I was taught that in a magnetic field $$\vec B$$, a current carrying loop with a magnetic moment $$\vec M$$, the loop experiences a torque given by $$\tau=\vec M\times\vec B$$. Further, the associated energy is given by (as in the electrical case by integrating $$\tau d\theta$$) $$U(\theta)=-\vec M\cdot \vec B$$. Now I had learnt that magnetic field cannot do any work (force and velocity are always perpendicular).

So,how are we associating an energy here? Where does this energy $$U(\theta)$$ come from? I can't account for it.

• "Now I had learnt that magnetic field cannot do any work (force and velocity are always perpendicular)." - Take a look at the answer here: "The Lorentz force $\textbf{F}=q\textbf{v}\times\textbf{B}$ never does work on the particle with charge $q$. This is not the same thing as saying that the magnetic field never does work." Oct 9, 2018 at 15:18

@Hal Hollis mentioned correctly in the above comment that $$B$$ do non zero work on systems, like in this question.
The given potential energy expression is misleading in interpretation and it is derived from the work done by $$B$$ as follows: $$W_B=\int_{\theta_i}^ {\theta_f} \tau d\theta = \int_{\theta_i}^ {\theta_f}MBsin(\theta)d\theta =-MB[cos(\theta_f) -cos(\theta_i)]$$ The general expression for potential energy change will be $$\Delta U=-W_B=MB[cos(\theta_f) -cos(\theta_i)]$$
Setting reference: zero of potential energy as(when torque is maximum) $$\theta_f =\pi/2$$ and putting $$\theta_i =\theta$$, we get $$U(\theta)=-MBcos(\theta)=-\vec{M}.\vec{B}$$ So the energy $$U(\theta)$$ is nothing but the change in total potential energy of the system with respect to position $$\pi/2$$ and it is not an absolute energy.