首页 文章

如何在R中通过线性插值找到点

提问于
浏览
5

我有两点(5,0.45)和(6,0.50)并且需要 find the value when x=5.019802 by linear interpolation

但是如何在R中编码呢?

我得到了下面的代码,但只是得到一个图表insdeed .

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')

2 回答

  • 1

    您只需指定 xout 值即可 .

    approx(x,y,xout=5.019802)
    $x
    [1] 5.019802
    
    $y
    [1] 0.4509901
    
  • 8

    我建议制作一个能解决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
    

相关问题