Cubic Spline Approximation

DESCRIPTION:
Interpolates through the datapoints by means of a cubic spline.

USAGE:
spline(x, y, n = <<see below>>, periodic = F, boundary = 0,
       xmin = min(x), xmax = max(x))

REQUIRED ARGUMENTS:
x,y:
coordinates of points. The coordinates can be given by two arguments that are vectors or by a single argument x which is a univariate time series, a complex vector, a matrix with 2 columns, or a list containing components named x and y. Missing values are not accepted.

OPTIONAL ARGUMENTS:
n:
approximate number of output points. The default is three times the length of x and y.
periodic:
logical, is the function periodic? If TRUE, the y-values at min(x) and max(x) should agree and the output will have derivatives matched at these end points.
boundary:
constant used in boundary value computation. The second derivative of the output function at the end points will be boundary times the second derivative at the adjacent point.
xmin,xmax:
output x values include all the input values (except often the last) plus values equally spaced between each pair of neighboring input x values.

VALUE:
list containing components named x and y with the results of the cubic spline fitting. The spline output has two continuous derivatives and goes exactly through the input points.

DETAILS:
The function was derived from the UNIX system command spline, which gives Hamming (1973) as a reference. Note that this does not smooth the data.

BACKGROUND:
Splines approximate a function with a set of polynomials defined on subintervals. A cubic spline is a collection of polynomials of degree less than or equal to 3 such that the second derivatives agree at the "knots", i.e., the spline has a continuous second derivative.

When interpolating a number of points, a spline can be a much better solution than a polynomial interpolation, since the polynomial can oscillate wildly in order to hit all of the points (polynomials fit the data globally while splines fit the data locally).


REFERENCES:
Blum, E. K. (1972). Numerical Analysis and Computation Theory and Practice. Addison-Wesley, Reading, Mass.

Hamming, R. W. (1973). Numerical Methods for Scientists and Engineers, 2nd ed. McGraw-Hill, New York.


SEE ALSO:
approx , bs , interp , ns .

EXAMPLES:
x <- 1:20; y <- sin(x)
plot(x, y); lines(spline(x, y))