Graphing time vs velocity

21 views (last 30 days)
Liam Wall
Liam Wall on 22 Sep 2021
Edited: DGM on 23 Sep 2021
Im trying to plot the speed of a train over time, fom 0 seconds to 100 seconds in 1 second increments, however my plot is only going from 0 to 1 in 0.1 increments.
% Train acceleration (m/s^2) a1
a1 = input('Input Acceleration: ');
% Train decceleration (m/s^2) a2
a2 = input('Input Decceleration: ');
% Max train velocity (m/s) x
X = input('Input Max Velocity: ');
% Distance between stations (m) S
S = input('Input Distance Between Stations: ');
% convert max speed from km/h to m/s
x = (X*1000)/3600;
% Calculate travel time(t4) = time accelerating(t1), at max speed (t3) and
% deccelerating (t2)
% Accelerating time t1
t1 = x/a1;
%Deccelerating time t2
t2 = x/a2;
%Distance accelerating s1
s1 = (1/2)*a1*t1^2;
%Distnace deccelerating s2
s2 = (1/2)*a2*t2^2;
%Distance at max speed s3
s3 = S - (s1 + s2);
%Time at max speed t3
t3 = s3/x;
% Total travel time (s) t4
t4 = t1 + t2 +t3;
fprintf(' Min travel time between stations is %f s.',t4);
fprintf(' Time travelling at constant speed is %f s.',t3);
t=linspace(0, 1, 100);
if t <= t1
v = a1*t;
elseif (t1 <= t) && (t <= (t1+t3))
v = x;
elseif (t >= (t3+t1)) && (t <= (t1+t2+t3))
v = a2*t;
else
v =0;
end
figure(1)
plot(t,v);

Answers (2)

DGM
DGM on 23 Sep 2021
Edited: DGM on 23 Sep 2021
This is a starting point:
% some test parameters
a1 = 1; % Train acceleration (m/s^2)
a2 = 1; % Train decceleration (m/s^2)
vmax = 20; % Max train velocity (m/s) -- using "x" for velocity is confusing
S = 1000; % Distance between stations (m)
% convert max speed from km/h to m/s
%x = (x*1000)/3600; % input should already be in m/s as stated
t1 = vmax/a1; % Accelerating time t1
t2 = vmax/a2; % Deccelerating time t2
s1 = (1/2)*a1*t1^2; % Distance accelerating s1
s2 = (1/2)*a2*t2^2; % Distnace deccelerating s2
s3 = S - (s1 + s2); % Distance at max speed s3
t3 = s3/vmax; % Time at max speed t3
t4 = t1 + t2 +t3; % Total travel time (s) t4
fprintf(' Min travel time between stations is %f s.\n',t4);
Min travel time between stations is 70.000000 s.
fprintf(' Time travelling at constant speed is %f s.\n',t3);
Time travelling at constant speed is 30.000000 s.
% define the time vector as stated
t = 0:100;
% logical operations and conditional assignment don't work like that
% maybe use masking instead
maskacc = (t >=0) & (t < t1);
maskcon = (t >= t1) & (t < (t1+t3));
maskdec = (t >= (t1+t3)) & (t <= t4);
v = zeros(size(t));
v(maskacc) = a1*t(maskacc);
v(maskcon) = vmax;
v(maskdec) = vmax + a2*((t1+t3)-t(maskdec)); % corrected
plot(t,v);
Note that nothing here checks whether the train stops within 100s. Consider using this instead:
t = 0:t4;
Similarly, note that the results will be incorrect if the route is too short for the train to reach vmax. The calculation of the interval times will need to be changed, and the constant term in the v(maskdec) expression will need to be changed.

Image Analyst
Image Analyst on 23 Sep 2021
You say you want 0-100 in steps of 1 for time, but you're specifying 0-1 in 0.1 increments:
t=linspace(0, 1, 100); % 0-1 with 100 sample points so delta t = 0.1.
Try it like this instead:
t = 0 : 100; % 0 - 100 in steps of 1

Tags

Community Treasure Hunt

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

Start Hunting!