% polyreg.m
% to investigate the least squares approach the polynomial regressions.
clear

% generate a coefficient vector, generaty noisy data
degree=3;
sigma=0.2;
coeff=rand(1,degree+1);
x=-1:0.02:1;
truedata=coeff*[(x.^3)' (x.^2)' (x.^1)' (x.^0)']';
data=truedata+sigma*randn(1,length(truedata));

% estimate the coefficients in the least squares sense
coeffest=polyfit(x,data,degree);

% plot the result
figure(1)
clf
plot(x,truedata,'r', x,data,'g', x, coeffest*[(x.^3)' (x.^2)' (x.^1)' (x.^0)']','b')
hold on
title(' red-true, blue-estimate ')
hold off

% Maybe you will note, particularly if you increase the noise, that polynomial
% fitting is not a very stable method. You can do better if you choose other
% basis functions than x^0, x^1, x^2, ... . Try to obtain a set of orthogonal
% basis functions, such as Legendre polynomials if you insist on using powers.
% A choise noted for its stability is sines and cosines, a k a the Fourier transform.