Effacer les filtres
Effacer les filtres

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

571 vues (au cours des 30 derniers jours)
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 commentaire
Rik
Rik le 3 Oct 2018
You should either attach the data, or provide us with some details about what function is fitted to the data.

Connectez-vous pour commenter.

Réponse acceptée

KALYAN ACHARJYA
KALYAN ACHARJYA le 3 Oct 2018
Modifié(e) : KALYAN ACHARJYA le 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 commentaires
Benoit Pasquier
Benoit Pasquier le 6 Nov 2023
Modifié(e) : Benoit Pasquier le 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 le 6 Nov 2023
Modifié(e) : DGM le 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.

Connectez-vous pour commenter.

Plus de réponses (2)

Preetham Manjunatha
Preetham Manjunatha le 8 Fév 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 le 8 Mar 2023
Modifié(e) : DGM le 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 commentaires
Rik
Rik le 8 Mar 2023
Déplacé(e) : DGM le 4 Nov 2023
If you want to remove the manual part, you can use the min function instead of find.
Guilherme Weber Sampaio de Melo
Will this code works to geographical coordinates (lat,long) lines?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by