Ordinary Least Squares (OLS) + Matlab

1k Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

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.

2
On

Based on what you wrote it seems to me that the integral equation is imposing a constraint on your least squares solution. In order to satisfy this constraint the coefficients $a, b, c$ must satisfy the equation

$$ 2a + 3b + 6c = 0, $$

that is, solutions must lie on the plane through the origin with normal vector $(2,3,6)$. Your Matlab solution is correct in the unconstrained case, but it doesn't work in the constrained case since it finds the best solution over all of $\mathbb{R}^3$. I am not sure what Matlab toolboxes you have access to, but you may want to look at lsqlin, which solves constrained linear least-squares problems.