how to find intersection data between 2 function

11 views (last 30 days)
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.

Accepted Answer

Sam Chak
Sam Chak on 25 Feb 2024
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])

More Answers (1)

Hassaan
Hassaan on 25 Feb 2024
Edited: Hassaan on 25 Feb 2024
@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.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!