Finding the intersect between 2 lines

56 views (last 30 days)
Tb
Tb on 11 Feb 2021
Commented: Tb on 11 Feb 2021
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 Comments
Rik
Rik on 11 Feb 2021
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 on 11 Feb 2021
Ok, but how do I solve this? Thanks

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 11 Feb 2021
Edited: James Tursa on 11 Feb 2021
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;

More Answers (1)

David Hill
David Hill on 11 Feb 2021
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*');

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!