How to find intercept of two lines pls
2 Comments
Answers (2)
2 Comments
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!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!