Find Intersection point between two arrays

8 views (last 30 days)
Anfal AlShehhi
Anfal AlShehhi on 20 Jul 2022
Answered: Nipun on 26 Sep 2023
In the intersection part am not getting it. Error in fzero. What function to use to plot the intersection
format long
clc
clear all
% Target Characteristics pre-defined parameters
xt = 2.3/1000; % target (x) in m
yt = 2.3/1000; % target (y) in m
p = 50/100; % percent target
vis = 23; % Visibility in km target
er = 38; % LRF
dref = 500/1000; % LRF distance in m
pref = 85/100; % LRF
T = 90/100; % LRF
wave= 1.543;
%Atmospheric transmission
x= 0:1:10;
b=(0.55/wave);
a=(3.912/vis);
c=0.5*(vis^(1/3));
dbc=b^c;
atmtrans = a*dbc ; % Atmospheric transmission equation
% beam area
angle= 0.6/1000/2; % div angle in mrad
B=[];
sc=[];
A=[];
deno=[];
f1 = [];
f2 = [];
f = [];
xsol = [];
ysol =[];
% distance range in km
for d= 0:1:10
R = tan(angle).*d ; % radius vary with distance in mm
beamArea = (pi.*R.*R) ; % beam area in km2
%target area
targetArea=(xt*yt); % in km2
sc= [sc ((beamArea)./targetArea)]; %ratio between the Atarget and Abeam
% Attenuation
A = [A exp(-2*atmtrans.*d)]; %in km
% Sensitivity
pratio=pref/p
dratio=(d.*d)/(dref^2);
ex=exp(atmtrans *dref*-2);
er10=-10^(er/10);
deno=[T.*sc];
B = [(pratio.*dratio).*((ex*er10)./deno) ];
end
% Plotting curves initially
plot(x, A, x, B)
% Finding the intersection point
for x= 0:1:10
f1 = @(x) [ A exp(-2*atmtrans.*d) ];
f2 = @(x) [(pratio.*dratio).*((ex*er10)./deno) ];
f = @(x) [f1(x) - f2(x)];
x0 = 0.5; %initial guess value (intersection is somewhere near x = 0.5)
xsol = fzero(f, x0)
ysol = f1(xsol)
end
% Marking the intersection point
plot(x, A, x, B), hold on
plot(xsol,ysol, 'mo', 'MarkerSize', 14), hold on
ylim([0 1])
xlabel("Range in km")
ylabel("Distance in km")
title("Range Preformance")
legend('Atmospheric Attenuation','Sensitivity','Intersection')
hold off
  4 Comments
Star Strider
Star Strider on 20 Jul 2022
With respect to interp1, here is an example —
x = [0 10];
Line1 = [x(:) ones(2,1)] * randn(2,1)
Line1 = 2×1
-1.0596 11.6990
Line2 = [x(:) ones(2,1)] * randn(2,1)
Line2 = 2×1
0.0584 9.2103
xint = interp1(Line1 - Line2, x, 0) % X-Intersection Value
xint = 3.0999
yint = interp1(x, Line1, xint) % Y-Intersection Value
yint = 2.8954
figure
plot(x, Line1)
hold on
plot(x, Line2)
plot(xint, yint, 'rs')
hold off
grid
legend('Line1','Line2','Intersection', 'Location','best')
Providing an intersection exists in the region-of-interest, this approach will work.
.
Mathieu NOE
Mathieu NOE on 20 Jul 2022
you can streamline and vectorize your code
no need for for loop
x and d are both used for the same job can be surce of trouble.
I guess there is something wrong with the equations as A and B curves are so off that no risks of crossing at x = 0.5
like this line , th end seems a bit suspect to me :
ex=exp(atmtrans *dref*-2); % how to interpret the -2 ?
ex=exp(-2*atmtrans *dref); ??
clc
clearvars
% Target Characteristics pre-defined parameters
xt = 2.3/1000; % target (x) in m
yt = 2.3/1000; % target (y) in m
p = 50/100; % percent target
vis = 23; % Visibility in km target
er = 38; % LRF
dref = 500/1000; % LRF distance in m
pref = 85/100; % LRF
T = 90/100; % LRF
wave= 1.543;
%Atmospheric transmission
d= 0.1:0.1:10 ; % distance range in km
% do not start at zero (otherwise division by zero occurs in B = [(pratio.*dratio).*((ex*er10)./deno) ];
b=(0.55/wave);
a=(3.912/vis);
c=0.5*(vis^(1/3));
dbc=b^c;
atmtrans = a*dbc ; % Atmospheric transmission equation
% beam area
angle= 0.6/1000/2; % div angle in mrad
R = tan(angle).*d ; % radius vary with distance in mm
beamArea = (pi.*R.*R) ; % beam area in km2
%target area
targetArea=(xt*yt); % in km2
sc = (beamArea./targetArea); %ratio between the Atarget and Abeam
% Attenuation
A = exp(-2*atmtrans.*d); %in km
% Sensitivity
pratio = pref/p;
dratio=(d.*d)/(dref^2);
ex=exp(atmtrans *dref*(-2)); % ex=exp(atmtrans *dref*(-2)) ??
% ex=exp(atmtrans *dref.^(-2)); % ex=exp(atmtrans *dref*(-2)) ??
er10=-10^(er/10);
deno=[T.*sc];
B = [(pratio.*dratio).*((ex*er10)./deno) ];
% Plotting curves initially
plot(d, A, d, B)

Sign in to comment.

Answers (1)

Nipun
Nipun on 26 Sep 2023
Hi Anfal,
I understand that you are trying to find the intersection between two arrays that represent plots on 2D surface.
I recommend using curve intersections by NS to find the intersection of two curves. Download and unzip InterX.m and the following code after plotting A and B:
p = InterX([d;A],[d;B]);
hold on
for i=1:length(p)
plot(p(1,i), p(2,i), 'rs');
end
All points in p correspond to intersection points. If p is empty, then there are no intersection points. Hope this helps.
Regards,
Nipun

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!