Intersection and maximum value of plots?
11 views (last 30 days)
Show older comments
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?
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);
%Taking thresold as 10^-2
%x and y coordinates of the intersection points
%Max of plot y2
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.
Find more on Line Plots 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!