Borrar filtros
Borrar filtros

How to find the intersection values of line and curve?

1 visualización (últimos 30 días)
GULZAR
GULZAR el 22 de Ag. de 2023
Comentada: Star Strider el 22 de Ag. de 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

Respuesta aceptada

Star Strider
Star Strider el 22 de Ag. de 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 comentarios
GULZAR
GULZAR el 22 de Ag. de 2023
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 el 22 de Ag. de 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'})
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.
.

Iniciar sesión para comentar.

Más respuestas (1)

Torsten
Torsten el 22 de Ag. de 2023

Categorías

Más información sobre Elementary Math en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by