\Header{splinefun}{Interpolating Splines}
\alias{spline}{splinefun}
\keyword{math}{splinefun}
\keyword{dplot}{splinefun}
\begin{Usage}
\begin{verbatim}
splinefun(x, y, method = "fmm")
spline(x, y, n = 3*length(x), method = "fmm",
       xmin = min(x), xmax = max(x))
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x,y}] vectors giving the coordinates of the points to be
interpolated.  Alternatively a single plotting structure can be
specified.
\item[\code{method}] specifies the type of spline to be used.  Possible
values are \code{"fmm"}, \code{"natural"} and \code{"periodic"}.
\item[\code{n}] interpolation takes place at \code{n} equally spaced points
spanning the interval [\code{xmin}, \code{xmax}].
\item[\code{xmin}] left-hand endpoint of the interpolation interval.
\item[\code{xmax}] right-hand endpoint of the interpolation interval.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
If \code{method = "fmm"}, the spline used is that of Forsythe, Malcolm
and Moler (an exact cubic is fitted through the four points at each
end of the data, and this is used to determine the end conditions).
Natural splines are used when \code{method="natural"}, and periodic
splines when \code{method="periodic"}.\end{Details}
\begin{Value}
\code{spline} returns a list containing components \code{x} and
\code{y} which give the ordinates where interpolation took place and
the interpolated values.

\code{splinefun} returns a function which will perform cubic spline
interpolation of the given data points.  This is often more useful
than \code{spline}.\end{Value}
\begin{References}\relax
Forsythe, G. E., Malcolm, M. A. and Moler, C. B. (1977)
\emph{Computer Methods for Mathematical Computations}.\end{References}
\begin{SeeAlso}\relax
\code{\Link{approx}} and \code{\Link{approxfun}} for constant and
linear interpolation.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
op <- par(mfrow = c(2,1), mgp = c(2,.8,0), mar = .1+c(3,3,3,1))
n <- 9
x <- 1:n
y <- rnorm(n)
plot(x, y, main = paste("spline[fun](.) through",n,"points"))
lines(spline(x, y))
lines(spline(x, y, n = 201), col = 2)

y <- (x-6)^2
plot(x, y, main = "spline(.) -- 3 methods")
lines(spline(x, y, n = 201), col = 2)
lines(spline(x, y, n = 201, method = "natural"), col = 3)
lines(spline(x, y, n = 201, method = "periodic"), col = 4)
legend(6,25, c("fmm","natural","periodic"), col=2:4, lty=1)

f <- splinefun(x, y)
ls(envir = environment(f))
splinecoef <- eval(expression(z), envir = environment(f))
curve(f(x), 1, 10, col = "green", lwd = 1.5)
points(splinecoef, col = "purple", cex = 2)
par(op)
\end{ExampleCode}
\end{Examples}

