\Header{approx[fun]}{Interpolation Functions}
\alias{approx}{approx[fun]}
\alias{approxfun}{approx[fun]}
\keyword{arith}{approx[fun]}
\keyword{dplot}{approx[fun]}
\begin{Usage}
\begin{verbatim}
approx   (x, y, xout, method="linear", n=50,
          yleft, yright, rule=1, f=0)
approxfun(x, y,       method="linear",
          yleft, yright, rule=1, f=0)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\texttt{x,y}] vectors giving the coordinates of the points to be
interpolated.  Alternatively a single plotting structure can be
specified.
\item[\texttt{xout}] an optional set of values specifying where interpolation
is to take place.
\item[\texttt{method}] specifies the interpolation method to be used.  Choices
are \texttt{"linear"} or \texttt{"constant"}.
\item[\texttt{n}] If \texttt{xout} is not specified, interpolation takes place at
\texttt{n} equally spaced points spanning the interval [\texttt{min(x)},
\texttt{max(x)}].
\item[\texttt{yleft}] the value to be returned when input \texttt{x} values
less than \texttt{min(x)}. The default is defined by the value
of \texttt{rule} given below.
\item[\texttt{yright}] the value to be returned when input \texttt{x} values
greater than \texttt{max(x)}. The default is defined by the value
of \texttt{rule} given below.
\item[\texttt{rule}] an integer describing how interpolation is to take place
outside the interval [\texttt{min(x)}, \texttt{max(x)}].
If \texttt{rule} is \texttt{1} then \texttt{NA}s are returned for such
points and if it is \texttt{2}, the value at the closest data extreme
is used.
\item[\texttt{f}] For \texttt{method="constant"} a number between 0 and 1
inclusive, indicating a compromise between left- and
right-continuous step functions. If \texttt{y0} and \texttt{y1} are the
values to the left and right of the point then the value is
\texttt{y0*f+y1*(1-f)} so that \texttt{f=0} is right-continuous and
\texttt{f=1} is left-continuous.
\end{ldescription}
\end{Arguments}
\begin{Value}
\texttt{approx} returns a list with components \texttt{x} and \texttt{y},
containing \texttt{n} coordinates which interpolate the given data
points according to the \texttt{method} (and \texttt{rule}) desired.

The function \texttt{approxfun} returns a function performing (linear or
constant) interpolation of the given data points.  For a given set of
\texttt{x} values, this function will return the corresponding
interpolated values.  This is often more useful than \texttt{approx}.\end{Value}
\begin{SeeAlso}
\texttt{\Link{spline}} and \texttt{\Link{splinefun}} for spline
interpolation.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
x <- 1:10
y <- rnorm(10)
par(mfrow = c(2,1))
plot(x, y, main = "approx(.) and approxfun(.)")
points(approx(x, y), col = 2, pch = "*")
points(approx(x, y, method = "constant"), col = 4, pch = "*")

f <- approxfun(x, y)
curve(f(x), 0, 10, col = "green")
points(x, y)
is.function(fc <- approxfun(x, y, method = "const"))  # T
curve(fc(x), 0, 10, col = "darkblue", add = TRUE)
\end{ExampleCode}
\end{Examples}

