0

I have two sets of data curves, x1,y1 and x1,y2. I wish to find the intersection between these two curves. However, I do not have the equation for x1,y1. Moreover, when using traditionnal intersection computing function, I get little to no result, as the curves do not "exactly" intersect at a specific point: they almost intersect !

I would like to find, for the smallest x1 possible, at which point x1 these curves are very close to each other (almost intersect). However, I am unsure regarding how to proceed... I know for sure that I will need to define a margin, but how can I check if the values y1 and y2 are close to each other and determine for each x1 ? Do you have any idea ? Any function you can think of ?

Thank you,

Here are my two curves: curves almost intersecting

enter image description here

Dave
  • 5,108
  • 16
  • 30
  • 40
Lu_
  • 1
  • Can we assume the two curves are functions. I.e. they pass the [vertical line test](https://en.wikipedia.org/wiki/Vertical_line_test)? – jodag Jun 07 '18 at 21:38

1 Answers1

1

If I understood you want to find the minium distance between two points.

There are different between, if x1,y1,x2, and y2 are matrices/vectors then I suggest the following.

dist=sqrt((x2-x1).^2+(y2-y1).^2); %compute distance
[M,I] = min(dist); %find value of minimum distance and index
xf=x1(I); yf=y1(I); %this is the point on the first curve for the "intersection"
xs=x2(I); ys=y2(I); %this is the point on the second curve for the "intersection"

This should do what you want as it computes the distance and then looks for the minimum value. Now as said in the comments, let's also assume different x elements (different spacing) and/or different number of elements. (x1 and y1 must have the same elements otherwise you can't even plot it, and you'll need to "remesh" your grid/data points).

if length(x1)<length(x2)
    l=length(x1);
    c=length(x2);
    xo=x1; xe=x2; yo=y1; ye=y2;
else 
    l=length(x2);
    c=length(x1);
    xo=x2; xe=x1; yo=y2; ye=y1;
end
dist=zeros(l,c);
for kk=1:l
    xm=xo(kk)*ones(1,c); %you might have to change this depending on if your arrays are vertical/horizontal
    ym=yo(kk)*ones(1,c); %same as xm but for y
    dist(kk,:)=sqrt((xe-xm).^2+(ye-ym).^2); %compute distance
end
[M,I] = min(dist(:)); %find value of minimum distance and index
[row,col]=ind2sub(size(dist),I)

So what does this do, it runs a for loop the minimum amount of times, every iteration it checks 1 value of either against all the other values of the other line. This means it computes all the distances between all the points. Then you again find the minimum distance and you convert the position to a row and column number, the row tells you the index for the shortest array, and column tells you the index for the longest (you can add some stuff to automatically find which is which inside the if function or by adding a few lines at the end).

As mentioned by another user, this will be slow for large datasets 10000+ or 100000+ points(depends on your pc).

Bob van de Voort
  • 211
  • 1
  • 11
  • What if `x1, y1` and `x2, y2` do not have the same number of elements? – rayryeng Jun 07 '18 at 19:20
  • well you're going to have to make a for loop, I'll update it to make the function complete – Bob van de Voort Jun 07 '18 at 21:31
  • 1
    Avoid using a loop. You can find the distances between two sets of points over every pair efficiently. https://stackoverflow.com/q/23911670/3250829. Once you do that, find the row and column that is the smallest which correspond to the indices between the two data sets that give you the points with the smallest distance. I'd recommend using Shai's answer. It's faster with more recent versions of MATLAB. – rayryeng Jun 07 '18 at 21:41
  • @rayryeng True that option is much faster indeed. Not that important if the data set is small but it does matter if it's large (like towards 10000 or a 100000 data points). This was just the first thing that came to mind. I don't mind waiting 1 sec longer ;P (having a good pc helps) – Bob van de Voort Jun 07 '18 at 22:04