# Numerical computation of electric multipoles

I'm trying to do a plot of the first multipole terms in Mathematica. My plot isn't what I expected, so maybe my problem is in the mathematics.

Following the Griffiths' book on Electrodynamics, the angle used to describe the poles is the angle between the element of charge and the point we are calculating the field, I will call it $\psi$ and that angle depends on the angles of both vectors relative to the reference frame:

https://math.stackexchange.com/questions/231221/great-arc-distance-between-two-points-on-a-unit-sphere

My question is if I have a charge distribution, on which variables will it depend? I used $r '$, $\psi$ and $\phi '$, where the ' coordinates are the coordinates of the vector conecting the origin to the elements of charge.

Hope I was clear. Sorry for english mistakes.

If you want to describe some arbitrary charge distribution $\rho(\vec r')$, then you need to calculate the electrostatic potential as $$V(\vec r)=\int\frac{\rho(\vec r')\mathrm d\vec r'}{||\vec r-\vec r'||} \tag 1$$ as usual. Putting in the explicit dependences, you need to calculate $$V(\vec r)=\int \frac{\rho(r',\theta',\phi') r'² \sin(\theta')\mathrm dr'\mathrm d\theta'\mathrm d\phi'}{\sqrt{r^2+r'^2-2rr'\cos(\psi(\theta,\theta',\phi,\phi'))}}, \tag 2$$ where $\psi(\theta,\theta',\phi,\phi')=\arccos\left(\cos\theta\cos\theta'+\sin\theta\sin\theta'\cos\left(\phi-\phi'\right)\right)$ is the angle between $\vec r$ and $\vec r'$ (as described in the math.se post you linked to).
If you want, you can then do a multipolar expansion on the inverse-square-root Coulomb kernel (in essence its Taylor expansion in $r/r'$), and that will give you the multipolar series for $V(\vec r)$ in terms of the multipolar moments of $\rho(\vec r')$.
To be a bit more explicit, consider the multipolar series for the kernel up to the dipolar term, $$\frac{1}{\sqrt{r^2+r'^2-2rr'\cos(\psi(\theta,\theta',\phi,\phi'))}} =\frac{1}{r} +\frac{r'}{r^2}\left(\cos\theta\cos\theta'+\sin\theta\sin\theta'\cos\left(\phi-\phi'\right)\right),$$ and substitute it into the integral in $(2)$. You should be able to reduce it into the form $$V(\vec r)=\frac{Q}{r}+\frac{\vec r\cdot\vec d}{r^3},$$ where $$Q=\int \rho(r',\theta',\phi') r'² \sin(\theta')\mathrm dr'\mathrm d\theta'\mathrm d\phi'$$ is the total charge, and $$\vec d=\int (r'\sin\theta'\cos\phi'\hat{x}+r'\sin\theta'\sin\phi'\hat{y}+r'\cos\theta'\hat{z})\rho(r',\theta',\phi') r'² \sin(\theta')\mathrm dr'\mathrm d\theta'\mathrm d\phi'$$ is the dipole moment of $\rho(\vec r)$. If you want to go beyond this into quadrupole terms - well, you know what to do.