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

1,374 次查看(过去 30 天)
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 个评论
Rik
Rik 2018-10-3
You should either attach the data, or provide us with some details about what function is fitted to the data.

请先登录,再进行评论。

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2018-10-3
编辑:KALYAN ACHARJYA 2018-10-3
% You can proceed this way also
t=0:.1:10;
y1=sin(t); % say f1
y2=exp(t); %f2
intersection=find(y1==y2);
  5 个评论
Benoit Pasquier
Benoit Pasquier 2023-11-6
编辑:Benoit Pasquier 2023-11-6
@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 2023-11-6
编辑:DGM 2023-11-6
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.

请先登录,再进行评论。

更多回答(2 个)

Preetham Manjunatha
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 2023-3-8
编辑:DGM 2023-11-4
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 个评论
Rik
Rik 2023-3-8
移动:DGM 2023-11-4
If you want to remove the manual part, you can use the min function instead of find.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by