how to find intersection data between 2 function
조회 수: 8 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Sam Chak
2024년 2월 25일
Hi @Arif
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')
%% Intersection coordinate
xi = C; % x-coordinate of the intersection
yi = f.p1*xi + f.p2 % y-coordinate of the intersection
fprintf('The coordinate of the intersection is (%.4f, %.4f).', xi, yi);
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])
댓글 수: 0
추가 답변 (1개)
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.
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!