How to find the point of intersection of two lines in matlab ?

1,637 views (last 30 days)
I have two llines say f1 and f2, each having 100 data points. How to find the point of intersection of these two lines or how to find a points in f1 and f2 which have nearly equal values
  1 Comment
Rik
Rik on 3 Oct 2018
You should either attach the data, or provide us with some details about what function is fitted to the data.

Sign in to comment.

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 3 Oct 2018
Edited: KALYAN ACHARJYA on 3 Oct 2018
% You can proceed this way also
t=0:.1:10;
y1=sin(t); % say f1
y2=exp(t); %f2
intersection=find(y1==y2);
  5 Comments
Benoit Pasquier
Benoit Pasquier on 6 Nov 2023
Edited: Benoit Pasquier on 6 Nov 2023
@DGM No, the "answer" is incorrect because the question specifically mentions "nearly equal values". Better answers are provided in the comments (e.g., Rik's suggestion) and in other answers.
DGM
DGM on 6 Nov 2023
Edited: DGM on 6 Nov 2023
Actually, I suppose you have a good point about the specifics.
I tend to lean toward communicating value through upvoting and letting the OP have dibs on making the decision as to what's chosen as "accepted". Other moderators might feel differently, especially on a high-traffic thread. If so, I'll leave that up to them.
Personally, if I were to consider replacing the answer on a high-traffic thread, I would not be strictly concerned with the specific needs of OP anymore. I instead would be looking for a more comprehensive reference answer that might also better serve the varied needs of current and future readers. After all, the question that 1600 people a month expect to be answered is in the title, and it's very generic.
I agree that exact matching is not typically usable, but I also don't think that nearest-neighbor matching in 1D adequately satisfies the generalized needs. The data is often explicitly specified on mismatched x-locations. The pointlists often differ in length. We often want the point where the polyline segments intersect, not just the nearest vertex -- in fact, that's the case suggested by the title.
If you would like to express your preference for one of the existing answers, you can upvote one. If you would like to try your hand at writing a reference answer covering various approaches to various cases, or if you know of other threads which would serve as good references, you're free to do that as well.
For what it's worth, this thread has been sitting in my bookmarks because I know it could probably use more elaboration. I bookmark them, but I'll never get to them all. That said, while I can change which answer is accepted, I can't do so if I'm the one who wrote the reference answer.

Sign in to comment.

More Answers (2)

Preetham Manjunatha
Preetham Manjunatha on 8 Feb 2022
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.

Clement Hilty
Clement Hilty on 8 Mar 2023
Edited: DGM on 4 Nov 2023
Heyo, if your data isn't a bunch of exactly matching numbers but you still need to find the closest intersection, you can do this:
% Line1 and Line2 are two sets of data based on the same X-indexes
smallNumber = 1
[~,index]=find(abs(Line1-Line2)<smallNumber)
% the closest intersection is:
[X_values(index) Line1(index)] % could also be [X_values Line2(index)]
% The values of Line1 and Line2 never actually equal each other,
% but they get super close.
Then, decrease smallNumber until you only have one response value for "index". For the task I set out to find this for, the smallNumber was actually 1.65 because my data is spaced apart.
  2 Comments
Rik
Rik on 8 Mar 2023
Moved: DGM on 4 Nov 2023
If you want to remove the manual part, you can use the min function instead of find.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices 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!