x1 = [0;1;2;3;4;5;6];
y1 = [9.5;7.6;4.6;7.8;8.1;14.1;20.1];
figure ( 1 )
plot ( x1, y1, 'bo', 'linewidth', 4 )
grid on
print ( '-dpng', 'fitting_data.png' );

figure ( 2 )
plot ( x1, y1, 'bo-', 'linewidth', 4 )
grid on
print ( '-dpng', 'fitting_pwl.png' );

A2 = [ x1.^0, x1.^1, x1.^2];
c2 = pinv(A2)*y1

xp = linspace ( 0.0, 6.0, 101 );
y2 = c2(1) + c2(2) * xp + c2(3) * xp.^2;
figure ( 3 )
plot ( x1, y1, 'bo', xp, y2, 'r-', 'linewidth', 4 )
grid on
print ( '-dpng', 'fitting_degree2.png' );

A3 = [ x1.^0, x1.^1, x1.^2, x1.^3, x1.^4, x1.^5, x1.^6];
c3 = pinv(A3)*y1;
y3 = polyval ( c3(7:-1:1), xp );
figure ( 4 )
plot ( x1, y1, 'bo', xp, y2, 'r-', xp, y3, 'g-', 'linewidth', 4 )
grid on
print ( '-dpng', 'fitting_degree6.png' );

