# Minimizing mean squared error

I want to find a $d$ that minimizes the value of the expression below. I think the first step is to find the derivative w.r.t. $d$ (is that correct? If not, what is the first step?). If so, I'm having trouble finding that derivative given this form (because of the summation and the function $I_l$ and $I_r$ (which just means, the image at that point)). Can someone please explain where to even start with finding the derivative.

My ultimate goal would be to use MATLAB to find the value of $d$ that would minimize this but even any ideas on how to start would be very useful.

$$\frac{1}{(2W+1)^2}\sum_{-W\le j\le W}\sum_{-W\le i\le W}\large{\left(I_l(y+j, x+i)-I_r(y+j,x+i+d)\right)^2}$$

-
Differentiate w.r.t $d$ and set the resulting expression equal to $0$. (It will involve the derivative $\partial I_r / \partial d$, which we cannot compute without an explicit expression for $I_r$.) –  Sammy Black Mar 21 '13 at 21:57
So is there any way to do this without an explicit expression for I_r? I_r just represents the value of a known image at point i,j –  user1136342 Mar 21 '13 at 22:05

\begin{align} & \hspace{-2em}\frac{\partial}{\partial d} \left[ \frac{1}{(2W + 1)^2} \sum_{\substack{-W \le i \le W \\ -W \le j \le W}} \big( I_l(y + j, x + i) - I_r(y + j, x + i + d) \big)^2 \right] \\ &= \frac{1}{(2W + 1)^2} \sum_{\substack{-W \le i \le W \\ -W \le j \le W}} \frac{\partial}{\partial d} \left[ \big( I_l(y + j, x + i) - I_r(y + j, x + i + d) \big)^2 \right] \\ &= \frac{1}{(2W + 1)^2} \sum_{\substack{-W \le i \le W \\ -W \le j \le W}} -2 \big( I_l(y + j, x + i) - I_r(y + j, x + i + d) \big) \cdot \frac{\partial I_r}{\partial d} \end{align}
Critical points occur where this last expression is $0$ (or fails to exist because of an asymptote), but you need to know $I_r$.