% Exa = 1;

switch Exa
case 1
%%%%% straight line regression
x = [0; 1; 2; 3.5; 4]
y = [-0.5; 1; 2.4; 2.0; 3.1]

F = [ones(size(x)) x]
p = (F'*F)\(F'*y)
residual = norm(F*p-y)

xn = [-1 5]; yn = p(1)+p(2)*xn;
plot(x,y,'+r',xn,yn)
title('Linear Regression with Straight Line')
xlabel('x');  ylabel('y');  grid on
case 2
%%%%%  regression with parabola
x = [0; 1; 2; 3.5; 4];
y = [-0.5; 1; 2.4; 2.0; 3.1];

F = [ones(size(x)) x x.^2]
p = (F'*F)\(F'*y)
residual = norm(F*p-y)

xn = linspace(-1,5,20); 
yn = p(1)+p(2)*xn + p(3)*xn.^2;
plot(x,y,'+r',xn,yn)
title('Linear Regression with Parabola')
xlabel('x');  ylabel('y'); grid on

case 3
%%%%% straight line regression, with LinearRegression.m
x = [0; 1; 2; 3.5; 4];
y = [-0.5; 1; 2.4; 2.0; 3.1];

F = [ones(size(x)) x];

[p,y_var,r,p_var] = LinearRegression(F,y)
%[p,y_var,r,p_var] = LinearRegressionMatlab(F,y)

yn = F*p;
plot(x,y,'+r',x,yn)
result = [p sqrt(p_var)]
title('Linear Regression with Straight Line')
xlabel('x');  ylabel('y'); grid on

case 4
%%%%% regression with parabola, with LinearRegression.m
x = [0; 1; 2; 3.5; 4];
y = [-0.5; 1; 2.4; 2.0; 3.1];

F = [ones(size(x)) x x.^2];

%[p,y_var,r,p_var] = LinearRegression(F,y)
[p,y_var,r,p_var] = LinearRegressionMatlab(F,y)

yn = F*p;
plot(x,y,'+r',x,yn)
title('Linear Regression with Parabola')
xlabel('x');  ylabel('y'); grid on

result = [p sqrt(p_var)]

%% generate a smooth curve
figure(2)
xn = linspace(-1,5,51)';
yn = [ones(size(xn)) xn xn.^2]*p;
plot(x,y,'+r',xn,yn)
end%switch
