How to get gradient of formula?

34 Views Asked by At

I have question :

suppose that $x_i , y \in \mathbb{R^n}$:

$C(y) = \sum_{i =1}^{n} || y - x_i|| $

then how I can get $\nabla C = - \sum_{i = 1}^n \frac{x_i - y}{|| y - x_i||}$ .

1

There are 1 best solutions below

0
On BEST ANSWER

Writing $x_i\in \mathbb{R}^{\mathbb{n}}$ as $(x_{i,1},\ldots,x_{i,\mathbb{n}})$, we have that $$ y-x_i=(y_1-x_{i,1},\ldots,y_\mathbb{n}-x_{i,\mathbb{n}}), $$ whence $$ \|y-x_i\|=\sqrt{(y_1-x_{i,1})^2+\ldots+(y_\mathbb{n}-x_{i,\mathbb{n}})^2}. $$ Now compute $$ \frac{\partial }{\partial y_j} \|y-x_i\| = \frac{y_j-x_{i,j}}{\|y-x_i\|}, $$ or $$ \frac{\partial }{\partial y_j} \sum_{i=1}^n\|y-x_i\|=\sum_{i=1}^n\frac{y_j-x_{i,j}}{\|y-x_i\|}. $$ Thus $$ \nabla C=\left(\sum_{i=1}^n\frac{y_1-x_{i,1}}{\|y-x_i\|},\ldots,\sum_{i=1}^n\frac{y_\mathbb{n}-x_{i,\mathbb{n}}}{\|y-x_i\|}\right)=\sum_{i=1}^n\frac{y-x_{i}}{\|y-x_i\|}=-\sum_{i=1}^n\frac{x_{i}-y}{\|y-x_i\|}. $$