How to find intersection between a straight line and a curve?
Show older comments
I want to find the coordinates for the two intersection points between the horisontal line and the graph. I think the problem is that there is no matching value between the line and the elements in the vector, so when I try to use intersect() I get an empty set. Any idea how this can be done? Thx .

On the y-axis I have a vector with intensity levels, and on the x-axis position. The data is from a file.
az = load('profile_AZ_7cm_Full.mat');
el = load('profile_EL_7cm_Full.mat');
x = az.Position(1,1:end);
intensity_az = 10*log10(sum(az.RF.^2));
normx_intensity = intensity_az-max(intensity_az);
y = el.Position(2,1:end);
intensity_el = 10*log10(sum(el.RF.^2));
normy_intensity = intensity_el-max(intensity_el);
l = -6;
isx = interp1(normx_intensity,l);
figure;
subplot(2,1,1)
plot(x*1e3,normx_intensity)
hold on
yline(l)
title('Azimuth scan of beam profile')
xlabel('x-axis (mm)')
ylabel('Intensity (dB)')
Accepted Answer
More Answers (1)
Davide Masiello
on 4 Feb 2022
I am a bit ovewhelmed by your code, so I will give you a qualitative example instead of modifying it.
x = [x1,x2,x3,___,xn]; % Your array of x data
y = [y1,y2,y3,___,yn]; % Your array of y data
c = c0; % The y value of your straight horizontal line
f = @(z) interp1(x,y-c,z); % Creates anonymous function representing the difference between curve and straight line
xi = fzero(f,x0); % Finds zero of the function (i.e., the intersection point)
The value of xi depends on the the initial condition x0, so you might want to loop the fzero over several x0 values to find multiple intersection points. It could be like this:
x0 = linspace(x1,xn,5);
for i = 1:5
xi(i) = fzero(f,x0(i)); % Finds zero of the function (i.e., the intersection point)
end
Then you can do
xi = unique(xi);
to remove repeated values.
Hope it makes sense.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


