필터 지우기
필터 지우기

How to find the intersection values of line and curve?

조회 수: 1 (최근 30일)
GULZAR
GULZAR 2023년 8월 22일
댓글: Star Strider 2023년 8월 22일
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에서 Elementary Math에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by