4

If I calculate the Euclidean distance $d$ of two sets of $x,y$ points $A_{1} \in \mathbb{R}^{n\times2}$ and $B_{2} \in \mathbb{R}^{n\times2}$ as

$d = \sqrt{(A_{1}-B_{1})^2 + (A_{2}-B_{2})^2}$

How would I calculate the gradient of $d$?

Based on THIS answer I interpreted it as the gradient would be:

$\nabla d =\frac{A-B}{\sqrt{(A_{1}-B_{1})^2 + (A_{2}-B_{2})^2}}$

However, this doesn't seem to give me the results I was expecting.

1 Answers1

2

The most straightforward way to arrive at the expression for the gradient of a Eucledian distance is the following:

Consider a point $\overrightarrow{x}$ with coordinates $(x,y)$, and another point $\overrightarrow{x_C}$ with coordinates $(x_C,y_C)$, then $d$ = $||\overrightarrow{x}-\overrightarrow{x_C}||$ can be expressed as:

$d=\sqrt{(x-x_C)^2+(y-y_c)^2}$

$\frac{\partial d}{\partial x}=\frac{2(x-x_C)}{2\sqrt{(x-x_C)^2+(y-y_c)^2}}=\frac{x-x_C}{\sqrt{(x-x_C)^2+(y-y_c)^2}}$

Similarly, for $\frac{\partial d}{\partial y}$

$\frac{\partial d}{\partial y}=\frac{(y-y_C)}{\sqrt{(x-x_C)^2+(y-y_c)^2}}$

which leads to,

$\begin{bmatrix}\frac{\partial d}{\partial x} \\ \frac{\partial d}{\partial y}\end{bmatrix}= \begin{bmatrix}\frac{(x-x_C)}{\sqrt{(x-x_C)^2+(y-y_c)^2}}\\ \frac{(y-y_C)}{\sqrt{(x-x_C)^2+(y-y_c)^2}}\end{bmatrix}$

Therefore,

$\boxed{\nabla d =\frac{\overrightarrow{x}-\overrightarrow{x_C}}{||\overrightarrow{x}-\overrightarrow{x_C}||}}$

Hope this answers your question and clarifies any confusion.