I have two points (5,0.45) & (6,0.50) and need to find the value when x=5.019802 by linear interpolation
But how to code it in R?
I tried the code below but just got a graph.
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')
What Does Linear Interpolation Mean? Linear interpolation is a form of interpolation, which involves the generation of new values based on an existing set of values. Linear interpolation is achieved by geometrically rendering a straight line between two adjacent points on a graph or plane.
The simplest interpolation method is to locate the nearest data value, and assign the same value.
You just need to specify an xout
value.
approx(x,y,xout=5.019802)
$x
[1] 5.019802
$y
[1] 0.4509901
I suggest make a function that solves for 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With