
function demo_pol_tratti

deg=3;
card=10;

subs=(card-1)/deg;
pts_for_sub=1000;
cardt=subs*pts_for_sub+1;

example=2;

switch example
        case 1
f=@(x) sqrt(x);
a=0; 
b=1;
    case 2
f=@(x) 1./(1+x.^2);
a=-5; 
b=5;
    case 3
f=@(x) exp(x);
a=0; 
b=1;
end

x=linspace(a,b,card);
y=feval(f,x);

xt=linspace(a,b,cardt);
yt=feval(f,xt);

xtp=[];
ytp=[];

for ii=1:subs
    xL=x((ii-1)*deg+1:(ii-1)*deg+1+deg);
    yL=y((ii-1)*deg+1:(ii-1)*deg+1+deg);
    
    xtL=xt((ii-1)*pts_for_sub+1:ii*pts_for_sub+1);
    
    p=polyfit(xL,yL,deg);
    
    diff_val0=3*p(1)*(xL(1))^2+2*p(2)*xL(1)+p(3)
    diff_val1=3*p(1)*(xL(end))^2+2*p(2)*xL(end)+p(3)
    
    pxt=polyval(p,xtL);
    
    xtp=[xtp xtL];
    ytp=[ytp pxt];
    
    diff_val0a=(pxt(2)-pxt(1))/(xtL(2)-xtL(1))
    diff_val1a=(pxt(end)-pxt(end-1))/(xtL(end)-xtL(end-1))
    fprintf('\n \t --------------------')
    pause
end

pp = csape(x,y,'variational');
st=fnval(pp,xt);

clf;
                   
                   hold on;
plot(xt,yt,'k-',xtp,ytp,'b-',xt,st,'r-');
plot(x,y,'co','MarkerEdgeColor','k',...
                       'MarkerFaceColor','c',...
                       'MarkerSize',6);    
legend('Funzione di Runge','Funz. intp. tratti di grado 3', 'Spline cubica naturale intp.','Pti intp.');

hold off;