I'm trying to solve this problem: Let $p_j=(x_j,y_j)\in \mathbb{R}^2$ $(j=1,\ldots,n)$ with $p_i\neq p_j$ for $i\neq j$. Find the function of de form $f(x)=ax+b$ such that
$$\sum_{j=1}^{n} (f(x_j)-y_j)^2=\sum_{j=1}^{n}(ax_j+b-y_j)^2=F(a,b)$$
is a minimum.
We are trying the theme of Lagrange multipliers. I considered the function $F\colon \mathbb{R}^2 \to \mathbb{R}$ given by
$$F(l,m)=\sum_{j=1}^{n}(lx_j+m-y_j)^2$$
for which we want to find a minimum. However I don't know the function I can use like a constraint to apply Lagrange multipliers. Also I obtained the gradient of $F$ and equaling it to $0$ I found $m$ in terms of the points $(x_j,y_j)$ but to obtain $l$ I have a factor that accompanies $l$ and may be it could be 0 in some case and is not possible to divide by it. I don't know if equaling the gradient to (0,0) is the correct way to solve this. Could you give me sone suggestions for this problem? Which is the constraint function in this case? Thanks.
Using gradient of $F$ : $$\frac{\partial F}{\partial a} = 2\sum_{j=1}^n x_j(ax_j+b-y_j)$$ $$\frac{\partial F}{\partial b} = 2\sum_{j=1}^n (ax_j+b-y_j)$$ $\frac{\partial F}{\partial b}=0$ means $$a\sum_j x_j + nb-\sum_jy_j=0\iff b=\bar y-a\bar x$$ where $\bar x=\frac1n\sum_j x_j$ is the mean value of the $x$ values$.
Now $\frac{\partial F}{\partial b}=0$ gives you $$a\sum_j x_j^2+n\bar x\bar y-na\bar x^2-\sum_j x_jy_j=0 \iff a\left(\frac1n\sum x_j^2-\bar x^2\right) = \frac1n\sum_jx_jy_j-\bar x\bar y$$ so $$a=\frac{{\rm Cov}(X,Y)}{{\rm Var}(X)}$$ as expected.