# Matlab: least square method

Specify a linear function in terms of the least squares method approximates the set point table. Then calculate the sum of squares deviations of this linear function in given points.

xi=[1.25 2.34 4.15 5.12 6.05 6.45]; yi=[-1.3 -1.6 -2.3 -3.1 -3.8 -4.1];

I assume that the required polynomial is second-degree, and the answer is: P = -0.5467x - 0.3894

How to format following equation in Matlab?

sum = $\sum_{i=0}^{n}[p(x_{i})-y_{i}]^2$

-

symbolic toolbox is not the usual way to do least square method in MATLAB, the most used function is polyﬁt and polyval, in which polyﬁt will return the coefficients set $\{a_k\}$ in the following fitting polynomial: $$p_n(x) = a_n x^n + a_{n-1} x^{n-1} + \cdots + a_0$$ simply type in

a = polyfit(x,y,1); % fitting polynomial has degree 1


and you will find that a = [-0.54668 -0.38939] which coincides with what you give.

If you use second degree a = polyfit(x,y,2);, a will be [-0.074948 0.033439 -1.234].

For the second question, to evaluate $\displaystyle\sum\limits_{i=0}^{n}[p(x_{i})-y_{i}]^2$, say you have two $(n+1)$-array xi and yi, then the most vectorized command to compute this explicitly is, supposedly you have your p give as above:

p = @(x)-0.5467*x-0.3894
S = sum((p(xi)-yi).^2,2)


noted the dot before the exponential hat, it is for the vectorized arithmetic operation in MATLAB. Or simply use the built-in Euclidean norm function norm which returns the $l^2$-norm of a sequence:

S = norm(p(xi)-yi);
S = S^2;


will give you the same result.

-
That part of task isn't inquiring, i already attached answer in my question, but the problem i have is related to second part, with calculating a sum of squared deviations. Thanks anyway ;) –  sundancer Aug 28 '12 at 20:09
@sundancer please refer to my edit. –  Shuhao Cao Aug 28 '12 at 20:18
correct answer for the given exam is 0.9526, but i got 0.2371, so i think there is an error in calculation. P.S. this isn't two questions, it's one question with two parts (sum part is directly related to the first part) –  sundancer Aug 28 '12 at 20:30

As @Shuhao mentioned I suggest polyfit. You can use plot($x$,$y$) after you define your $x$ and $y$ vectors. Then in the plot, you will have options $\rightarrow$ tools. There you will see basic fit. There you can choose any degree of the polinomial fit and see how it fits. You have also the norm of residues etc..

So to calculte the squared error by yourself in MATLAB. Let your model be $Y=aX+b$ and you have a vector $x$ in MATLAB. what you do is to type Y=a.*x+b. Now you have $Y$. to find the absolute squared error you have sum(abs(Y-y).^2).

I hope it helps..

-
As I already replied to @Shuhao, i need a help with calculating with sum of squared deviations, but not with the determining of linear function with polyfit (that is ridiculously simple). –  sundancer Aug 28 '12 at 20:18
@sundancer it is also simple. polyfit gives you norm of residues as well I think it should be square root of what you are looking for. –  Seyhmus Güngören Aug 28 '12 at 20:22
@sundancer please see the edit. –  Seyhmus Güngören Aug 28 '12 at 20:26

try this:
[p,pint,r] = regress(yi',[ones(6,1) xi'])

p is the coefficients, pint is the 95% confidence interval of the coefficient estimates, and r is the residuals you're looking for.

-
ps. you can calculate the sum with r'*r –  karakfa Aug 28 '12 at 20:26