% Harjutustund 8, ylesanne 1
x=[-1,0.1,0.5,3,4,6.3,7,9];
y=[-2,0.4,0.7,2,4,3.6,3.8,6];
disp([x;y])
plot(x,y,'o')
grid on
% 1) l2hendame esimese astme polünoomiga
p1=polyfit(x,y,1) 
% saime c1=0.6646 ja c0=-0.0882
% Phi(x)=0.6646x-0.0882
yp1=polyval(p1,x);
disp([x;y;yp1])
hold on
plot(x,yp1)
% 2) teise astme polünoomi Phi(x)=c2*x^2+c1*x+c0
p2=polyfit(x,y,2)
yp2=polyval(p2,x)
hold on
plot(x,yp2)
% 3) kolmanda astme polünoomi Phi(x)=c3*x^3+c2*x^2+c1*x+c0
p3=polyfit(x,y,3)
yp3=polyval(p3,x);
hold on
plot(x,yp3)
% 4) kuuenda astme polünoomi Phi(x)=c6*x^6+...+c3*x^3+c2*x^2+c1*x+c0
xvork=[-1:0.1:9];
p6=polyfit(x,y,6)
yp6=polyval(p6,xvork);
hold on
plot(xvork,yp6)
% leiame polünoomi väärtuse, kui x=3.5
% leiame mitme polünoomi korral
[polyval(p1,3.5),polyval(p2,3.5),polyval(p3,3.5),polyval(p6,3.5)]
