%%%
% sample script to illustrate overfitting
%%%
r   = 2*pi*rand(1,10);            % points to be fitted randomly selected 
s   = 0.01*randn(size(r))+sin(r); % small noise added
p3  = polyfit(r,s,3);             % fitting with polynomial 3-order
p10 = polyfit(r,s,10);            % 10-order polynomial (10 points)
                                  % i.e. exact fitting

x   = 0:0.1:2.1*pi;
y3  = polyval(p3,x);  % polynomial evaluation
y10 = polyval(p10,x); 

plot(r,s,'*');        % point to be fitted
hold on
plot(x,sin(x),'b--'); % function from which the points were selected
plot(x,y3,'r');       % plot fitted polynomial
plot(x,y10,'g');      % -"-
axis([0 2.11*pi -1.5 1.5]);
hold off
