%% Example 2
clear all; clc; close all;
load input
load target
load test
load realt

target = target';
input = input';
realt = realt';
test = test';
Fi=[ones(25,1) input];

theta1=inv(Fi'*Fi)*(Fi'*target); % Linear Regression
theta2=inv(0.1*eye(4)+Fi'*Fi)*(Fi'*target); % Linear Regression with regularization coefficient = 0.1
theta3=inv(0.6*eye(4)+Fi'*Fi)*(Fi'*target);% Linear Regression with regularization coefficient = 0.6

test=[ones(6,1) test]; 
%% Prediction 1 plot
pred1=test*theta1; % prediction 1 (without regularization)

figure(1)
plot(input, target, 'ro', test, realt, 'bo',test, pred1,'g*')
hold on
grid on
xlabel('Input')
ylabel('Output')
title('Linear Regression without Regularization')
er1=sum((realt-pred1').^2) % error 1 (without regularization)

%% Prediction 2 plot
pred2=test*theta2; % prediction 2 (with regularization coefficient = 0.1 )
figure(2)
plot(input, target, 'ro', test, realt, 'bo',test, pred2,'g*')
grid on
xlabel('Input')
ylabel('Output')
title('Linear Regression with Regularization (λ=0.1)')
er2=sum((realt-pred2').^2) % error 2 (with regularization coefficient = 0.1 )

%% Prediction 3 plot
pred3=test*theta3; % prediction 2 (with regularization coefficient = 0.1 )
figure(3)
plot(input, target, 'ro', test, realt, 'bo', test, pred3,'g*')
hold on
grid on
xlabel('Input')
ylabel('Output')
title('Linear Regression with Regularization (λ=0.6)')
er3=sum((realt-pred3').^2) % error 3 (with regularization coefficient = 0.6 )
