MATLAB: Get intersection value from two lines

arraydoublegraphintersectionlinesplotvalue

Hi all,
The following code generates the graph below it:
clear all;
prompt = {'Height Antennas:','Height Tags:','Step:','Distance:','Frequency (MHz):'};
dlg_title = 'Input values';
num_lines = 1;
def = {'0','1','0.1','2','867'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
h1=str2double(answer(1));
h2=str2double(answer(2));
st=str2double(answer(3));
d=str2double(answer(4));
f=str2double(answer(5));
%n=h2/st;
v=0.72/st;
n= ceil(v);
y(1)= h2-0.36;
R1(1) = sqrt(d^2+(y(1)-h1)^2);
R2(1) = sqrt(d^2+(y(1)+h1)^2);
x(1)=R2(1)-R1(1);
for k=2:n,
y(k)=y(k-1)+st;
R1(k) = sqrt(d^2+(y(k)-h1)^2);
R2(k) = sqrt(d^2+(y(k)+h1)^2);
x(k)=R2(k)-R1(k);
end
c=3e8;
f=f*1e6;
lambda=c/f;
plot(x,y,'-rd')
for m=1:5,
lamb(m)=m*lambda;
str = sprintf ('l%d',m);
str = line([lamb(m) lamb(m)], [0 y(k)]);
lam = sprintf ('%d\\Lambda',m);
text(lamb(m),0, lam );
end
%lambda/2
lamb2=lambda/2;
l12 = line([lamb2 lamb2], [0 y(k)]);
lam2 = sprintf ('\\Lambda/2');
text(lamb2,0, lam2 );
% 3lambda/2
lamb32=3*(lambda/2);
l32= line([lamb32 lamb32], [0 y(k)]);
lam32 = sprintf ('3\\Lambda/2');
text(lamb32,0, lam32 );
% 5lambda/2
lamb52=5*(lambda/2);
l52 = line([lamb52 lamb52], [0 y(k)]);
lam52 = sprintf ('5\\Lambda/2');
text(lamb52,0, lam52 );
% 7lambda/2
lamb72=7*(lambda/2);
l72 = line([lamb72 lamb72], [0 y(k)]);
lam72 = sprintf ('7\\Lambda/2');
text(lamb72,0, lam72 );
% 9lambda/2
lamb92=9*(lambda/2);
l92 = line([lamb92 lamb92], [0 y(k)]);
lam92 = sprintf ('9\\Lambda/2');
text(lamb92,0, lam92 );
% 3lambda/4
lamb34=3*(lambda/4);
l34 = line([lamb34 lamb34], [0 y(k)]);
lam34 = sprintf ('3\\Lambda/4');
text(lamb34,0, lam34 );
% 5lambda/4
lamb54=5*(lambda/4);
l54 = line([lamb54 lamb54], [0 y(k)]);
lam54 = sprintf ('5\\Lambda/4');
text(lamb54,0, lam54 );
% 7lambda/4
lamb74=7*(lambda/4);
l74 = line([lamb74 lamb74], [0 y(k)]);
lam74 = sprintf ('7\\Lambda/4');
text(lamb74,0, lam74 );
% 9lambda/4
lamb94=9*(lambda/4);
l94 = line([lamb94 lamb94], [0 y(k)]);
lam94 = sprintf ('9\\Lambda/4');
text(lamb94,0, lam94 );
% 11lambda/4
lamb114=11*(lambda/4);
l114 = line([lamb114 lamb114], [0 y(k)]);
lam114 = sprintf ('11\\Lambda/4');
text(lamb114,0, lam114 );
% 13lambda/4
lamb134=13*(lambda/4);
l134 = line([lamb134 lamb134], [0 y(k)]);
lam134 = sprintf ('13\\Lambda/4');
text(lamb134,0, lam134 );
ylabel('Height (m)', 'FontSize',10)
xlabel('Lambda (m)', 'FontSize',10)
grid on
But I would like to get the value from the y axis when the blue vertical line is intersected with the read line.
The read line is created from two double arrays x and y. However, the blue lines are created using the command line.
Is it clear?
Thanks a lot.
Raúl.

Best Answer

  • check_at = [2:14, 16, 18, 20] / 4;
    intersect_points = interp1( x, y, (check_at * lambda)), 'linear' );
    You should get NaN at any point outside of the red line.
    You can also use check_at values to construct the labels.
    [N, D] = rat(check_at);
    for K = 1 : length(N)
    text(check_at(K)*lambda, 0, sprintf('%dLambda/%d', N(K), D(K)) );
    end
    You could of course tighten this up for the case where D(K) is 1 (the integer multiples)