

#interpolacja

0==0;

M=30;
a=0;b=4;
f=@(x) sin(6*x); #sin(x)
N=5;
s=linspace(a,b,10000);
fs=f(s);
for k=1:M,
 x=linspace(a,b,N+1);
 y=f(x);
 p=polyfit(x,y,length(x)-1);
 ps=polyval(p,s);
 er(k)=norm(fs-ps,'inf');
 plot(s,fs,s,ps,";wiel interp;",x,y,"+");pause(1);
 [N, er(k)]
 N++;
endfor


#wezly czebyszewa

M=20;
a=0;b=4;
f=@(x) sin(6*x); #sin(x)
N=5;
s=linspace(a,b,10000);
fs=f(s);
for k=1:M,
 x=(a+b)*0.5+0.5*(b-a)*cos(pi*(0.5+(0:N))/(N+1));
 y=f(x);
 p=polyfit(x,y,length(x)-1);
 ps=polyval(p,s);
 er(k)=norm(fs-ps,'inf');
 plot(s,fs,s,ps,";wiel interp;",x,y,"+");pause(0.5);
 [N, er(k)]
 N++;
endfor


#interp splinami kubicznymi

M=8;er=zeros(M,1);
a=0;b=4;
f=@(x) sin(6*x); #sin(x)
N=5;
s=linspace(a,b,10000);
fs=f(s);
for k=1:M,
 x=linspace(a,b,N+1);
 y=f(x);
 pp=spline(x,y);
 pps=ppval(pp,s);
 er(k)=norm(fs-pps,'inf');
 plot(s,fs,s,pps,";spline interp;",x,y,"+");pause(1);
 [N, er(k)]
 N*=2;
endfor
er(1:M-1)./er(2:M)

#spline not-a-know vs hermitowsi z s'(a)=0=s'(b)
x=-1:6;
zz=linspace(min(x),max(x));
y=10*randn(size(x));
s1=spline(x,y);
s2=spline(x,[0,y,0]);
plot(x,y,"+",zz,ppval(s1,zz),";not-a-knot;",zz,ppval(s2,zz),";herm;");

#splajn bazowy tzn =0 wwe wszystkic hwezlach poza 4 gdzie s =1
x=-1:6;
zz=linspace(min(x),max(x));
y=zeros(size(x)); y(4)=1;
s1=spline(x,y);
s2=spline(x,[0,y,0]);
plot(x,y,"+",zz,ppval(s1,zz),";not-a-knot;",zz,ppval(s2,zz),";herm;");



#splajn o min nosniku
N=10;
x=-1+0:10;
zz=linspace(min(x),max(x),100);
y=zeros(size(x)); y(5)=-4;y(4)=y(6)=-1
s1=spline(x,y);
y(2:N+1)=-y(1:N)
s2=spline(x,[0,y,0]); # by zobaczyc roznice
plot(x,y,"+",zz,szz1=ppval(s1,zz),";not-a-knot;",zz,szz2=ppval(s2,zz),";herm;");














