# Intersection and maximum value of plots?

11 views (last 30 days)
James Lumley on 7 Dec 2015
Answered: Arnab Sen on 28 Dec 2015
I've got multiple plots on one figure, and I need to figure out the point of intersection of only two of the plots, and then the maximum value of another plot?
Just wondering if anyone could give me some help on the code I would need to find this?
Cheers
##### 2 CommentsShow 1 older commentHide 1 older comment
James Lumley on 7 Dec 2015
The thing is is that i've used multiple equations and got arrays with thousands of elements to draw the plots, i was wondering if there was a quick 'code' way of doing it

Arnab Sen on 28 Dec 2015
From the description I understand that you would like to find out intersection of multiple plots and find maximum of another plot.
If you do not have the access to the vectors which construct the plot, you can get those vectors from 'XData' ,'YData' properties of line structure returned by the 'plot' function. Now note that this vectors are discrete points on the plot and may not contain the actual intersection point. However, we can approximate it by taking those points for which the values of y coordinates are less than a threshold value. Note that more the number of points, more dense the points are, less the threshold and the more approximated values are closer to real intersection points.
Following code snippet illustrates this approach:
>> x = linspace(-2*pi,2*pi,1000);
>> y1 = sin(x);
>> y2 = cos(x);
>> p = plot(x,y1,x,y2);
>> idy1=p(1).YData;
>> idy2=p(2).YData;
%Taking thresold as 10^-2
>> index=find(abs(idy1-idy2)<=10^-2);
%x and y coordinates of the intersection points
>> intersection_X_coordinates=x(index);
>> intersection_y_coordinates=idy1(index);
%Max of plot y2
>> maxy2=max(idy2);
index contains the points for which value of y coordinates of both the plot is differs at most by 10^-2. Here we have taken 1000 points. You can take more points if you need more accuracy. However, if the there are too many points, there is a chance of multiple points close to a single intersection point are given as output by this script.