How to find the second interesection between two plots?

조회 수: 2 (최근 30일)
Meshooo
Meshooo 2015년 1월 6일
편집: dpb 2015년 1월 10일
Dear all,
I have two data sets, A and B (117 values each), and the values are included in the attached txt file, and their plot figure as follows:
As we can see from the plot, the two data sets intersect two times. My question is how to find the second intersection Xc?
I tired to use the polyfit but it seems not working because there are two intersections. I tried the following code:
x = 1:117;
hold on;
plot(x, A, 'g-', 'LineWidth', 2);
plot(x, B, 'r-', 'LineWidth', 2);
leftCoefficients = polyfit(x,A,1);
rightCoefficients = polyfit(x,B,1);
xc = (rightCoefficients (2) - leftCoefficients (2)) / (leftCoefficients(1) - rightCoefficients(1))
xc = 83.1228, but I think it should be some value close to 86.
Any idea how to find correct Xc value?
Any help will be appreciated.
Meshoo

채택된 답변

Roger Stafford
Roger Stafford 2015년 1월 6일
There is no reason to believe that the best-fitting straight lines to the two entire curves will intersect at the same points as the curves intersect. You can see that if you add your two straight lines to your plot.
Instead use a high order polynomial fit on the difference between A and B, and then use 'roots' to find the zero crossings. Or better still, plot the two curves and visually locate the approximate intersections. Then do a 'polyfit' for the difference in the immediate vicinity of each intersection and then use 'roots' for each of these.
  댓글 수: 2
dpb
dpb 2015년 1월 6일
Or, equivalent to the zero-crossing of the difference, use fsolve to find the zeros of the difference as the objective function.
Meshooo
Meshooo 2015년 1월 9일
편집: Meshooo 2015년 1월 9일
I tried to find the absolute difference between the two signals, A and B, then find the values at the zero index. If we have two or more values with zero index, then take the highest one.
Seems it is working but I am not sure if it is robust enough..
Dif = abs(A-B);
plot(x, Dif, 'g-', 'LineWidth', 2);
index = find(Dif == min(Dif(:))); % to find the index of low values
Xc = max(index)
Xc is equal to 86, and that's correct for this case.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

dpb
dpb 2015년 1월 10일
편집: dpb 2015년 1월 10일
Regarding your last version/query...
If you only care to the nearest index location, sure. The same idea can be written more succinctly as...
>> find(diff(A>B),1,'last')+1
ans =
86
>>
It'll find the rightmost intersection presuming that there is at least one; for multiple crossings it'll be the first from the RHS of the plot; if only one it'll be that one of course. The '+1' accounts for the fact that the length(diff(x)) is one less than length(x).
Change the 'k' value in the find call to get other crossing locations if such exist. Results of more than one come out sorted, the 'last' argument simply means if there were yet another crossing its index would be lower yet.
If need/want the actual intersection values, could use above as the crude locator then fit a small area of each curve around these points and solve for the intersection as previously indicated.

Julian Hapke
Julian Hapke 2015년 1월 6일

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by