MATLAB: How to find the coordinates of the point of intersection of a line and a curve

curvegraphintersectionpoint

Capture.PNG
How to find the coordinates of the indicated point?

Best Answer

Add these lines to the end of the previous code (How to draw a tangent using a point and an angle?):
x2_near_origin_idx = find(x2 <= origin(1), 1, 'last');
B2 = [x2(x2_near_origin_idx) 1; x2(x2_near_origin_idx+1) 1] \ [y2(x2_near_origin_idx); y2(x2_near_origin_idx+1)]; % Equation Of Data Near ‘tangline’

Btl = [tangline(1,1) 1; tangline(1,2) 1] \ [tangline(2,1); tangline(2,2)]; % Equation Of ‘tangline’

xint = -(B2(2) - Btl(2)) / (B2(1) - Btl(1));
yint = [xint 1] * B2;
plot(xint, yint, '+r')
so the entire code is now:
filename1 = 'finalocc.xlsx';
filename2 = 'Nzpftest.xlsx';
values1 = xlsread(filename2,'Sheet1','A1:B7');
x1 = values1(:,1);
y1 = values1(:,2);
plot(y1,x1)
hold on
values2 = xlsread(filename1,'Sheet1','A2:B14');
x2 = values2(:,1);
y2 = values2(:,2);
plot(x2,y2, '.-')
dist= 0.32;
xx=interp1(y2,x2,50)
m=(50-0)/(xx-0);
deg= atand(m)
plot([1.68,2],[240.177,240.177])
% hold on
ad = 89.8412;
origin = [1.68;240.177];
lineLen = 20;
tangline = [0 cosd(ad);0 sind(ad)];
tangline = bsxfun(@plus, lineLen*[0 cosd(ad);0 sind(ad)], origin);
plot(tangline(1,:), tangline(2,:))
x2_near_origin_idx = find(x2 <= origin(1), 1, 'last');
B2 = [x2(x2_near_origin_idx) 1; x2(x2_near_origin_idx+1) 1] \ [y2(x2_near_origin_idx); y2(x2_near_origin_idx+1)]; % Equation Of Data Near ‘tangline’
Btl = [tangline(1,1) 1; tangline(1,2) 1] \ [tangline(2,1); tangline(2,2)]; % Equation Of ‘tangline’
xint = -(B2(2) - Btl(2)) / (B2(1) - Btl(1));
yint = [xint 1] * B2;
plot(xint, yint, '+r')
hold off
The point of intersection will be plotted as a red ‘+’ at (xint,yint).