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

%% data
d1 = [0 2];
d2 = [-1 5];
d3 = [-2 4];
d4 = [1 1];

%% 1st column is t value
%% 2nd column is y value
data_vec = [d1; d2; d3; d4];
figure(1)
plot(data_vec(:,1), data_vec(:,2),'bo','markersize',10, 'linewidth', 5); hold on
set(gca,'fontsize', 20)

pause

A = [0 0 1; 1 -1 1; 4 -2 1; 1 1 1];
b = [2; 5; 4; 1];
xstar1 = A'*A \ A'*b;
xstar2 = A \ b;
disp(norm(xstar1 - xstar2));
tvec = linspace(-2, 2, 100);
parabola = @(t) xstar1(1).*t.^2 + xstar1(2).*t + xstar1(3);
plot(tvec, parabola(tvec), 'r', 'linewidth', 4);

