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

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?

Risposte (2)

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 Commenti

There is also a contribution in the MATLAB file exchange from @Douglas Schwarz that others have recommended for this purpose. I don't personally have any experience using it.
I think it is quite a bit more general though, as it doesn't assume a common horizontal (x) coordinate that you have.So perhaps overkill for your problem.
There are multiple advantages of a tool like intersections.
  1. Intersections should often be faster than the call you have written using fzero.
  2. Intersections is designed around the idea the function is linear between each pairt of points. And since you are using the default connect the dots interpolation in interp1, that is all it is: piecewise linear. Effectively, using fzero, you are actually using a more sophisticated tool than you need.
  3. Intersections will compute ALL intersections, in case there is more than one intersection between the curves.
  4. It is not difficult to cook up an example where fzero will fail to find any solution, yet there are at least two solutions. Intersections will always find a solution there.
That intersections allows a slightly more general case, where the two curves need not have the same support does not seem terribly relevant. fzero, combined with interp1 also does not require the same support for those curves.
Hi @John D'Errico thanks for the very informative response regarding the advantages of @Douglas Schwarz's interesection program
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

Accedi per commentare.

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.
.

Richiesto:

il 23 Giu 2023

Commentato:

Jon
il 26 Giu 2023

Community Treasure Hunt

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

Start Hunting!

Translated by