Nabla/del of $(y_i-\hat y(\mathbf x,t_i))^2$? I want to take nabla, w.r.t. $\mathbf x$, of the function 
$$
f(\mathbf x)=\sum_{i=1}^m(y_i-\hat y(\mathbf x,t_i))^2 \tag 1
$$
Is the following correct:
\begin{align}
\nabla f(\mathbf x)
&=
\nabla\bigg(\sum_{i=1}^m(y_i-\hat y(\mathbf x,t_i))^2\bigg)\\
&=
\nabla\sum_{i=1}^m(y_i-\hat y(\mathbf x,t_i))(y_i-\hat y(\mathbf x,t_i))\\
&=
\sum_{i=1}^m(y_i-\hat y(\mathbf x,t_i))\nabla(y_i-\hat y(\mathbf x,t_i))\\
&=
-\sum_{i=1}^m(y_i-\hat y(\mathbf x,t_i))\nabla\hat y(\mathbf x,t_i))\tag 2
\\
&\dots \text{Multiply?}
\end{align}
Or must I expand the parenthesis first:
\begin{align}
\nabla f(\mathbf x)
&=
\nabla\bigg(\sum_{i=1}^m
(y_i-\hat y(\mathbf x,t_i))^2
\bigg)\\
&=
\nabla\sum_{i=1}^m
(
y_i^2-y_i\hat y(\mathbf x,t_i)-\hat y(\mathbf x,t_i)y_i +\hat y(\mathbf x,t_i)^2
)\\
&=
\nabla\sum_{i=1}^m
(
y_i^2-2y_i\hat y(\mathbf x,t_i) +\hat y(\mathbf x,t_i)^2
)\\
&=
\sum_{i=1}^m
(
-2y_i\nabla\hat y(\mathbf x,t_i) +\nabla\hat y(\mathbf x,t_i)^2
) \tag 3\\
&\dots\text{Stuck here}
\end{align}

Both are the same,
\begin{align}
\nabla f(\mathbf x)
&=
\nabla\bigg(\sum_{i=1}^m(y_i-\hat y(\mathbf x,t_i))^2\bigg)\\
&=
\color{blue}{2}\nabla\sum_{i=1}^m(y_i-\hat y(\mathbf x,t_i))(y_i-\hat y(\mathbf x,t_i))\\
&=
\color{blue}{2}\sum_{i=1}^m(y_i-\hat y(\mathbf x,t_i))\nabla(y_i-\hat y(\mathbf x,t_i))\\
&=
-\color{red}{2\sum_{i=1}^m(y_i-\hat y(\mathbf x,t_i))\nabla\hat y(\mathbf x,t_i))}\tag 1
\end{align}
If you expand instead
\begin{align}
\nabla f(\mathbf x)
&=
\nabla\bigg(\sum_{i=1}^m
(y_i-\hat y(\mathbf x,t_i))^2
\bigg)\\
&=
\nabla\sum_{i=1}^m
(
y_i^2-y_i\hat y(\mathbf x,t_i)-\hat y(\mathbf x,t_i)y_i +\hat y(\mathbf x,t_i)^2
)\\
&=
\nabla\sum_{i=1}^m
(
y_i^2-2y_i\hat y(\mathbf x,t_i) +\hat y(\mathbf x,t_i)^2
)\\
&=
\sum_{i=1}^m
(
-2y_i\nabla\hat y(\mathbf x,t_i) +\nabla\hat y(\mathbf x,t_i)^2
) \\
&= 
\sum_{i=1}^m
(
-2y_i\nabla\hat y(\mathbf x,t_i) +2 \hat y(\mathbf x,t_i)\nabla\hat y(\mathbf x,t_i)
) \\
&=
\color{red}{-2\sum_{i=1}^m(y_i-\hat y(\mathbf x,t_i))\nabla\hat y(\mathbf x,t_i))}\tag 2
\end{align}
$(1) \equiv (2)$
