% Example of regression
%
% Samuli Siltanen September 2011

% Choose amplitude of noise
noiselevel= 0.3;

% Choose parameters for the line
a = .8;
b = 1;

% Create ideal (y) and noisy (m) data
x = 1:.1:10;
y = a*x+b;
m = y+noiselevel*randn(size(y));

% Plot the model, and ideal and noisy data 
figure(1)
clf
plot(x,y,'k')
hold on
plot(x,y,'k.')
axis equal
box off
axis([0 11 0 10])
title(['Simulation of data: a=', num2str(a),' , b=', num2str(b)])
pause
plot(x,m,'r.')
pause

% Plot the result of regression
figure(2)
clf
plot(x,m,'r.')
hold on
axis equal
box off
axis([0 11 0 10])
title('Regression')
pause
A = [x(:),ones(length(x),1)];
reg = A\m(:);
t = linspace(1,10,20);
plot(t,reg(1)*t+reg(2),'b')
title(['Regression: a=',num2str(reg(1)), ', b=', num2str(reg(2))])

