Physics with Calculus/Electromagnetism/Field Energy

Taken from: https://en.wikipedia.org/wiki/Electrostatics#Electrostatic_energy

A single test particle's potential energy, $$ U_\mathrm{E}^{\text{single}}$$, can be calculated from a line integral of the work, $$q_n\vec E\cdot\mathrm d\vec\ell $$. We integrate from a point at infinity, and assume a collection of $$N$$ particles of charge $$Q_n$$, are already situated at the points $$\vec r_i$$. This potential energy (in Joules) is:


 * $$ U_\mathrm{E}^{\text{single}}=q\phi(\vec r)=\frac{q }{4\pi \varepsilon_0}\sum_{i=1}^N \frac{Q_i}{\left \|\mathfrak\vec r_i \right \|} $$

where $$ \vec\mathfrak r_i =  \vec r - \vec r_i ,$$ is the distance of each charge $$Q_i$$ from the test charge $$q$$, which situated at the point $$\vec r $$, and $$ \phi(\vec r)$$  is the electric potential that would be at $$\vec r $$ if the test charge were not present. If only two charges are present, the potential energy is $$k_eQ_1Q_2/r$$. The total electric potential energy due a collection of N charges is calculating by assembling these particles one at a time:


 * $$U_\mathrm{E}^{\text{total}} = \frac{1 }{4\pi \varepsilon _0}\sum_{j=1}^N Q_j \sum_{i=1}^{j-1} \frac{Q_i}{r_{ij}}= \frac{1}{2}\sum_{i=1}^N Q_i\phi_i ,$$

where the following sum from, j = 1 to N, excludes i = j:


 * $$\phi_i = \frac{1}{4\pi \varepsilon _0}\sum_{j=1 (j\ne i)}^N \frac{Q_j}{4\pi \varepsilon _0 r_{ij}}. $$

This electric potential, $$\phi_i$$ is what would be measured at $$\vec r_i$$ if the charge $$Q_i$$ were missing. This formula obviously excludes the (infinite) energy that would be required to assemble each point charge from a disperse cloud of charge. The sum over charges can be converted into an integral over charge density using the prescription $$\sum (\cdots) \rightarrow \int(\cdots)\rho\mathrm d^3r$$:


 * $$U_\mathrm{E}^{\text{total}} = \frac{1}{2} \int\rho(\vec{r})\phi(\vec{r}) \operatorname{d}^3 r = \frac{\varepsilon_0 }{2} \int \left|{\mathbf{E}}\right|^2 \operatorname{d}^3 r$$,

This second expression for electrostatic energy uses the fact that the electric field is the negative gradient of the electric potential, as well as vector calculus identities in a way that resembles integration by parts. These two integrals for electric field energy seem to indicate two mutually exclusive formulas for electrostatic energy density, namely $$\frac{1}{2}\rho\phi$$ and $$\frac{\varepsilon_0 }{2}E^2$$; they yield equal values for the total electrostatic energy only if both are integrated over all space.