Module 2 : Electrostatics
Lecture 9 : Potential Energy of a System of Charges
(In case of a line charge or a surface charge distribution, the integration is over the appropriate dimension).
Since the integral is over the charge distribution, it may be extended over all space by defining the charge density to be zero outside the distribution, so that the contribution to the integral comes only from the region of space where the charge density is non-zero. Writing
 
\begin{displaymath}W = \int_{\rm { all space}}\rho(\vec r)\phi(\vec r) d\tau\end{displaymath}
  From the differential form of Gauss's law, we have
 
\begin{displaymath}\vec\nabla\cdot\vec E = \frac{\rho}{\epsilon_0}\end{displaymath}
  With this
 
\begin{displaymath}W = \frac{\epsilon_0}{2}\int_{\rm all\ space}(\vec\nabla\cdot\vec E)\phi d\tau\end{displaymath}
   
14