Learn R Programming

pracma (version 1.4.5)

interp1: One-dimensional Interpolation

Description

One-dimensional interpolation of points.

Usage

interp1(x, y, xi = x,
        method = c("constant", "linear", "nearest", "spline", "cubic"))

Arguments

x
Numeric vector; points on the x-axis; at least two points require; will be sorted if necessary.
y
Numeric vector; values of the assumed underlying function; x and y must be of the same length.
xi
Numeric vector; points at which to compute the interpolation; all points must lie between min(x) and max(x).
method
One of ``constant", ``linear", ``nearest", ``spline", or ``cubic".

Value

  • Numeric vector representing values at points xi.

Details

Interpolation to find yi, the values of the underlying function at the points in the vector xi.

Methods can be: ll{ constant constant between points linear linear interpolation (default) nearest nearest neighbor interpolation spline cubic spline interpolation cubic cubic Hermite interpolation }

See Also

approx, splinefun

Examples

Run this code
x <- c(0.8, 0.3, 0.1, 0.6, 0.9, 0.5, 0.2, 0.0, 0.7, 1.0, 0.4)
y <- x^2
xi <- seq(0, 1, len = 81)
yl <- interp1(x, y, xi, method = "linear")
yn <- interp1(x, y, xi, method = "nearest")
ys <- interp1(x, y, xi, method = "spline")

plot(x, y); grid()
lines(xi, yl, col="blue", lwd = 2)
lines(xi, yn, col="black", lty = 2)
lines(xi, ys, col="red")

Run the code above in your browser using DataLab