R: Search for the intersection of two lines

I have this simple code that creates 3 matrices and exposes them:

Y=matrix(c(1,2,3,4), nrow=1)
X1=matrix(c(2,3,3.5,4.5))
X2=matrix(c(0.1, 0.2, 0.6, 1.1), nrow=1)
#Plotting
plot(X1, Y)+lines(X1,Y)
par(new=TRUE)
plot(X2, Y)+lines(X2,Y) + abline(v=0.4, col="red")

And here is the plot: enter image description here

Now I want the value to X 0.4get all the values Y. The Y values ​​are the values ​​at which the red line intersects the other two lines. Therefore, there must be two values, one value Y1for one row and another value Y2for the other row.

Is there any function I could use for this? I would really appreciate any suggestion on how to do this.

+4
source share
2 answers

Since the two graphs use different x scales, this is a rather strange question. Getting the intersection point of line X2 is easy, but line X1 is a bit more complicated.

## X2 line
AF2 = approxfun(X2, Y)
AF2(0.4)
[1] 2.5

X1 , 0.4 X2 = 0.4, X1!= 0.4. , 0.4 X1 = 2.5 X1 = 3, , X1 = 2.75.

AF1 = approxfun(X1, Y)
AF1(2.75)
[1] 1.75

:

#Plotting
plot(X1, Y)+lines(X1,Y) + abline(v=0.4, col="red")
par(new=TRUE)
plot(X2, Y)+lines(X2,Y) 
abline(v=0.4)
points(c(0.4,0.4), c(1.75, 2.5), pch=20, col="red")

Crazy graph

+4

identify() , . , , . !

+2

Source: https://habr.com/ru/post/1695429/


All Articles