library(interpolation)
a <- 0.2; bx <- 0.3; by <- -0.4
x0 <- y0 <- seq(1, 10, by = 1)
Grid <- expand.grid(X = x0, Y = y0)
x <- Grid$X; y <- Grid$Y
z <- a + bx*x + by*y
xnew <- ynew <- seq(2.5, 8.5, by = 1)
fun <- interpfun(x, y, z, "linear")
# computed values:
( znew <- fun(xnew, ynew) )
# true values:
a + bx*xnew + by*ynew
# a vector-valued example ####
x <- y <- c(-5, -4, -3, -2, 2, 3, 4, 5)
From <- as.matrix(expand.grid(x0 = x, y0 = y))
f <- function(x0y0) {
d <- c(-10, -5) - x0y0
x0y0 + 0.8 * d / sqrt(c(crossprod(d)))
}
To <- t(apply(From, 1L, f))
x0 <- From[, "x0"]; y0 <- From[, "y0"]
x1 <- To[, 1L]; y1 <- To[, 2L]
# plot data
plot(
x0, y0, asp = 1, pch = 19, xlab = "x", ylab = "y"
)
arrows(x0, y0, x1, y1, length = 0.1)
# interpolate
library(interpolation)
fun <- interpfun(x0, y0, To, method = "linear")
From_new <- rbind(
as.matrix(expand.grid(x0 = c(-1, 0, 1), y0 = (-5):5)),
as.matrix(expand.grid(x0 = c(-5, -4, -3, -2), y0 = c(-1, 0, 1))),
as.matrix(expand.grid(x0 = c(2, 3, 4, 5), y0 = c(-1, 0, 1)))
)
To_new <- fun(From_new)
x0 <- From_new[, "x0"]; y0 <- From_new[, "y0"]
x1 <- To_new[, 1L]; y1 <- To_new[, 2L]
points(x0, y0, pch = 19, col = "red")
arrows(x0, y0, x1, y1, length = 0.1, col = "red")
Run the code above in your browser using DataLab