% fitting data
clc;clf;clear all;

% data
t_k = [-4,-3,-2,-1,0,1,2,3,4];
b_k = [-16,-11,-2.5,.2,1.5,.5,-5.1,-9,-16]';

% plot the data to make an initial guess of what a good fit might be
plot(t_k,b_k,'r*');

%%%% uncomment from here on to perform the fitting

% hold on;
% 
% % assemble estimation matrix for x_1 * t^2 + x_2 * t + x_3
% A = [t_k.^2; t_k; ones(size(t_k))]';
% 
% % %%%%% solve the instead of "A x = b_k" the problem "A^T A x = A^T b_k"
% b = (transpose(A)*b_k);
% AA = transpose(A)*A;
% x = AA\b;
% % try replacing the last three lines by the next and compare
% % x = A\b_k;
% 
% % assemble the estimated function b
% display(' estimated function ')
% display(['b(t)= ',num2str(x(1,1)),' * t^2 + ',num2str(x(2,1)),' * t + ',num2str(x(3,1)) ])
% 
% b = @( t )(x(1,1)*t.^2 + x(2,1)*t + x(3,1));
% 
% % plot estimated function
% t = t_k(1):.1:t_k(end);
% estimate = b(t);
% 
% plot(t,estimate,'g');
% title(['fitted function: b(t)= ',num2str(x(1,1)),' * t^2 + ',num2str(x(2,1)),' * t + ',num2str(x(3,1)) ]);