필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Capture.PNG
How to find the coordinates of the indicated point?

채택된 답변

Star Strider
Star Strider 2019년 3월 5일
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).
  댓글 수: 2
Neha Sinha
Neha Sinha 2019년 3월 5일
Thank you so much :) .
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’
xint = -(B2(2) - Btl(2)) / (B2(1) - Btl(1));
yint = [xint 1] * B2;
Can you please explain this part?
Star Strider
Star Strider 2019년 3월 5일
As always, my pleasure!
Sure.
The code needs to find the values of ‘x2’ that include ‘origin’, and the ‘x2_near_origin_idx’ calculation detects the 'last' index of ‘x2’ that is less than or equal to ‘origin(1)’. Here, that and the next largest index include the parts of (x2,y2) that include the limits of ‘tangline’. The code uses those coordinates to calculate linear parameter estimates of both lines in that region, calculating them as ‘B2’ for the data and ‘Btl’ for ‘tangline’. It then calculates ‘xint’ using simple algebra, and ‘yint’ using ‘xint’ and the parameters for one of the linear models. (Either one would work and produce the same value for ‘yint’. I chose ‘B2’ arbitrarily.)
This is fortunately a relatively straightforward problem. The code would be much more complicated if the beginning and end limits of ‘tangline’ crossed more than one segment of the data. That would likely involve generating estimates of both the data and ‘tangline’ with much higher resolution (using linspace and interp1) and initially subtracting them to find the approximate region of intersection. Then, a version of the same code here would calculate the intersection with greater precision.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

darova
darova 2019년 3월 5일
x = linspace(0,2.7*pi,2);
y = 0.1*(x)+0.4;
x0 = linspace(2.7*pi, 4*pi,5);
x = [x x0];
y = [y sin(x0)+2.5];
x_ = linspace(0.5, 4*pi-0.5, 100);
y_ = sin(x_+pi)+1.3;
plot(x,y,x_,y_);
hold on
for i = 1:length(x)-1
a = (y(i+1)-y(i)) / (x(i+1) - x(i));
b = y(i) - a*x(i);
for j = 1:length(x_)-1
a_ = (y_(j+1)-y_(j)) / (x_(j+1) - x_(j));
b_= y_(j) - a_*x_(j);
X = (b-b_)/(a_-a);
if( X > x(i) && X < x(i+1) &&...
X > x_(j) && X < x_(j+1))
plot(X,a*X+b,'or')
end
end
end

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by