close all;
clear all;
clc;


%% Parameters
% Model parameters
a=0.6;
b=-2;
NoiseStd=0.5;
% Training set size
N=1000;
% Learning rate
Eta=0.003;

%% Create trainning set
% X is the input
X=20*rand(N,1)-10;
% Y_ is the expected output (+ noise)
Y_=a*X+b +normrnd(0,NoiseStd,N,1);


%% Initialize weight
W=[0;0];

%% Trainig loop
for i = 1:numel(X)
    % Forward
    Y=W'*[X(i);1];
    W=W+Eta*(Y_(i) - Y)*[X(i);1];
end

%% Plots
% Training set
plot (X,Y_,'.');
hold on;

% Network output
Xt=[-10:10];
Yt=W(1)*Xt + W(2);
plot (Xt,Yt,'r','LineWidth',4);
xlabel ('X');
ylabel ('Y');
title ('Single Layer perceptron');
axis square equal;
grid on;

%% Display results
 Model=[a,b]
Weights=W'

%% Expected output :
%Model =
%
%    0.6000   -2.0000
%
%
%Weights =
%
%    0.5954   -1.8923