how to find intersection data between 2 function

조회 수: 8 (최근 30일)
Arif
Arif 2024년 2월 25일
댓글: Walter Roberson 2024년 3월 1일
hi guys can u help me to find the data which is intersection between 2 line ?
this is my graph :
i have data for the blue-line graph. so that i have data for points A,B,C.
The red line is an extended linear line from point A to B.
The yellow line is an vertical line when X = C
How do i get the intersection data between red-line and yellow-line ? the code formula.

채택된 답변

Sam Chak
Sam Chak 2024년 2월 25일
The red line represents the extrapolation up to the vertical line . The main concept here is to determine the equation of line and subsequently calculate the y-coordinate of the intersection point.
A = [0, 0]; % point A
B = [0.05, -150]; % point B
C = 0.125; % vertical line, x = C
figure(1)
plot([A(1), B(1)], [A(2), B(2)]), hold on
xline(C, '--'), grid on
xlabel x, ylabel y
%% Find the line equation between A and B
f = fit([A(1), B(1)]', [A(2), B(2)]', 'poly1')
f =
Linear model Poly1: f(x) = p1*x + p2 Coefficients: p1 = -3000 p2 = -0
%% Intersection coordinate
xi = C; % x-coordinate of the intersection
yi = f.p1*xi + f.p2 % y-coordinate of the intersection
yi = -375
fprintf('The coordinate of the intersection is (%.4f, %.4f).', xi, yi);
The coordinate of the intersection is (0.1250, -375.0000).
figure(2)
xr = B(1):0.001:C;
yr = f.p1*xr + f.p2;
plot([A(1), B(1)], [A(2), B(2)], 'LineWidth', 2), hold on
plot(xr, yr, 'LineWidth', 2)
plot(A(1), A(2), 'o', 'LineWidth', 2, 'MarkerSize', 12)
plot(xi, yi, 'o', 'LineWidth', 2, 'MarkerSize', 12)
plot(B(1), B(2), 'o', 'LineWidth', 2, 'MarkerSize', 12), hold off
xline(C, '-', {'x = 0.125'}, 'color', '#123456'), grid on
xlabel x, ylabel y, xlim([0, 0.3])

추가 답변 (1개)

Hassaan
Hassaan 2024년 2월 25일
편집: Hassaan 2024년 2월 25일
@Arif A rough idea:
% Assuming you have the following coordinates
% A(xA, yA), B(xB, yB), and the x-coordinate of C (xC)
xA = % (your data for xA)
yA = % (your data for yA)
xB = % (your data for xB)
yB = % (your data for yB)
xC = % (your data for xC)
% Calculate the slope of the line AB (red line)
m = (yB - yA) / (xB - xA);
% Calculate the y-coordinate of the intersection point with the yellow line
yIntersect = m * (xC - xA) + yA;
% Display the intersection point
fprintf('The intersection occurs at (x, y) = (%.2f, %.2f)\n', xC, yIntersect);
Note:
  • May need adjustment as per your requirement
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by