Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find points by linear interpolation

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')
like image 466
Ys Kee Avatar asked Oct 18 '16 23:10

Ys Kee


People also ask

What does linear interpolation show?

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.

What is the easiest method for solving interpolation?

The simplest interpolation method is to locate the nearest data value, and assign the same value.


2 Answers

You just need to specify an xout value.

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

$y
[1] 0.4509901
like image 160
Ben Bolker Avatar answered Sep 29 '22 22:09

Ben Bolker


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
like image 35
Matt L. Avatar answered Sep 30 '22 00:09

Matt L.