find intersection of 2 graphs in one plot with unknown function

So i have 3 sets of data each containing 25 rows and 3 columns. (first column are the data points for the x axis and the other 2 for the y axis)
When i plot the data in a single figure they intersect.
how can i find the interscetion without knowing the functions of each graph?

Answers (2)

You could fit your data using polyfit or fit (sounds like your data is relatively smooth). Then run feval on the objects over the same x array (with steps as small as you like x=-5:.0001:5). Then subtract and look for the zeros (approximately zero).
a=fit(m(:,1),m(:,2),'smoothingspline');
b=fit(m(:,1),m(:,3),'smoothingspline');
x=min(m(:,1)):.0001:max(m(:,1));
A=feval(a,x);
B=feval(b,x);
c=find(abs(A-B)<.001,1);%may have to play with the number here (.001). If only 1 crossing specify.
pointOfintersection=[x(c),A(c)];
It would help to have the actual data.
This simply requires basic MATLAB functions:
A = [0:20; (0:20).^2; 100-(0:20).^1.5].'; % Create Array
x_intersect = interp1(A(:,2)-A(:,3), A(:,1), 0); % X-Value At Intersection
y_intersect = interp1(A(:,1),A(:,2),x_intersect); % Y-Value At Intersection
figure
plot(A(:,1), A(:,2), A(:,1), A(:,3))
hold on
plot(x_intersect, y_intersect, 'sk', 'MarkerSize',10)
hold off
grid
legend('Column 1','Column 2', 'Intersection', 'Location','best')
It may be necessary to experiment for it to work with your data, if there is more than 1 intersection or if the data are not monotonically increasing or decreasing with respect to the independent variable. Also, the rows must be sorted by the independent variable in column 1 for this to work.

Categories

Products

Release

R2020b

Asked:

on 25 Mar 2021

Answered:

on 25 Mar 2021

Community Treasure Hunt

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

Start Hunting!