How to find the intersection of two curves with the input data being two vectors?
90 views (last 30 days)
Show older comments
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1418008/image.png)
0 Comments
Answers (2)
Jon
on 23 Jun 2023
Edited: Jon
on 23 Jun 2023
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)
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 Comments
Jon
on 23 Jun 2023
Hi @John D'Errico thanks for the very informative response regarding the advantages of @Douglas Schwarz's interesection program
Jon
on 26 Jun 2023
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
on 23 Jun 2023
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.
.
0 Comments
See Also
Categories
Find more on Interpolation 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!