TITLE: Matrix derivative $(Ax-b)^T(Ax-b)$
QUESTION [14 upvotes]: I am trying to find the minimum of $(Ax-b)^T(Ax-b)$ but I am not sure whether I am taking the derivative of this expression properly.
What I did is the following:
\begin{align*}
\frac{\delta}{\delta x_i}\left(\sum_i \sum_j (A_{ij}x_i-b_i)(A_{ij}x_j-b_j)\right)&= \sum_j A_{ij}(A_{ij}x_j-b_j) + \sum_i A_{ij}(A_{ij}x_j-b_i) 
\end{align*}
but I'm not quite sure if this is correct and what the derivate would then be. Any help is appreciated.

REPLY [2 votes]: Using the identities: 
$\frac{\partial x^{T} Bx}{\partial x} = (B+B^{T} )x  $
$\frac{\partial x ^{T} a}{\partial x} = \frac{\partial x a ^{T}}{\partial x} = a$
$\frac{\partial a ^{T} x b}{\partial x} = a b ^{T}$
$\frac{\partial a ^{T} x ^{T} b}{\partial x} = ba ^{T}$
We have
$f(x) = (Ax-b)^{T}(Ax-b) $
$= x^{T} A^{T} Ax - x^{T} A^{T} b -b ^{T} Ax -b^{T} b$ 
Using above identities, we have
${f}'(x) = (A^{T} A+(A^{T} A)^{T})x - A^{T} b -(b^{T} A)^{T} $
$ = 2A^{T} Ax - 2A^{T} b$