首页 > 解决方案 > 在R中找到两条线的交点

问题描述

我有一些代码可以从一组点中绘制两条线。我需要找到两者的交集。

red <- read.table("red.txt", header=TRUE) #Get's table
mag <- red$T #T values
outer <- red$O #outer values
inner <- red$I #inner values
plot(outer, mag) #plot outer points
abline(lm(mag~outer)) #line of best fit 
abline(inner, mag) #add inner line

它产生一个在此处输入图像描述类似于我想要的图表。
(我知道这不是最时尚的)。如何找到线交叉的坐标?

(对不起,我只是在学习如何使用它的狡猾可能令人作呕的代码)

标签: rgraph

解决方案


从斜率/截距 (m/b) 回归输出生成直线方程。所以

reg1 <- lm(y1 ~ x1)
reg2 <- lm(y2 ~ x2)

提供斜率 (m1, m2) 和截距 (b1, b2)

y1 = m1x1 + b1

y2 = m2x2 + b2

由于 x1 = x2 = x 和 y1 = y2 = y 那么

m1x - y = -b1

m2x - y = -b2

您可以求解 x,y 未知数的方程。


推荐阅读