How to find the intersection of two curves with the input data being two vectors?

조회 수: 118 (최근 30일)
Akshay Varma
Akshay Varma 2023년 6월 23일
댓글: Jon 2023년 6월 26일
The blue and orange lines are arg_px and arg_ikTx respectively.
Both were plotted against the wavelength as 3e8./f11.
Now, how do I go about finding the intersection point?
Are there any inbuilt functions?

답변 (2개)

Jon
Jon 2023년 6월 23일
편집: Jon 2023년 6월 23일
Here is an example of one way to do this, you would have to put in the data for your curves, I just made up two curves for this example:.
% solve intersection between two curves defined pointwise in vectors
% Make example curves
t = linspace(0,3);
y1 = t.^2 ;
y2 = 2 -exp(-t);
% plot curves to see that they intersect
plot(t,y1,t,y2)
% solve for intersection
tsol = findIntersect(t,y1,y2)
tsol = 1.3159
function tintersect = findIntersect(t,y1,y2)
% find intersection between pointwise curves
tintersect = fzero(@zerofun,[0 3]);
% define function which will equal zero when curves intersect
% interpolate to find values between defined points
% note t,y1 and y2 are in scope as this is a nested function
function val = zerofun(tq)
val = interp1(t,y1,tq) - interp1(t,y2,tq);
end
end
  댓글 수: 4
Jon
Jon 2023년 6월 23일
Hi @John D'Errico thanks for the very informative response regarding the advantages of @Douglas Schwarz's interesection program
Jon
Jon 2023년 6월 26일
Did one of these answers, provide you with a solution to your problem? If so please accept an answer so that others will know that an answer is available and the issue is closed

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


Star Strider
Star Strider 2023년 6월 23일
Another approach —
lambda = linspace(0, 10);
arg_px = sin(2*pi*lambda/5);
arg_ikTx = 1.1 - (lambda/8);
L = numel(lambda);
ixv = find(diff(sign(arg_px - arg_ikTx))); % Approximate Intersection Indices
for k = 1:numel(ixv)
idxrng = max(1, ixv(k)-1) : min(L,ixv(k)+1);
intx(k) = interp1(arg_px(idxrng) - arg_ikTx(idxrng), lambda(idxrng), 0, 'linear'); % X-Coordinates
inty(k) = interp1(lambda(idxrng), arg_px(idxrng), intx(k), 'linear'); % Y-Coordinates
end
figure
hp1 =plot(lambda, arg_px, 'DisplayName','arg\_px');
hold on
hp2 =plot(lambda, arg_ikTx, 'DisplayName','arg\_ikTx');
hp3 =plot(intx, inty, 'sm', 'DisplayName','Intersections');
hold off
legend([hp1 hp2 hp3(1)], 'Location','best')
This will detect, calculate, and plot any number of intersections of the two curves.
.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by