Integrating a function's gradient

1.6k Views Asked by At

Given the gradient of a function, how do you find the function itself?

For a scalar-valued function $f : \mathbb{R}^n \to \mathbb{R}$, the gradient of $f$, denoted $\nabla f : \mathbb{R}^n \to \mathbb{R}^n$, is defined as

$\nabla f(x)_i := \frac{\delta f(x)}{\delta x_i}, \quad i=1,\ldots,n.$

For example, for $A \in \mathbb{R}^{n \times n}$ and $b,x \in \mathbb{R}^n$, the gradient of the function

$f(x) := \frac{1}{2} (Ax-b)^T(Ax-b)$

is

$\nabla f(x) = (Ax-b)^T A.$

Now suppose we are told that the gradient of a scalar-valued function $g$ is

$\nabla g(x) = (Ax-b)^T D$

for some diagonal matrix $D \in \mathbb{R}^{n \times n}$.

Is there a nice closed-form expression for $g(x)$?

These two threads (here, here) seem to use guess-and-check. We tried looking at function of the form

$g(x) := \frac{1}{2} (Ax-b)^T W (Ax-b)$

for various weighting matrices $W$, but no dice.

2

There are 2 best solutions below

2
On

There is a nice closed form expression for $g(x)$ if it exists. Assuming it does, the path integration is quite straightforward. \begin{multline} g(x) = \int\nabla g(x)\cdot d\mathbf{x} = \int (\mathbf{A}\mathbf{x} - \mathbf{b})^T\mathbf{D}d\mathbf{x} = \int \mathbf{x}^T\mathbf{A}^T\mathbf{D}d\mathbf{x} - \mathbf{b}^T\mathbf{D}\int d\mathbf{x} \\?= \frac{1}{2}\mathbf{x}^T\mathbf{A}^T\mathbf{D}\mathbf{x} -\mathbf{b}^T\mathbf{D}\mathbf{x} = \left(\frac{1}{2}\mathbf{A}\mathbf{x} - \mathbf{b}\right)^T\mathbf{D}\mathbf{x} \end{multline}

However, the equality with the ? in front of it is not, in general, true. What condition on $\mathbf{A}$ and $\mathbf{D}$ is needed to make it true?

0
On

This is not an answer... Just to give a simple example why it is not always solvable. Imagine a function $\mathbb R^2 \to \mathbb R$.

Let us prescribe any non-closed (importante!) contour of gradient orthogonal to the direction of the contour with $\nabla g = \|\nabla g\|\hat n$. Now let every other point in the plane have $\nabla g = 0$

What happens at the end points of this contour?

We can walk (integrate) around it along lines consisting of points of gradient 0. Along these lines the function value must not change because both partial differentials are 0. But yet if we walk (integrate) across the contour the function value is prescribed to change.

It becomes an impossible situation.

Example : Consider the function $$x,y \to f = \cases{1, x^2+y^2 < 0.5\\0,x^2+y^2 \geq 0.5}\\ x,y \in \mathbb R^2$$ and its gradient with color coded direction:

Example if we set the negative half-plane (y<0) of the gradient to $0$ and try to solve with a numerical solver, optimizing $$\min_f\|(\nabla f) - g_0\|_2^2$$, where $g_0$ is the truncated gradient field preserved only for positive y:s as explained above :

You can't first walk up a hill and then find some path to walk around the hill getting back to the place of the lower altitude without descending anywhere along the way. If a map says that you can then that map does not describe our world.