Electric potential generated by spherical symmetric charge density I know this question is pretty basic but I found a supposedly wrong formula in my notes and I'm trying to understand where this is coming from. Suppose we have a spherically symmetric charge density $\rho({\boldsymbol{r}})=\rho(r)$, then the formula I was given for the potential is
$$\phi(r)=\frac{1}{r}\int_0^r4\pi\rho(r')r'^2dr'\tag{1}$$
But using Gauss law for electric field one gets
$$\int\boldsymbol{E}\cdot d\boldsymbol{S}=4\pi\underbrace{\int\rho(\boldsymbol{r'})d^3\boldsymbol{r'}}_{Q(r)}\implies \boldsymbol{E}(\boldsymbol{r})=\frac{Q(r)}{r^2}\hat{\boldsymbol{r}}=\frac{\int_0^r4\pi\rho(r')r'^2dr'}{r^2}\hat{\boldsymbol{r}}\tag{2}$$
Taking the gradient of $(1)$
$$\boldsymbol{E}(\boldsymbol{r})=-\nabla\phi=\left[\frac{\int_0^r4\pi\rho(r')r'^2dr'}{r^2}-\frac{4\pi\rho(r)r^2}{r}\right]\hat{\boldsymbol{r}}=\left[\frac{Q(r)}{r^2}-\frac{dQ(r)/dr}{r}\right]\hat{\boldsymbol{r}}$$
That is off by a term from what I got from Gauss Law, so I concluded $(1)$ is wrong.
Is this correct?
 A: The formula
$$\phi(r)= \frac{1}{r}\int_0^r 4\pi\rho(r')r'^2dr' \tag{1}$$
for the potential is indeed wrong,
as you have already proven by checking
$\mathbf{E}(\mathbf{r})=-\nabla\phi$.
It misses the contribution of charges outside of
radius $r$ to the potential $\phi(r)$.
While these outside charges have no effect on the
inside field strength $\mathbf{E}$,
they do have an effect on the inside potential $\phi$.
A correct formula would be
$$\phi(r)= \frac{1}{r}\int_0^r 4\pi\rho(r')r'^2dr'
+\int_r^\infty \frac{1}{r'}4\pi\rho(r')r'^2 dr' \tag{a}$$
Equivalently, you may write this as
$$\phi(r)=
\int_0^\infty \frac{1}{\text{max}(r,r')}4\pi\rho(r')r'^2 dr'$$
It is easy to verify that (a) satisfies
$$\mathbf{E}(\mathbf{r})=-\nabla\phi(r)
=\frac{Q(r)}{r^2}\hat{\mathbf{r}}$$
A: The first formula is true only far away from the source. It is in fact the first term of the multipole expansion.
By using Gauss theorem, you obtain the correct result for the electric field, but if you try to compute it with the gradient, you should use the general expression for the potential of a continuous distribution:
\begin{equation}
\Phi(r)=4\pi \int_{V} \frac{\rho(\textbf{r}')}{|\textbf{r}-\textbf{r}'|} d^{3}r'
\end{equation}
where the primed indices refer to the points inside the volume of the source, the un primed indices to points in which you determine the potential, and V is the total volume of the source. This formula reduces to (1) at first order of its Taylor expansion.
A: The first formula seems to assume that the potential at point r inside the sphere is given only by the charge included in the sphere of radius r. This is true for the electric field but not for the potential, in general. You can see that this is the case taking a constant charge density. According to your formula (1) you will get $V(r) =\frac{4\pi \rho}{r} \frac{r^3}{3}$ or $V(r) =\frac{4\pi \rho}{3} r^2$. The potential inside a uniformly charged sphere goes like $r^2$, as you can see in this link. http://www.phys.uri.edu/gerhard/PHY204/tsl94.pdf but the formula of the potential inside the sphere has two terms. This is true for the convention assigning zero potential to infinity. You can see that the extra term is a constant. So, it may be that your formula (1) may work for a different reference for potential. What is the source of the formula?  Does it have an expression for the field outside the sphere?
A: I haven't checked your computation, however I notice some immediate faults.
You have incorrectly stated Gauss's law. Gauss's law is:
$$\iint \vec{E}(r) \cdot \vec{da} = \frac{Q_{enc}}{\epsilon_{0}}$$
You have also incorrectly stated the integral for the total charge.
The charge enclosed should read $$\int_{0}^{R} 4\pi r'^2 \rho dr'$$
Which is some infinitesimal shell of surface area $4\pi r^2$ thickness dr multiplied by the charge density. Summed up from 0 to R where R is the radius of the sphere, or replaced with r if inside the sphere.
$\int_{0}^{R} \int_{0}^{2\pi}\int_{0}^{\pi} \rho[ r^2 sin(\theta)d\theta d\phi dr]$
$\int_{0}^{R} \int_{0}^{2\pi}2\rho[ r^2 d\phi dr]$
$\int_{0}^{R} 4\pi r^2 \rho[dr]$
