How to find intercept of two lines pls

5 visualizzazioni (ultimi 30 giorni)
Lacey Little
Lacey Little il 14 Ott 2024
Commentato: Star Strider il 15 Ott 2024
Hi, need help please, i cant find anything that will work on these forums, it should be a really simply answer that doesnt involve re-writing this whole curve. I just want to find the intercept of these two lines on the graph please, how do i do that?
[Editor's note: Info about Vc, fMAX, m, Crr, g, and xe are missing!]
t_v23 = (23 - Vc)/((fMAX/m) - Crr*g);
Unrecognized function or variable 'Vc'.
% Restating xN_An etc
dt = 1; % dt CHANGED !!!
ts = 0; % time start
tf = 2; % time domain
tSim = ts:dt:tf;
n = tf/dt;
figure(1);
xN_An = 0.5*((fMAX/m)-Crr*g)*tSim.^2+Vc*tSim+xe; % analytical position
plot(tSim,xN_An,'--b','LineWidth',1)
title('Distance vs Time (Anal.) dt=1','FontSize',12)
xlabel('Time [s]','FontSize',10)
ylabel('Distance [m]','FontSize',10)
hold on
xline(ones(size(tSim))*t_v23,'--g','linewidth',1)
plot(tSim,xline,'--r','linewidth',1)
hold off
  2 Commenti
Sam Chak
Sam Chak il 15 Ott 2024
In pure math, the term "intercept" typically refers to the point at which a line crosses an axis in a coordinate system. Are you referring to the "intersection" where the blue and green dashed lines cross each other?
If so, are you looking for the pure math method (as described in textbooks) or the MATLAB numerical technique that is not commonly found in geometry math textbooks?
Umar
Umar il 15 Ott 2024
Hi @Lacey Little,
If you need any further assistance, please let us know.

Accedi per commentare.

Risposte (2)

Star Strider
Star Strider il 14 Ott 2024
Modificato: Star Strider il 15 Ott 2024
There apears to be much missing informaiton.
That aside, the general approach would be —
x = linspace(0, 2, 3).';
y = x.^2;
xl = 1.21;
y_intcp = interp1(x, y, xl)
y_intcp = 1.6300
figure
plot(x, y, 'DisplayName','Data')
hold on
plot(xl, y_intcp, 'sr', 'DisplayName','Intercept')
hold off
xline(xl, 'DisplayName','xline')
xlabel('Time [s]')
ylabel('Distance [m]')
legend('Location','best')
EDIT — (15 Oct 2024 at 11:47)
Since I am not certain what intercept you want to find, an alternative to find both the xline intercept with the data plot and the intersection of the two parts of the data line plot would be —
x = linspace(0, 2, 3).';
y = x.^2;
xl = 1.21;
y_intcp1 = interp1(x, y, xl)
y_intcp1 = 1.6300
FirstLineX = [0.1 0.5];
FirstLineY = interp1(x, y, FirstLineX);
B1 = [FirstLineX; [1 1]].' \ FirstLineY(:);
SecondLineX = [1.9 2.0];
SecondLineY = interp1(x, y, SecondLineX);
B2 = [SecondLineX; [1 1]].' \ SecondLineY(:);
x_intcp2 = (B2(2) - B1(2)) / (B1(1) - B2(1))
x_intcp2 = 1
y_intcp2 = [x_intcp2 1] * B1
y_intcp2 = 1
x_intcp2 = fsolve(@(x) B1(1)*x + B1(2) - (B2(1)*x + B2(2)), rand) % Alternativee Approach
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x_intcp2 = 1
figure
plot(x, y, '--b', 'DisplayName','Data')
hold on
plot(xl, y_intcp1, 'sr', 'DisplayName','Intercept: ‘xline’ & ‘Data’', 'MarkerSize',10)
plot(x_intcp2, y_intcp2, 'sg', 'DisplayName','Intercept: ‘Data’ Line Segments', 'MarkerSize',10)
hold off
xline(xl, '--g', 'DisplayName','xline')
xlabel('Time [s]')
ylabel('Distance [m]')
legend('Location','NW')
.
  2 Commenti
Sam Chak
Sam Chak il 15 Ott 2024
I believe I have reconstructed the same scenario with 95% confidence, although there is a significant amount of missing information. The blue dashed line is a quadratic curve as indicated in the OP's original code.
x = [ 0 1 2]'; % visually estimated
y = [25 42 67]'; % visually estimated
p2 = fit(x, y, 'poly2')
p2 =
Linear model Poly2: p2(x) = p1*x^2 + p2*x + p3 Coefficients: p1 = 4 p2 = 13 p3 = 25
t_v23 = 1.22; % visually estimated
% Original parameters by the OP
dt = 1; % dt CHANGED !!!
ts = 0; % time start
tf = 2; % time domain
tSim = ts:dt:tf;
n = tf/dt;
figure(1);
% xN_An = 0.5*((fMAX/m) - Crr*g)*tSim.^2 + Vc*tSim + xe; % analytical position
xN_An = 4*tSim.^2 + 13*tSim + 25;
plot(tSim, xN_An,'--b','LineWidth', 1)
xline(t_v23, '--g', 'linewidth', 1)
xticks([0:0.2:2])
Star Strider
Star Strider il 15 Ott 2024
@Sam Chak — You are probably correct.
Combining your code with mine produces this result —
x = [ 0 1 2]'; % visually estimated
y = [25 42 67]'; % visually estimated
p2 = fit(x, y, 'poly2')
p2 =
Linear model Poly2: p2(x) = p1*x^2 + p2*x + p3 Coefficients: p1 = 4 p2 = 13 p3 = 25
t_v23 = 1.22; % visually estimated
% Original parameters by the OP
dt = 1; % dt CHANGED !!!
ts = 0; % time start
tf = 2; % time domain
tSim = ts:dt:tf;
n = tf/dt;
% x = linspace(0, 2, 3).';
% y = x.^2;
% xl = 1.21;
xN_An = 4*tSim.^2 + 13*tSim + 25;
y_intcp1 = interp1(tSim, xN_An, t_v23)
y_intcp1 = 47.5000
FirstLineX = [0.1 0.5];
FirstLineY = interp1(tSim, xN_An, FirstLineX);
B1 = [FirstLineX; [1 1]].' \ FirstLineY(:);
SecondLineX = [1.9 2.0];
SecondLineY = interp1(tSim, xN_An, SecondLineX);
B2 = [SecondLineX; [1 1]].' \ SecondLineY(:);
x_intcp2 = (B2(2) - B1(2)) / (B1(1) - B2(1))
x_intcp2 = 1.0000
y_intcp2 = [x_intcp2 1] * B1
y_intcp2 = 42.0000
x_intcp2 = fsolve(@(x) B1(1)*x + B1(2) - (B2(1)*x + B2(2)), rand) % Alternativee Approach
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x_intcp2 = 1.0000
figure(1);
% xN_An = 0.5*((fMAX/m) - Crr*g)*tSim.^2 + Vc*tSim + xe; % analytical position
xN_An = 4*tSim.^2 + 13*tSim + 25;
plot(tSim, xN_An,'--b','LineWidth', 1)
xline(t_v23, '--g', 'linewidth', 1)
xticks([0:0.2:2])
hold on
plot(t_v23, y_intcp1, 'sr', 'DisplayName','Intercept: ‘xline’ & ‘Data’', 'MarkerSize',10)
plot(x_intcp2, y_intcp2, 'sg', 'DisplayName','Intercept: ‘Data’ Line Segments', 'MarkerSize',10)
hold off
xlabel('Time [s]')
ylabel('Distance [m]')
legend('Location','NW')
I appreciate your contribution here.
.

Accedi per commentare.


Umar
Umar il 15 Ott 2024

Hi @Lacey Little ,

To find the intercept of two lines on a graph in MATLAB, you need to define the equations of both lines clearly and then calculate their intersection point. In your case, you have one line defined by the equation for xN_An, and you want to plot it against another line defined by t_v23. As I mentioned define all the parameters used in the equations, such as Vc, fMAX, m, Crr, g, and xe. You have already provided the equation for xN_A which represents the position over time. Afterwards, plot the analytical position and the vertical line at t_v23. To find the intersection, solve the equations of the two lines. Here is the complete MATLAB code that incorporates all the above steps:

% Define parameters
Vc = 5;        % Initial velocity in m/s
fMAX = 10;     % Maximum force in N
m = 2;         % Mass in kg
Crr = 0.01;    % Coefficient of rolling resistance
g = 9.81;      % Acceleration due to gravity in m/s^2
xe = 0;        % Initial position in m
% Calculate t_v23
t_v23 = (23 - Vc) / ((fMAX/m) - Crr * g);
% Time settings
dt = 1;        % Time step
ts = 0;        % Time start
tf = 2;        % Time end
tSim = ts:dt:tf; % Time simulation vector
n = tf/dt;    % Number of time steps
% Calculate analytical position
xN_An = 0.5 * ((fMAX/m) - Crr * g) * tSim.^2 + Vc * tSim + xe; 
% Plotting
figure(1);
plot(tSim, xN_An, '--b', 'LineWidth', 1); % Plot analytical position
hold on;
% Plot vertical line at t_v23
xline(t_v23, '--g', 'LineWidth', 1);
% Define the second line (for example, a linear function)
% Here we assume a simple linear function for demonstration
slope = 2; % Example slope
intercept = 1; % Example y-intercept
line2 = slope * tSim + intercept; % Define the second line
% Plot the second line
plot(tSim, line2, '--r', 'LineWidth', 1);
% Hold off to stop adding to the current plot
hold off;
% Title and labels
title('Distance vs Time (Analytical and Linear)', 'FontSize', 12);
xlabel('Time [s]', 'FontSize', 10);
ylabel('Distance [m]', 'FontSize', 10);
legend('Analytical Position', 't_v23', 'Second Line', 'Location', 'Best');
% Finding the intersection
% We can find the intersection by solving the equations
% xN_An = line2
% Rearranging gives us: 0.5 * ((fMAX/m) - Crr * g) * t^2 + Vc * t + xe - (slope                               * t + intercept) = 0
coeffs = [0.5 * ((fMAX/m) - Crr * g), Vc - slope, xe - intercept];
t_intersect = roots(coeffs); % Find roots of the polynomial
% Display the intersection time
disp('Intersection time(s):');
disp(t_intersect);
% Calculate the intersection point
y_intersect = slope * t_intersect + intercept; % Calculate y value at 
intersection
disp('Intersection point(s):');
disp([t_intersect, y_intersect]);

Please see attached.

In the above code snippet,

  • The parameters are defined at the beginning to ensure clarity and ease of modification.
  • The time vector tSim is created to simulate the time domain.
  • The analytical position is calculated using the provided formula.
  • The analytical position and the second line are plotted, along with a vertical line at t_v23.
  • The intersection is calculated by solving the polynomial formed by equating the two lines.
  • The roots function is used to find the time(s) at which the lines intersect.
  • The intersection times and points are displayed in the command window.

Hope this helps.

If you have any further questions or need additional modifications, feel free to ask!

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by