clear all
xdata = [0 1 4 6 8 10];
fdata = [5 2 4 9 2 0];
[a,b,c]=QuadraticSplineCoeffs(xdata,fdata);
n=length(xdata)-1;
x = linspace(xdata(1),xdata(n+1),100);
f=QuadraticSplineEval(a,b,c,xdata,x);
hold off
plot(xdata,fdata,’ko’,’MarkerSize’,20,’MarkerFaceColor’,’r’)
hold on
plot(x,f,’b-‘,’Linewidth’,4)
xlabel(‘x’)
ylabel(‘f(x)’)
function fi=QuadraticSplineEval(a,b,c,xdata,xi)
n=length(xdata)-1;
fi=zeros(1,length(xi));
for i=1:length(xi)
for j=1:n
if xdata(j)>xi(i)
j=j-1;
break
end
end
fi(i)=a(j)+b(j)*(xi(i)-xdata(j))+c(j)*(xi(i)-xdata(j))^2;
end
end
function [a,b,c]=QuadraticSplineCoeffs(xdata,fdata)
n=length(xdata)-1; %/* number of intervals */
a=zeros(1,n+1);
a=zeros(1,n+1);
a=zeros(1,n+1);
for i=1:n+1
a(i)=fdata(i);
end
c(1) = 0.0;
h(1) = xdata(2)-xdata(1);
for i=2:n
h(i) = xdata(i+1)-xdata(i);
c(i) = (1/h(i))*(a(i+1)/h(i)-a(i)*((1/h(i-1))+(1/h(i)))+a(i-1)/h(i-1)-c(i-1)*h(i-1));
end
for i=1:n
b(i)=(a(i+1)-a(i))/h(i)-c(i)*h(i);
end
end