%
% Data points representing height and weight of certain
% people
%

pt1 = [65, 130];
pt2 = [68, 170];
pt3 = [67, 170];
pt4 = [74, 170];
pt5 = [71, 160];
pt6 = [72, 160];

%
% This time, we make a quadratic model
%             weight = a * height^2 + b * height + c
%

% Make A u = d
% where                                       [ y1 ]
%        [x1^2 x1 1 ]                         [ y2 ]
%   A =  [x2^2 x2 1 ], u = [a; b, c] and d =  [ y3 ]
%        [x3^2 x3 1 ]		              [ y4 ]
%				              [ y5 ]

% Just collecting data into a matrix
P = [pt1; pt2; pt3; pt4; pt5; pt6];

samplesize = rows(P)

height = P(:,1)
weight = P(:,2)


A = [height.^2, height, ones(samplesize,1)]

%
% We will solve the problem using pseudo-inverse of A
% produced by pinv(A)
%
TOL = 1e-3
u =  pinv(A, TOL) * weight

a = u(1)

b = u(2)

c = u(3)

% Now, a prediction of weight of a person 64 inches tall

a * 64^2 + b * 64 + c

norm(A*u-weight)/sqrt(samplesize)
