%SYSREG: MATLAB script M-file with an example of 
%regression for a linear system. 
%
%Define the data
[x1 x2]=meshgrid(linspace(-1,1,5),linspace(-1,1,5))
y1 = [-3.8656   -3.3269   -2.3375   -1.0512    0.1679;
   -2.0415   -1.6084    0.2587    0.4690    1.1981;
   -1.5647    0.0857    1.1814    2.3724    3.1793;
    0.7155    2.3946    2.4842    3.8523    4.9076;
    2.0797    3.6924    4.1787    5.3543    6.1222];
%
y2 = [-17.3183 -3.8368 -15.4453 -8.0703 -10.0780;  
   12.3868    9.6517   15.1628    9.0420   21.7404; 
   30.9294   40.3569   42.7626   37.7885   42.0379;
   56.4325   62.3721   70.5031   61.1921   72.4431;
   86.9658   94.0868   97.7211  103.2523   89.1758];
%
%Define dependent vectors; use [],1 to select a single column
%and any number of rows (for portability)
Y1 = reshape(y1,[],1);
Y2 = reshape(y2,[],1);
%Define columns of ones and columns of zeros
col1 = ones(size(reshape(y1,[],1)));
col0 = zeros(size(reshape(y1,[],1)));
%Use standard deviation as weights
w1=std(Y1);
w2=std(Y2);
%
%Define design matrix
F1=[col1 reshape(x1,[],1) reshape(x2,[],1) col0 col0];
F2=[col0 col0 reshape(x1,[],1) col1 reshape(x2,[],1)];
%
F = [F1/w1;F2/w2]; Y=[Y1/w1;Y2/w2];
p = F\Y
%Standard deviation
q = length(Y)-length(p);
ssr = norm(Y-F*p)^2
s = sqrt(ssr/q);
s1 = s*w1
s2 = s*w2
%Adjusted coefficient of determination
Ybar = mean(Y);
T = norm(Y-Ybar)^2
Rsq = 1-ssr/T
Rsqa = 1-(1-Rsq)*(length(Y)-1)/q








