function [t,z] = spline(x,y)
%    SPLINE  -  Cubic spline data interpolation.
%       Given data vectors x and y the
%       function [t,z] = SPLINE(x,y) uses cubic spline interpolation
%       to find a value z corresponding to several values of t from smallest x to largest x.
%
%  First, we need the coefficients for the cubic spline (only needs to be done once actually)

[a,b,c,d] = splinec(x,y);
n = max(size(x))-1;

%  Finally, we output values using the spline to interpolate

t = [];
z = [];


for k=1:n

  %  Use t values in the kth interval with the kth coefficients...

  t0 = x(k):0.01:x(k+1);
  z0 = a(k) + b(k) .*(t0-x(k)) + c(k) .*(t0-x(k)) .^2 + d(k) .*(t0-x(k)) .^3;

  %  Accumulate the points on the end of the previously accumulated points

  t = [t t0];
  z = [z z0];
end