How to find the intersection values of line and curve?

3 次查看(过去 30 天)
How to find the intersection values of line(black) and the curve(blue)
clc
close all
d1 = 0.4;d2 = 0.6;d = d1 + d2; n1 = 3.46;n2 = 1.5;
lambda = linspace(400e-3,800e-3, 100000); w=2*pi./lambda;
D1 = (2*pi*n1*d1)./lambda;D2 = (2*pi*n2*d2)./lambda;
RHS = cos(D1).*cos(D2) - 0.5*(n1^2+n2^2)/(n1*n2) * sin(D1) .*sin(D2);
plot(w,RHS)
hold on
yline(-1); hold off
hold on
yline(1); hold off

采纳的回答

Star Strider
Star Strider 2023-8-22
Try this —
% clc
% close all
d1 = 0.4;d2 = 0.6;d = d1 + d2; n1 = 3.46;n2 = 1.5;
lambda = linspace(400e-3,800e-3, 100000); w=2*pi./lambda;
D1 = (2*pi*n1*d1)./lambda;D2 = (2*pi*n2*d2)./lambda;
RHS = cos(D1).*cos(D2) - 0.5*(n1^2+n2^2)/(n1*n2) * sin(D1) .*sin(D2);
yl = [-1 1];
for k = 1:numel(yl)
[xi{k},yi{k}] = intsx(w, RHS, yl(k));
end
figure
plot(w,RHS)
hold on
for k = 1:numel(yl)
plot(xi{k}, yi{k}, 'rs')
end
yline(-1)
yline(1)
hold off
function [xi,yi] = intsx(x,y,c) % Simple Intersection Function
% ARGUMENTS: (x,y): Vectors, c: Constant
zci = find(diff(sign(y-c)));
for k = 1:numel(zci)
idxrng = max(1,zci(k)-1) : min(numel(x),zci(k)+1);
xi(k,:) = interp1(y(idxrng)-c,x(idxrng),0);
yi(k,:) = interp1(x(idxrng), y(idxrng), xi(k));
end
end
.
  2 个评论
GULZAR
GULZAR 2023-8-22
Thank you.....One more favor for me, I need the intersection points in ascending order in array. Can you please help.....
Star Strider
Star Strider 2023-8-22
As always, my pleasure!
They already are for each line value (-1,+1) intersection.
To get them all in one array, concatenate them and sort it by the ‘x’ value:
% clc
% close all
d1 = 0.4;d2 = 0.6;d = d1 + d2; n1 = 3.46;n2 = 1.5;
lambda = linspace(400e-3,800e-3, 100000); w=2*pi./lambda;
D1 = (2*pi*n1*d1)./lambda;D2 = (2*pi*n2*d2)./lambda;
RHS = cos(D1).*cos(D2) - 0.5*(n1^2+n2^2)/(n1*n2) * sin(D1) .*sin(D2);
yl = [-1 1];
for k = 1:numel(yl)
[xi,yi] = intsx(w, RHS, yl(k));
xv{k,:} = xi;
yv{k,:} = yi;
end
Intersections = array2table(sortrows(cell2mat([xv yv]),1), 'VariableNames',{'x','y'})
Intersections = 12×2 table
x y ______ __ 7.9193 1 8.5627 1 9.4017 -1 9.8854 -1 10.831 1 11.153 1 12.033 -1 12.734 -1 13.695 1 13.825 1 14.811 -1 15.423 -1
figure
plot(w,RHS)
hold on
for k = 1:numel(yl)
plot(xv{k}, yv{k}, 'rs')
end
yline(-1)
yline(1)
hold off
function [xi,yi] = intsx(x,y,c) % Simple Intersection Function
% ARGUMENTS: (x,y): Vectors, c: Constant
zci = find(diff(sign(y-c)));
for k = 1:numel(zci)
idxrng = max(1,zci(k)-1) : min(numel(x),zci(k)+1);
xi(k,:) = interp1(y(idxrng)-c,x(idxrng),0);
yi(k,:) = interp1(x(idxrng), y(idxrng), xi(k));
end
end
The relevant previous function is interp1, and the relevant new functions are cell2mat, sortrows, and array2table.
.

请先登录,再进行评论。

更多回答(1 个)

Torsten
Torsten 2023-8-22

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by