How to derive the function from its gradient? Let $a$ be a vector and $a= \nabla\phi$ where $\phi$ is a scalar. How to find $\phi$ when $a$ is given?
My approach: $a_1dx=a_2dy=a_3dz=d\phi$
So, $\phi=\int a_1dx=\int a_2dy=\int a_3dz$
but the terms $\int a_1dx,\int a_2dy,\int a_3dz$ are coming as different. 
 A: Not all vector functions can be written as the gradient of some scalar function.  For a vector $V=(M,N,P)$, where $M,N,P$ are scalar functions, to be written as the gradient of a scalar function, we must have the condition $\nabla\times V=0$.  In Cartesian coordinates, this means that:
$$\frac{\partial M}{\partial y}=\frac{\partial N}{\partial x} $$
$$\frac{\partial M}{\partial z}=\frac{\partial P}{\partial x} $$
$$\frac{\partial N}{\partial z}=\frac{\partial P}{\partial y} $$
If these conditions are met (assuming the components are differentiable), then $V=\nabla \phi$  .  The usual way to proceed is through integration:
$$\phi=\int M~dx=f(x,y,z)+h(y,z) $$
Where $h$ comes from the partial integration.
Then, a good way to find $h$ is to take the partial derivative of $\phi$ w.r.t. $y$
$$\frac{\partial \phi}{\partial y}=\frac{\partial f}{\partial y}+\frac{\partial h}{\partial y}=N $$
Usually, the $f$ will cancel with some terms in $N$, and you can integrate again to find $h$ with a new integration "factor" $g(z)$.  You can then go through the same process of finding $h$ to find $g$ and you are done.  The thing you are missing are these integration factors.  They must be there as you are a looking for a function of several variables while you are integrating w.r.t. only one.
