Please help me to solve the equation with Ordinary Least Squares (OLS) method.
Given two same length vectors:
$x=(x_{1} , ... , x_{n})$
$y=(y_{1} , ... , y_{n})$
1) Find coefficients a,b,c for:
$y_{i} = ax_{i}^2 + bx_{i} + c$
such that: $$\int_0^1 (ax^2+bx+c) \,dx= 0$$
2) Solve it with Matlab
I'll be very grateful for your help!
My way:
I tried something like this:
$ \left\{ \begin{aligned} ax_{1}^2+bx_{1}+c &= y_{1} \\ ...\\ ax_{n}^2+bx_{n}+c &= y_{n} \end{aligned}\right. $
After that:
$$ \left[ \begin{array}{} x_{1}^2&x_{1}&1\\ x_{2}^2&x_{2}&1\\...\\ x_{n}^2&x_{n}&1\\ \end{array}\right]\left[ \begin{array}{} a\\ b\\c\\ \end{array} \right] = \left[ \begin{array}{} y_{1}\\ y_{2}\\...\\y_{n} \end{array} \right]$$
In Matlab:
M= [x.^2 x ones(length(x),1)];}
U = M\Y or U = pinv(M)*Y;
And after that:
U(1) = a;
U(2) = b;
U(3) = c;
But I don't understand why I need an Integral?
You can follow David's suggestion. As K. Miller mentioned, the integral gives you a constraint, or a relationship between the three coefficients. You can find
$$c=-\frac{1}{3}a-\frac{1}{2}b$$ and plug this $c$ into your fitting equation.
This gives you $$y_i=ax_i^2+bx_i-\frac{1}{3}a-\frac{1}{2}b\\ =a(x_i^2-\frac{1}{3})+b(x_i-\frac{1}{2})$$
Now using $x_i^2-\frac{1}{3}$ and $x_i-\frac{1}{2}$ as values in your matrix, you can find the coefficients $a$ and $b$. $c$ is then determined by the first equation obtained by that integral.