How to find the intersection values of line and curve?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
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
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'})
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.
.
Más respuestas (1)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!