function [ p ] = mypolyfit1( X, Y, N )
%%MYPOLYFIT fits a polynomial to data X and Y
%   Returns the vector of the coefficient of
%   the polynomial that best fits (in least squares sense)
%   The polynomial y=p(x) of degree N passes as close as
%   possible from data points (X(i),Y(i))
%   This version uses QR decomposition to invert the matrix
%   Minas Spetsakis, 2016

M=length(X);

clm  = ones(M,1);
Vand = ones(M,1);

for i=1:N
    clm = clm .* X;
    Vand = [clm, Vand];
end
[Q, R] = qr(Vand,0);

p=R\(Q'*Y);

end

