How to find the intersection values of line and curve?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
GULZAR
am 22 Aug. 2023
Kommentiert: Star Strider
am 22 Aug. 2023
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
0 Kommentare
Akzeptierte Antwort
Star Strider
am 22 Aug. 2023
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 Kommentare
Star Strider
am 22 Aug. 2023
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'})
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.
.
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!