Finding the intersect between 2 lines

조회 수: 62 (최근 30일)
Tb
Tb 2021년 2월 11일
댓글: Tb 2021년 2월 11일
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.
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:
Any help would be greatly appreciated.
Thanks.
  댓글 수: 2
Rik
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
x_intersect = 1.3163e-04
y_intersect = line2(x_intersect)
y_intersect = 23.4663
plot(xrt, line1(xrt), '-')
hold on;
plot(xrt, line2(xrt), '-')
plot(x_intersect,y_intersect,'r*')
Tb
Tb 2021년 2월 11일
Ok, but how do I solve this? Thanks

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
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
Tb
Tb 2021년 2월 11일
This worked out perfectly. Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Hill
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*');

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by