Given an function $f:\Bbb R^n \rightarrow \Bbb R$, is it always possible to find a non vanishing vector field $V:\Bbb R^n \rightarrow \Bbb R^n$ such that f is a first integral of the vector field? If not, when is it possible?
I read from this question that for every differentiable vector field, there exists a function that is constant along the trajectories of the field and such that it's differential is not zero.
My question is in the oposite way, so I would like to know if there is a result that guarantees the existence of such vector field, and under which conditions at the function f (maybe f needs to be analytic).
Notice that $f$ is constant along the trajectories of $V$ if and only if $V$ is tangent to the level sets $f^{-1}(c)$, $c \in \Bbb R$.
This observation implies that if $f$ has an isolated extremum at $p$, then any vector field $V$ which has $f$ as a first integral satisfies $V_p = 0$, and in particular there is no nonvanishing vector field $V$ for which $f$ is a first integral.
A nonvanishing vector field can fail to exist for other reasons, too. For example, suppose that $f: (x, y, z) \mapsto x^2 + y^2 + z^2$ is the first integral of a vector field $V$ on $\Bbb R^3$. Since $V$ is tangent to the sphere $S^2(R) := f^{-1}(R^2)$, $\smash{V\vert_{S^2(R)}}$ is a vector field on $S^2(R)$, so the Hairy Ball Theorem implies that it, and hence $V$, vanishes at some point.
A common generalization of the above $2$ examples is a corollary of the Poincaré–Hopf Theorem: If for some $c$ the level set $M := f^{-1}(c)$ is a connected, closed manifold of Euler characteristic not $0$, then there is no nonvanishing vector field on $M$, hence no nonvanishing vector field on $\Bbb R^n$ along whose trajectories $f$ is constant.
One situation in which existence can be guaranteed is the following: For any differentiable function $f : \Bbb R^2 \to \Bbb R$, the conjugate $V := (\operatorname{grad} f)^* = (f_x, f_y)^* = (-f_y, f_x)$ of the gradient vector field satisfies $$V \cdot f = (\operatorname{grad} f)^* \cdot \operatorname{grad} f = (-f_y, f_x) \cdot (f_x, f_y) = 0 .$$ So, $f$ is a first integral of $V$, and $V$ vanishes exactly at the critical points of $f$. In particular, if $f$ has no critical points, $f$ is a first integral of a vector field $V$ with no critical points. This construction can be generalized to $\Bbb R^{2 m}$.