如何通过线性插值找到点
How to find points by linear interpolation
我有两个点 (5,0.45) 和 (6,0.50),需要通过线性插值找到 x=5.019802 时的值
但是如何在 R 中编码呢?
我尝试了下面的代码,但得到了一个图表。
x <- c(5,6)
y <- c(0.45,0.50)
interp <- approx(x,y)
plot(x,y,pch=16,cex=2)
points(interp,col='red')
我建议创建一个函数来求解 y = mx + b。
x = c(5,6)
y = c(0.45, 0.50)
m <- (y[2] - y[1]) / (x[2] - x[1]) # slope formula
b <- y[1]-(m*x[1]) # solve for b
m*(5.019802) + b
# same answer as the approx function
[1] 0.4509901
您只需指定一个 xout
值。
approx(x,y,xout=5.019802)
$x
[1] 5.019802
$y
[1] 0.4509901
我有两个点 (5,0.45) 和 (6,0.50),需要通过线性插值找到 x=5.019802 时的值
但是如何在 R 中编码呢?
我尝试了下面的代码,但得到了一个图表。
x <- c(5,6)
y <- c(0.45,0.50)
interp <- approx(x,y)
plot(x,y,pch=16,cex=2)
points(interp,col='red')
我建议创建一个函数来求解 y = mx + b。
x = c(5,6)
y = c(0.45, 0.50)
m <- (y[2] - y[1]) / (x[2] - x[1]) # slope formula
b <- y[1]-(m*x[1]) # solve for b
m*(5.019802) + b
# same answer as the approx function
[1] 0.4509901
您只需指定一个 xout
值。
approx(x,y,xout=5.019802)
$x
[1] 5.019802
$y
[1] 0.4509901