
#interpolace
function cviceni5

#roh
x=linspace(-5,5,5);
y=abs(x/2);
plot(x,y);
x1=linspace(-5,5,100);
y1=interp1(x,y,x1,'linear');
y2=interp1(x,y,x1,'cubic');
plot(x,y,"o",x1,y1,x1,y2,".")
axis([-6,6,-1,4]);



#oblouk
y=cos(x/3-pi);
plot(x,y);
y1=interp1(x,y,x1,'linear');
y2=interp1(x,y,x1,'cubic');
plot(x,y,"o",x1,y1,x1,y2,".")
axis([-6,6,-1.5,1]);




#rychla zmena
x=linspace(-5,5,10);
y=sin(x);
x1=linspace(-5,5,100);
plot(x,y,"o")
y1=interp1(x,y,x1,'linear');
plot(x1,y1,"o")
y2=interp1(x,y,x1,'cubic');
plot(x,y,"o",x1,y2,".");
y3=interp1(x,y,x1,'spline');
plot(x,y,"o",x1,y3,".");
#kubicka interp. a splajn vypadaji podobne
plot(x1,sin(x1),'.r',x1,y2,'b',x1,y3,'color',[0,0.5,0])


#derivace
#linearni
d1=diff(y1);
plot(d1)

dd1=diff(y1,2);
plot(dd1)

#kubicky
d2=diff(y2);
plot(d2)
dd2=diff(y2,2);
plot(dd2)

#splajn
d3=diff(y3);
plot(d3)
dd3=diff(y3,2);
plot(dd3)

close

# koeficienty

koef=interp1(x,y,'cubic','pp')
ppval(koef,1)
sin(1)

#extrapolace v jednom bode
ppval(koef,5.5)
sin(5.5)

#extrapolace funkce
x2=linspace(-5,7,100);
yextr=interp1(x,y,x2,'spline','extrap')
plot(x,y,"o",x2,sin(x2),".",x2,yextr)



# 2D

vzorky=linspace(-5,5,10);
[xx,yy]=meshgrid(vzorky);
jemne=linspace(-5,5,100);
[xx1,yy1]=meshgrid(jemne);
zz=cos(xx).*sin(yy);
surf(xx,yy,zz)

pcolor(zz)
shading('flat')

zz1=interp2(xx,yy,zz,xx1,yy1,'spline')
surf(xx1,yy1,zz1)

pcolor(zz1)
shading('flat')


endfunction
