clear all; close all; clc;
%% example for least squares

%% setting up some parameters for generating synthetic data.
num_points = 100;
a = pi;
b = 1.4;
c = -2;

tvec = linspace(-b/(2*a) - 1, -b/(2*a) + 1, num_points);
parabola = @(t) a.*t.^2 + b.*t + c;
yvec = parabola(tvec);
figure(1)
%%plot(tvec, yvec, 'r', 'linewidth', 4); hold on;
set(gca,'fontsize', 20)

tvec_with_noise = tvec' + 0.5*(2.*rand(num_points, 1)-1);
yvec_with_noise = yvec' + 0.5*(2.*rand(num_points, 1)-1);

plot(tvec_with_noise, yvec_with_noise, 'b.', 'linewidth', 10); hold on;

A = [tvec_with_noise.^2 tvec_with_noise ones(num_points, 1)];
b = yvec_with_noise;

xstar = A'*A \ A'*b;
optimal_parabola = @(t) xstar(1).*t.^2 + xstar(2).*t + xstar(3);
plot(tvec, optimal_parabola(tvec), 'r', 'linewidth', 4);


