How to find point of intersection of lines given y value
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
i have the following code, i am trying to find where the line y = 1000 intersects with each line
l = 5000;
x1 = [0 290];
x2 = [22 233];
x3 = [60 190];
x4 = [80 300];
x5 = [100 439];
x6 = [150 384];
y = [0 l];
%space time graph
plot(x1,y, 'DisplayName' ,'vehicle 1' )
hold on
plot(x2,y, 'DisplayName' ,'vehicle 2')
plot(x3,y, 'DisplayName' ,'vehicle 3')
plot(x4,y, 'DisplayName' ,'vehicle 4')
plot(x5,y, 'DisplayName' ,'vehicle 5')
plot(x6,y, 'DisplayName' ,'vehicle 6')
hold off
% Add title and axis labels
title('Time - Space graph')
xlabel('Time (seconds)')
ylabel('Distance (meters)')
lgd = legend;
lgd.NumColumns = 2;
y1 = yline(1000);
how would i find the corresponding x values when it intersects with each line
0 Kommentare
Antworten (2)
Adam Danz
am 17 Aug. 2020
Bearbeitet: Adam Danz
am 17 Aug. 2020
For each line,
1) Compute the slope of the line using the standard slope forumula.
m = (y(2)-y(1)) / (x(2)-x(1));
2) Compute the y-intercpet of each line using
b = y(1)-m*x(1);
3) Now you have the equation of the line and you just need to solve for x at y=1000
x = (1000 - b) / m;
To show the intersection:
yline(1000)
xline(x)
0 Kommentare
Star Strider
am 17 Aug. 2020
Try this:
l = 5000;
x1 = [0 290];
x2 = [22 233];
x3 = [60 190];
x4 = [80 300];
x5 = [100 439];
x6 = [150 384];
y = [0 l];
xm = [x1; x2; x3; x4; x5; x6];
yl = 1000;
for k = 1:size(xm,1)
B = [xm(k,:); 1 1].' \ y(:);
lines(k,:) = [xm(k,:); 1 1].' * B;
xint(k) = (yl - B(2)) / B(1);
end
%space time graph
figure
hold on
y1 = yline(1000);
for k = 1:size(xm,1)
hv(k) = plot(xm(k,:), y, '-', 'DisplayName', sprintf('vehicle %d',k));
plot(xint(k), yl, '+k');
end
hold off
grid
% Add title and axis labels
title('Time - Space graph')
xlabel('Time (seconds)')
ylabel('Distance (meters)')
lgd = legend(hv, 'Location','SE');
lgd.NumColumns = 2;
producing:

.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Interactive Scenario Authoring finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!