Energy in Electric and Magetic fields I know that the energy in an electric/magetic fields are given by the integrals:
$$
Energy=\iiint (1/2) \epsilon_0 E^2 dV
$$
and 
$$
Energy=\iiint\frac{1}{2\mu_0} B^2 dV
$$
However I don't know why.  Can anyone please show me where this came from.  Thank you.
 A: The answer which I have written works only for electrostatics and magnetostatics but not electrodynamics.

For an electric field, the total energy can be given by:
$$U=\frac{1}{2} \sum_{i,j} \frac{1}{4\pi\epsilon_0}\frac{q_1q_2}{r} = \frac{1}{2} \int_{v}\rho Vd\tau $$

Where $d\tau$ is the volume element, $V$ is the potential and $v$ us the volume we are integrating over. The half is included to remove the repetition. 


Using $\nabla \cdot E= \frac{\rho}{\epsilon_0}$,
$$U=\frac{1}{2 } \int_{v} \epsilon_{0}(\nabla\cdot E)Vd\tau$$
$$= -\frac{1}{2}\epsilon_{0}\int_{v}E \cdot \nabla V d\tau + \frac{1}{2}\epsilon_{0}\int_{v}\nabla\cdot (EV) d\tau$$
$$= \frac{1}{2}\epsilon_{0}\int_{v}E^2 d\tau + \frac{1}{2} \epsilon_{0} \oint_{a} EV \cdot da$$
Where in the last step we used the divergence theorem.

Suppose we keep on increasing the volume over which we are integrating over. The $1^{st}$ term increases since it is positive. But since the sum is a constant, the $2^{nd}$ term will decrease. Thus if we are integrating over all space, the second term must go to zero. 
$$U = \frac{1}{2}\epsilon_{0}\int_{Space}E^2d\tau$$
Thus, the energy density per unit volume in electric field is $\frac{1}{2}\epsilon_0 E^2$


Now I shall derive the expression for the energy in a magnetic field.

The energy in a magnetic field can be easily shown to be $\frac{1}{2}Li^2$, where $L$ is the inductance.

Now, $Li=\phi$ where $\phi$ is the magnetic flux through a surface which is bounded by a current loop. It is given by:
$$\phi= \int_{S} B\cdot da = \int_{S} (\nabla \times A ) \cdot da = \oint_{C} A\cdot dl$$

Where we used stokes theorem in the last step.

Thus,
$$Li= \oint_{C} A\cdot dl$$
$$\Longrightarrow U=\frac{1}{2}i\oint_{C} A\cdot dl$$
Since $dl$ is along the current loop, we can instead take the dot product with the current.
$$U = \frac{1}{2}\oint_{C}( A\cdot i)dl$$
Since $i=\int_{S} J\cdot da$ , we can now write:
$$U= \frac{1}{2}\int_{v}( A\cdot J)d\tau $$
Using $\nabla \times B = \mu_0 J$,
$$U= \frac{1}{2\mu_0}\int_{v} A\cdot (\nabla \times B)d\tau$$
$$= \frac{1}{2\mu_0} \int_{v} B^2 - \frac{1}{2\mu_0} \int_{v} \nabla \cdot (A\times B) d\tau$$
$$= \frac{1}{2\mu_0} \int_{v} B^2 - \frac{1}{2\mu_0} \int_{s} (A\times B) \cdot da$$
Using the same argument as before, we can say that:
$$U = \frac{1}{2\mu_0} \int_{space} B^2$$
Thus, the energy density per unit volume in an magnetic field is $ \frac{1}{2\mu_0}B^2$
A: I've had the exact same question before myself.  The following is pretty technical, but I think it's the best answer to your question.
From Noether's Theorem, $E = \int dV T^{00}$ is the conserved energy, where $T^{\mu\nu}$ is the energy-momentum tensor.  The best way to compute a symmetric, gauge invariant energy momentum tensor is by varying the Lagrangian with respect to the metric.  See, for example, Eq. 4.75 on p. 164 of Sean Carroll's "Spacetime and Geometry" book.  From the Lagrangian of the electromagnetic field $\mathcal{L}=-\frac{1}{4}F_{\mu\nu}F^{\mu\nu}$ we may find the energy momentum tensor is $$T_{\mu\nu} = F_{\mu\lambda}F^{\lambda}_{\phantom{\lambda}\nu}+\frac{1}{4}g_{\mu\nu}F_{\alpha\beta}F^{\alpha\beta}.$$ Using $E^i=-F^{0i}$ and $F^{ij}=\epsilon^{ijk}B_k$ one can show that (in natural units) $$T^{00}=\frac{1}{2}(\vec{E}^2+\vec{B}^2).$$
A: One nice way to go is to prove uniqueness for these expressions, and then show that the energy defined in this way is in fact conserved.
The first thing to realize is that energy is a scalar, and the fields are vectors, so any equation for the energy density of the fields must only depend on the scalar products $E\cdot E$, $B\cdot B$, and $E \cdot B$. The $E \cdot B$ possibility doesn't work because it doesn't have the right symmetry properties. For example, if you put an electric charge at rest in the field of a solenoid, the energy shouldn't change if you then reverse the direction of the current in the solenoid.
Next, we can use the fact that Maxwell's equations have symmetry between electricity and magnetism. Physically, this symmetry isn't evident in everyday life for a couple of reasons. (1) Our universe doesn't seem to have magnetic monopoles, or if they exist, they seem to be rare. (2) If you're using SI units, then the electric and magnetic fields have different units. Based on this symmetry, the energy density must be of the form $\rho=f(|E|)+f(|B|)$, where we assume a system of units such that the electric and magnetic fields have the same units, and Maxwell's equations show the symmetry manifestly.
The function $f$ has to be a smooth function of the components of the fields, so it needs to be even, and its Taylor series can only have even-order terms. Furthermore, we can't have a mixture of terms that are of different orders, because we don't have any unitful constants available in Maxwell's equations that would allow us to match the units of the terms appropriately. This means that we must have $f(x)=\alpha x^k$ for some constant $\alpha$ and some even, positive integer $k$. The fact that $k=2$ is easily fixed by considering any example where we do work on a charge and the energy in the fields is easy to calculate. For example, you can find the work done by changing the distance between the plates of a parallel-plate capacitor. Such an example also fixes the constant $\alpha$ to be $1/8\pi k$, where $k$ is Coulomb's constant. (In SI units, you need an additional $1/c^2$ on the magnetic term.)
All of this proves that if there is a conserved energy, it has to have the given form. We now need to prove that this form is actually conserved, based on Maxwell's equations. You can approach this by taking the divergence of the energy density, and using Maxwell's equations to show that it obeys a continuity equation.
