%Numerische Mathematik Serie 6 marco Weber

%Aufgabe 3a)

%Seite 76 m script

function[res]=my_spline(x,f,xx)

n=length(x);

for z=1:1:(n-1)
    h(z)=x(z+1)-x(z); 
    b(z)=1/h(z);
end


for z=1:1:(n-2)
   a(z)=2*(b(z)+b(z+1));
end


for z=1:1:(n-1)
    c(z)=(f(z+1)-f(z))/h(z)^2;
end

d(1)=2*c(1)+h(1)/(h(1)+h(2))*(c(1)+c(2));
for z=2:1:(n-1)
    d(z)=3*(c(z-1)+c(z));
end
d(n)=2*c(n-1)+h(n-1)/(h(n-1)+h(n-2))*(c(n-1)+c(n-2));


disp(a)
disp(b)
disp(c)
disp(d)


A=spdiags([[b(1:n-2)';a(n-2)/2;1] [b(1);a';b(n-1)] [1;a(1)/2;b(1:n-1)']],-1:1,n,n);

%abl=f'

abl=A\d';

for j=1:length(xx)
idx = min(find((xx(j)-x)<0))-1;
alpha=[f(idx) f(idx+1)];
beta=[h(idx)*abl(idx) diff(alpha) h(idx)*abl(idx+1)];
gamma=diff(beta);
delta = diff(gamma);
t=(xx(j)-x(idx))/h(idx);
res(j)=alpha(1) +(beta(2)+(gamma(1)+delta(1)*t)*(t-1))*t;
end