How can i find the intersection of two lines between points? One of the lines isnt well behaved

조회 수: 31 (최근 30일)
I need to find the intersection values of x for these two lines between output points
  댓글 수: 3
Guillaume
Guillaume 2019년 8월 21일
What's a badly behaved line? Sings loudly outside your house at 2AM? Is slightly curvy at the ends?

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

채택된 답변

Star Strider
Star Strider 2019년 8월 21일
Try this:
x = linspace(0, 1, 40); % Create Data
y1 = 1.0592*ones(size(x)); % Create Data
y1(18) = 1.0251; % Create Data
y2 = 0.093*x + 1;
xi = linspace(min(x), max(x), numel(x)*100); % Interpolation Vector
y1i = interp1(x, y1, xi); % Interpolate
y2i = interp1(x, y2, xi); % Interpolate
yd = y1i - y2i; % Difference
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
ydi = zci(yd); % Zero-Crossing Indices
for k = 1:2
ix = (ydi(k)-1):(ydi(k)+1);
xv = xi(ix);
yv = yd(ix);
B = [xv(:), ones(size(xv(:)))] \ yv(:);
xval(k) = -B(2)/B(1); % Intersection X-Coordinates <—
end
y2r = [x(:) ones(size(y2(:)))] \ y2(:); % Regression For ‘y2’
yval = [xval(:) ones(2,1)] * y2r; % Intersection Y-Coordinates <—
figure
plot(xi, y1i)
hold on
plot(xi, y2i)
plot(xval, yval, 'pg')
hold off
grid
Here ‘xval’ are the x-values of the intersection. This computes the associated ‘yval’ vector and plots the points as well.
How can i find the intersection of two lines between points - 2019 08 21.png
  댓글 수: 4
Alex Earle-Sodhi
Alex Earle-Sodhi 2019년 8월 22일
Simply wonderful!
I thought it might have been something to do with that function only via process of elimination aha
I shall apply this to my code,
Thanks alot!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by