How to plot 2 lines and find the coordinates of their intersection? (2024)

322 views (last 30 days)

Show older comments

Swati Umamaheshwaran on 17 Sep 2017

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection

Answered: Preetham Manjunatha on 8 Feb 2022

I need to plot the lines x+y=3 and x-y= -1 and find their point of intersection. I know that to plot the lines I could use 'fplot'. How can I find the coordinates of their intersection and plot it? Please help.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (5)

Star Strider on 17 Sep 2017

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#answer_281936

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#answer_281936

Open in MATLAB Online

Another approach:

xymtx = [1 1; 1 -1];

cv = [3; -1];

xy = xymtx\cv; % Calculate Intercept

xint = xy(2); % X-Value Of Intercept

yint = xy(1); % Y-Value Of Intercept

xyind = [xint - 1, xint + 1]'; % X-Values For Plot

xydep = [cv -xyind.*xymtx(:,2)]; % Y-Values For Plot

figure(1)

plot(xyind, xydep(1,:), xyind, xydep(2,:), '-r')

hold on

plot(xint, yint, 'pg', 'MarkerFaceColor','g', 'MarkerSize',10)

hold off

This is straightforward and relatively simple linear algebra. The ‘xydep’ variable calculates the y-values corresponding to the arbitrary values in ‘xydep’, and is the result of solving ‘[X + Y] = C’ for ‘Y’.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Tutku Oztel on 9 Jan 2019

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#answer_355748

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#answer_355748

Open in MATLAB Online

Hello,

I'm sharing the function that I wrote to find the intersection points of two lines with their given slope and constant values:

function [x0 y0] = intersectPoints(m1,m2,b1,b2)

% Insersection point of two lines with known slope and constant

% parameters.

% [x0 y0] = intersectPoints(m1,m2,b1,b1)

% where m's are slope, and b's are constants.

% written by Tutku Öztel in 06.01.2019

x0 = (b2-b1)/(m1-m2); %find the x point

y0 = m1*x0+b1;

end

Late though, but still.. Hope it will be helpful! :)

Tutku

2 Comments

Show NoneHide None

José Luis Sandoval on 7 Jun 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#comment_887846

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#comment_887846

Usefuil, thanks.

Wai Keong Bryce Wong on 12 Oct 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#comment_1052126

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#comment_1052126

This is super useful. Thanks for your help!

Sign in to comment.

Simon Kölbl on 17 Sep 2017

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#answer_281915

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#answer_281915

Open in MATLAB Online

I would do it like this:

% define x Axis and evaluate functions

x_points = -5:0.1:5;

function1 = -x+1;

function2 = x+1;

index_intersection = find(function1 == function2);

x_value_intersection = x_points(index_intersection);

y_value_intersection = function1(index_intersection);

% plot functions and intersection point:

curve1 = plot(x_points, function1);

hold on

curve2 = plot(x_points, function2);

intersection = plot(x_value_intersection, y_value_intersection,...

'Marker', '+', 'MarkerSize', 6, 'Color', 'r');

2 Comments

Show NoneHide None

Cyril Okhio on 13 Nov 2018

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#comment_636946

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#comment_636946

Sorry it did not work. There are errors in Line 9.

Image Analyst on 5 Jun 2021

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#comment_1565965

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#comment_1565965

Open in MATLAB Online

@Cyril Okhio, I think @Simon Kölbl meant this:

clc;% Clear command window.

fprintf('Running %s.m ...\n', mfilename);

clear;% Delete all variables.

close all;% Close all figure windows except those created by imtool.

workspace;% Make sure the workspace panel is showing.

% Define x Axis and evaluate functions

x_points = -5:0.1:5;

function1 = -x_points+1;

function2 = x_points+1;

index_intersection = find(function1 == function2);

x_value_intersection = x_points(index_intersection)

y_value_intersection = function1(index_intersection)

% Plot functions and intersection point:

curve1 = plot(x_points, function1);

hold on

curve2 = plot(x_points, function2);

intersection = plot(x_value_intersection, y_value_intersection,...

'Marker', '+', 'MarkerSize', 20, 'Color', 'r', 'LineWidth', 2);

grid on

caption = sprintf('At intersection, x = %f, y = %f', x_value_intersection, y_value_intersection);

fontSize = 15;

title(caption, 'FontSize', 15);

xlabel('x', 'FontSize', 15);

ylabel('y', 'FontSize', 15);

fprintf('Done running %s.m\n', mfilename);

How to plot 2 lines and find the coordinates of their intersection? (9)

Sign in to comment.

Matt J on 5 Jun 2021

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#answer_717645

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#answer_717645

Edited: Matt J on 5 Jun 2021

Open in MATLAB Online

Using this File Exchange submission,

https://www.mathworks.com/matlabcentral/fileexchange/93470-intersections-of-multiple-2d-lines-or-line-segments

xy=linexlines2D( [1,1,-3].' , [1,-1,1] ); %the intersection point

hold on

fimplicit(@(x,y) x+y-3);

fimplicit(@(x,y) x-y+1);

plot(xy(1),xy(2),'or','MarkerFaceColor','r')

hold off

How to plot 2 lines and find the coordinates of their intersection? (11)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Preetham Manjunatha on 8 Feb 2022

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#answer_891390

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/357026-how-to-plot-2-lines-and-find-the-coordinates-of-their-intersection#answer_891390

Here is the link to find the intersection point of two line segments/lines. A fast two line intersection point finder based on the line parametric space. Finds the intersection point between two lines if it exists or else submits NaN. if you need to find the intersection of the multiple line segments, MATLAB's Mapping Toolbox has function polyxpoly - that finds the intersection points for lines or polygon edges.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsGraphics ObjectsGraphics Object Properties

Find more on Graphics Object Properties in Help Center and File Exchange

Tags

  • intersection of 2 lines

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to plot 2 lines and find the coordinates of their intersection? (13)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to plot 2 lines and find the coordinates of their intersection? (2024)

FAQs

How to find coordinates of intersection of two lines? ›

Point of Intersection Formula
  1. The point of intersection formula is used to find the point of intersection of the two lines, that is the meeting point of two lines. ...
  2. a1x+b1y+c1=0 and a2x+b2y+c2=0.
  3. x= (b1c2-b2c1)/(a1b2-a2b1)
  4. y=(c1a2-c2a1)/(a1b2-a2b1)
  5. (x, y) = ((b1c2-b2c1)/(a1b2-a2b1), (c1a2-c2a1)/(a1b2-a2b1))

How do you find the coordinates of two graphs intersecting? ›

When the graphs of y = f(x) and y = g(x) intersect , both graphs have exactly the same x and y values. So we can find the point or points of intersection by solving the equation f(x) = g(x). The solution of this equation will give us the x value(s) of the point(s) of intersection.

What is the formula for coordinates of point of intersection of two lines? ›

Properties of Intersection of Sets
LawRule of Intersection
Commutative LawA ∩ B = B ∩ A
Associative Law(A ∩ B) ∩ C = A ∩ (B ∩ C)
Law of ϕ and Uϕ ∩ A = ϕ, U ∩ A= A
Idempotent LawA ∩ A = A
1 more row

How to find the intersection of two lines vectors? ›

To find the point of intersection of these two lines, we must find a point P that lies on both lines. At the point of intersection the coordinates for L1 will equal the coordinates for L2. This means there exist values of s and t such that the x, y and z coordinates of the two lines are equal.

How to find the intersection of two sets? ›

Step 1: Determine all of the elements in the first set. Step 2: Determine all of the elements in the second set. Step 3: The intersection is formed by including all of the elements that appear in both Step 1 and Step 2.

How do you find the intersection of two lines on a graph? ›

Two distinct lines intersect at the most at one point. To find the intersection of two lines we just need to solve their equations. The alternative way is to graph the lines and find their point of intersection. The lines will intersect only if they are non-parallel lines.

How do you show that two lines are intersecting? ›

Two straight lines will intersect at a point if they are not parallel. The point of intersection is the meeting point of two straight lines. If two crossing straight lines have the same equations, the intersection point can be found by solving both equations at the same time.

How do you find the point on the intersection line of two planes? ›

To find a point on the line, you set one of the coordinates in the equations of both planes equal to zero and solve the system of equations you end up with. Note! If you set z = 0 and the line of intersection is perpendicular to the z -axis, no points on the line have z = 0 .

What is the intersection of two lines called? ›

The intersection of two lines is called the point of intersection. Since lines are straight figures, a line may only cross another line at one single point.

What is the formula to find the point of intersection? ›

The point of intersection formula is used to find the point of intersection of two lines, meaning the meeting point of two lines. These two lines can be represented by the equation a1x+b1y+c1=0 a 1 x + b 1 y + c 1 = 0 and a2x+b2y+c2=0 a 2 x + b 2 y + c 2 = 0 , respectively.

How do you find the equation of intersection of two lines? ›

Point of intersection means the point at which two lines intersect. These two lines are represented by the equation a1x + b1y + c1= 0 and a2x + b2y + c2 = 0, respectively. Given figure illustrate the point of intersection of two lines.

How to find the point of intersection of two planes? ›

1. Take the cross product of the normal vectors to find the directional vector of the line of intersection: n → α × n → β = ( 1 , 2 , − 3 ) × ( 1 , − 1 , 2 ) = ( 4 − 3 , − 3 − 2 , − 1 − 2 ) = ( 1 , − 5 , − 3 ) . 2. Set z = 0 and solve the system of equations to find the point P .

How to find the coordinates of a point of intersection of two diagonals? ›

Therefore, the intersection point can be found by calculating the midpoint of one of the diagonals. Let's calculate the midpoint of the diagonal AC (A to C). Midpoint formula: \((x_1 + x_2)/2, (y_1 + y_2)/2\).

How do you find the intersection of two relations? ›

For b , going off the last example, the intersection R1∩R2 would consist of ordered pairs {(1,1)}. Now applying that idea here, R1∩R2 relation would be {(a,b)|a divides b or a is a multiple of b} or {(a,b)|a=cb or b=ak for some integers k and c}.

What is the formula for the intersection of two events? ›

Formula of Two Independent Events? We can find the probability of the intersection of two independent events as, P(A∩B) = P(A) × P(B), where, P(A) is the Probability of an event “A” and P(B) = Probability of an event “B” and P(A∩B) is Probability of both independent events “A” and "B" happening together.

References

Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 6324

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.