stinepack (version 1.5)

# stinterp: A consistently well behaved method of interpolation

## Description

Returns the values of an interpolating function that runs through a set of points in the xy-plane according to the algorithm of Stineman (1980).

## Usage

`stinterp(x,y,xout,yp,method=c("scaledstineman","stineman","parabola"))`

## Value

`stinterp` returns a list with components 'x' and 'y' with the coordinates of the interpolant at the points specified by `xout`.

## Arguments

x,y

coordinates of points defining the interpolating function. NAs are not allowed, but see `na.stinterp` for gap-filling based on `stinterp`.

xout

the x-coordinate where the interpolant is to be found.

yp

slopes of the interpolating function at x. Optional: only given if they are known, else the argument is not used.

method

method for computing the slope at the given points if the slope is not known. With `method`="stineman", Stineman's original method based on an interpolating circle is used. Use `method`="scaledstineman" if scaling of x and y is to be carried out before Stineman's method is applied, and use `method`="parabola" to calculate the slopes from a parabola through every three points. Partial argument matching is supported (for example `m`="pa" instead of `method`="parabola").

## Author

Tomas Johannesson, Halldor Bjornsson

## References

Stineman, R. W. A Consistently Well Behaved Method of Interpolation. Creative Computing (1980), volume 6, number 7, p. 54-57.

`na.stinterp`, `stinemanSlopes` and `parabolaSlopes`.

## Examples

Run this code
``````## Interpolation with rational functions
#This example shows how the rational interpolating functions used in
#`stinterp' have a monotonic slope over an interval with widely varying
#slopes specified at the end points, consistent with Stineman's
#requirements above. A third degree interpolating function (commonly
#used in spline interpolation), on the other hand, leads to
#spurious oscillations, which are a well known problem in
#interpolation with a single polynomial function and also
#in piecewise polynomial interpolation.
if (FALSE) {
xo <- seq(0,1,by=1/50)
plot(c(0,1),c(0,1),xlim=c(0,1),ylim=c(-1,1),xlab="",ylab="")
for (s in 2:10) {
lines(stinterp(c(0,1),c(0,1),xo,yp=c(0,s)))
lines(xo,xo^2*((s-2)*xo-s+3),col=2) }
}
#Note that the two interpolation functions almost coincide for the
#lowest value (s=2) of the slope at the right end point.
#The user may verify that the rational interpolating functions continue
#to provide "reasonable" results for much higher values of the slope at the
#right end point (for example s=15, s=25 or s=100), for which the third degree
#polynomial leads to absurd results (for most practical purposes).

## Interpolate a smooth curve
#This example illustrates that the interpolation procedure
#reproduces a smooth function with known slopes at the specified
#points very well. If the slopes are not known, both methods for
#estimating the slopes at the specified points (the default method
#and method="parabola") lead to good interpolating functions, but
#the "parabola" method is slightly more accurate. The traditional
#spline interpolation method leads to a similar result as Stineman's
#method with slopes computed with method="parabola".
x <- seq(0,2*pi,by=pi/6)
y <- sin(x)
yp <- cos(x)
xo <- seq(0,2*pi,by=pi/150)
y1 <- stinterp(x,y,xo,yp)\$y
y2 <- stinterp(x,y,xo)\$y
y3 <- stinterp(x,y,xo,m="pa")\$y
if (FALSE) {
plot(x,y)
lines(xo,sin(xo))
points(stinterp(x,y,xo,yp),cex=1/5,col=2)
points(stinterp(x,y,xo),cex=1/5,col=3)
points(stinterp(x,y,xo,method="parabola"),cex=1/5,col=4)
points(spline(x,y,n=length(xo)),cex=1/5,col=5)
}

## Interpolate through a sharp oscillation
#This example shows that Stineman's interpolation, with the default
#method for estimating slopes at the given points, results in no oscillations
#in the neighbourhood of a spike. If the slopes at the given points are
#computed with method="parabola", some overshooting can be seen and
#spline interpolation leads to repeated oscillations near the spike.
if (FALSE) {
yy <- y
yy[3] <- -1.5
plot(x,yy,ylim=c(-1.5,1.5))
points(stinterp(x,yy,xo),cex=1/5,col=3)
points(stinterp(x,yy,xo,method="parabola"),cex=1/5,col=4)
points(spline(x,yy,n=length(xo)),cex=1/5,col=5)
}
``````

Run the code above in your browser using DataLab