Finding the intersect between 2 lines
35 次查看(过去 30 天)
显示 更早的评论
Hello, very simple question, however, I have tried using multiple methods, such as polyxpoly() from the Mapping Toolbox to find the intersection between these 2 lines, but I cannot see what the issue is. I have just used a simulatneous equation (as shown in the code) to solve for the equation just to get an output as polyxpoly() wasn't returning anything. The y value seems correct, but the x value is widely incorrect.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/517032/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/517032/image.png)
Here is my code:
xrt = linspace((1/297.7), (1/446.1), 20000)
m11 = -197.7506;
x11 = 0.0031;
b11 = 20.8898;
line1=m11*(xrt-x11)+b11;
plot(xrt, line1, '-')
hold on;
m22 = -897.4321;
x22 = 0.0029'
b22 = 20.9819;
line2=m22*(xrt-x22)+b22;
plot(xrt, line2, '-')
x_intersect = (b22-b11)/(m11-m22) %find the x point
y_intersect = m11*x0+b11
plot(x_intersect,y_intersect,'r*')
The output values for x_intersect and y_intersect:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/517037/image.png)
Any help would be greatly appreciated.
Thanks.
2 个评论
Rik
2021-2-11
Actually your math is incorrect, as the y-value is not correct either:
xrt = linspace((1/297.7), (1/446.1), 20000);
m11 = -197.7506;
x11 = 0.0031;
b11 = 20.8898;
line1=@(xrt) m11*(xrt-x11)+b11;
m22 = -897.4321;
x22 = 0.0029';
b22 = 20.9819;
line2=@(xrt) m22*(xrt-x22)+b22;
x_intersect = (b22-b11)/(m11-m22) %find the x point
y_intersect = line2(x_intersect)
plot(xrt, line1(xrt), '-')
hold on;
plot(xrt, line2(xrt), '-')
plot(x_intersect,y_intersect,'r*')
采纳的回答
James Tursa
2021-2-11
编辑:James Tursa
2021-2-11
If you want the intersection of two lines and you have the equations of the lines, just use backslash. E.g., if you have these equations:
y = m1*x + b1
y = m2*x + b2
Then your system is equivalently
y - m1*x = b1
y - m2*x = b2
or
[1 -m1;1 -m2]*[y;x] = [b1;b2]
So the intersection point is just
[1 -m1;1 -m2] \ [b1;b2]
You just need to plug in the appropriate m and b values for your two lines.
m1 = m11
b1 = m11*(-x11)+b11;
m2 = m22
b2 = m22*(-x22)+b22;
更多回答(1 个)
David Hill
2021-2-11
xrt = linspace((1/297.7), (1/446.1), 20000);
m11 = -197.7506;
x11 = 0.0031;
b11 = 20.8898;
line1=@(x)m11*(x-x11)+b11;
plot(xrt, line1(xrt), '-');
hold on;
m22 = -897.4321;
x22 = 0.0029;
b22 = 20.9819;
line2=@(x)m22*(x-x22)+b22;
plot(xrt, line2(xrt), '-');
A=[1,-m11;1,-m22];
b=[b11-m11*x11;b22-m22*x22];
c=A\b;
plot(c(2),c(1),'g*');
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!