Find intersection point between two plotted lines (2025)

12 views (last 30 days)

Show older comments

Mark Sc on 18 Apr 2023

  • Link

    Direct link to this question

    https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines

  • Link

    Direct link to this question

    https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines

Edited: Star Strider on 18 Apr 2023

  • data_tangent.xlsx

Open in MATLAB Online

Hi all,

I have been trying to find an intersection point between two plotted lines however, I used several functions but it doesnot work.. usually the output is 2*0 empty double matrix ... Actually i need the number of intersection ... ?

here is the code and attached are the data

clearvars

clc;

close all

Data = readmatrix('data_tangent.xlsx')

x = Data(:,1);

y = Data(:,2);

[ymax,idx] = max(y);

Binit = x(1:5) \ y(1:5)

Bymax = x(idx) \ y(idx)

Line_init = x*Binit;

Line_initv = Line_init <= ymax;

Line_ymax = x*Bymax;

Line_ymaxv = Line_ymax <= ymax;

figure

plot(x, y)

hold on

plot([0;x(Line_ymaxv)], [0;Line_ymax(Line_ymaxv)], '-r')

hold off

grid

axis('equal')

xlabel('X')

ylabel('Y')

axis([0 max(x) 0 max(y)])

x1=x(Line_ymaxv)

y1=Line_ymax(Line_ymaxv)

P=InterX([x;y],[x1;y1])

The function i used is from "https://www.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections"

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (2)

Star Strider on 18 Apr 2023

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#answer_1218238

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#answer_1218238

The code I wrote for your previous post, Plot tangent line on part of the curve is designed as requested to have only one intersection, that being at the origin, since both tangent lines were requested to go through the origin, at least as I understood it.

How else would you want to define the two tangent lines?

2 Comments

Show NoneHide None

Mark Sc on 18 Apr 2023

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#comment_2711153

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#comment_2711153

@Star Strider

Thank you for your feedback,

just one intersect point thats' what I wanna ...

I just wanna a code to let me know the intersection point value

Thank you once again

Star Strider on 18 Apr 2023

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#comment_2711163

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#comment_2711163

Edited: Star Strider on 18 Apr 2023

Open in MATLAB Online

  • data_tangent.xlsx

My pleasure!

The two lines I plotted (only one is shown here) both intersect at the origin, since that is how they were requested and designed. The intersection with the curve is designed to be at ‘ymax’ with the x-coordinate of that intersection being ‘x(idx)’, so nothing further needs to be computed.

% clearvars

% clc;

% close all

format long

Data = readmatrix('data_tangent.xlsx')

Data = 13042×2

1.980321668300000 7.370000000000000 1.982328936500000 7.350000000000000 1.985318034200000 7.380000000000000 1.987310766000000 7.400000000000000 1.990343472900000 7.310000000000000 1.993270790900000 7.510000000000000 1.995263522700000 7.530000000000000 1.997299863700000 7.430000000000000 2.000270790900000 7.510000000000000 2.002187206600000 7.740000000000000

x = Data(:,1);

y = Data(:,2);

[ymax,idx] = max(y);

Binit = x(1:fix(idx/4)) \ y(1:fix(idx/4))

Binit =

3.644815761434447

Bymax = x(idx) \ y(idx)

Bymax =

3.216013064246002

Line_init = x*Binit;

Line_initv = Line_init <= ymax;

Line_ymax = x*Bymax;

Line_ymaxv = Line_ymax <= ymax;

Intersection_x = interp1(Line_init-Line_ymax, x, 0, 'linear','extrap')

Intersection_x =

2.273736754432321e-13

Intersection_y = interp1(x, Line_init, Intersection_x, 'linear','extrap')

Intersection_y =

9.094947017729282e-13

Intersection = [Intersection_x, Intersection_y] % Desired Result

Intersection = 1×2

1.0e-12 * 0.227373675443232 0.909494701772928

figure

plot(x, y)

hold on

plot([0;x(Line_ymaxv)], [0;Line_ymax(Line_ymaxv)], '-r')

plot([0;x(Line_initv)], [0;Line_init(Line_initv)], '-r')

hold off

grid

axis('equal')

xlabel('X')

ylabel('Y')

axis([0 max(x) 0 max(y)])

Find intersection point between two plotted lines (5)

% x1=x(Line_ymaxv)

% y1=Line_ymax(Line_ymaxv)

% P=InterX([x;y],[x1;y1])

EDIT — (18 Apr 2023 at 20:03)

Added ‘Intersection’ calculation and result using interp1 to calculate the coordinates.

.

Sign in to comment.

Cameron on 18 Apr 2023

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#answer_1218263

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#answer_1218263

Edited: Cameron on 18 Apr 2023

Open in MATLAB Online

Looks like a stress-strain curve. Depending on the material, you could adjust the variable I named cutoff to 0.3*ymax or whatever you want. This code takes the first intersection of the stress-strain curve and interpolates the value for yield.

clearvars

clc;

close all

Data = readmatrix('data_tangent.xlsx')

x = Data(:,1);

y = Data(:,2);

[ymax,idx] = max(y);

Binit = x(1:5) \ y(1:5);

Bymax = x(idx) \ y(idx);

Line_init = x*Binit;

Line_initv = Line_init <= ymax;

Line_ymax = x*Bymax;

Line_ymaxv = Line_ymax <= ymax;

Line_y = Line_ymax(Line_ymaxv);

trunc_x = x(1:find(Line_ymaxv,1,'last'));

trunc_y = y(1:find(Line_ymaxv,1,'last'));

ii = length(trunc_y);

cutoff = 0.5*ymax; %you can adjust this

SaveMe = [];

while trunc_y(ii) > cutoff

if (Line_y(ii) > trunc_y(ii) && Line_y(ii-1) <= trunc_y(ii-1)) | ...

(Line_y(ii) < trunc_y(ii) && Line_y(ii-1) >= trunc_y(ii-1))

SaveMe = [SaveMe;ii];

end

ii = ii - 1;

end

m1 = polyfit(x(SaveMe(end)-1:SaveMe(end)),y(SaveMe(end)-1:SaveMe(end)),1);

m2 = polyfit(x(SaveMe(end)-1:SaveMe(end)),Line_y(SaveMe(end)-1:SaveMe(end)),1);

x_cross = (m2(2) - m1(2))/(m1(1) - m2(1));

y_cross = m1(1)*x_cross + m1(2);

figure

plot(x, y)

hold on

plot([0;x(Line_ymaxv)], [0;Line_ymax(Line_ymaxv)], '-r')

scatter(x_cross,y_cross,'filled')

grid

axis('equal')

xlabel('X')

ylabel('Y')

axis([0 max(x) 0 max(y)])

hold off

3 Comments

Show 1 older commentHide 1 older comment

Mark Sc on 18 Apr 2023

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#comment_2711148

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#comment_2711148

Edited: Mark Sc on 18 Apr 2023

@Cameron

Thank you for your answer,

I meant if I already have a two lines plotted so I just wanna know the point of intersection ..

So would you able to help me ?

Thank you very much

Cameron on 18 Apr 2023

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#comment_2711168

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#comment_2711168

The answer is in the code I provided as x_cross and y_cross.

Mark Sc on 18 Apr 2023

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#comment_2711233

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/1949258-find-intersection-point-between-two-plotted-lines#comment_2711233

@Cameron

Thank you Cameron, but what If I already have my own x and y

x1_y1 as arrays.....

I would appreciate if I can just enter these arrays and got the point of intersection..

Thanks,

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABApp BuildingDevelop Apps Using App Designer

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Tags

  • vectorization
  • plot
  • array
  • find

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.


Find intersection point between two plotted lines (10)

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

Find intersection point between two plotted lines (2025)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Edwin Metz

Last Updated:

Views: 5697

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.