Regressions in Matlab
Regressions in Matlab
Lia Vas
and
us
The following Matlab code calculates a for a data set (xi, yi ), i=1,2,...,m. Before executing this
code, x-values should be entered as a vector X and y-values should be entered as a vector Y.
X=[x1, x2, ... xm] and Y=[y1, y2, ... ym]. Note that the size of X and Y need to be the same.
The command X(i) returns the i-th coordinate xi .
function a=proportion(X, Y)
sxsq=0;
(sxsq is a variable for sum of squares of x-values)
sxy=0;
(sxy is a variable for sum of products of x and y-values)
m=size(X, 2);
(m is the number of x and y values)
for i=1:m
sxsq=sxsq+X(i)^2;
sxy=sxy+X(i)*Y(i);
end
a=sxy/sxsq;
Example 2. If we need to calculate a linear least-squares fit y=ax+b, for a data set (xi, yi ),
i=1,2,...,m, we are minimizing the function
and