why is my plot only showing 1 diagonal line

5 views (last 30 days)
Takura Nyatsuro
Takura Nyatsuro on 10 Mar 2022
Answered: VBBV on 10 Mar 2022
clear
close all
t=0;
V = 0;
dt = 0.1;
S = 3;
CD = 0.1;
T = 10000;
m = 500;
plot(t,V)
hold on
while t<=5
t(end+1)=t(end)+dt;
F = T - CD*V(end)^21225*S/2;
V(end+1) = V(end) + dt*F/m;
plot(t,V)
end
while t <=10
t(end+1) = t(end)+dt;
F = T - CD*V(end)^21225*S/2;
V(end+1) = V(end) + dt*F/m;
plot(t,V)
end
xlabel('Time (s)')
ylabel('Speed (m/s)')
title('Plot of speed against time')

Answers (2)

Scott MacKenzie
Scott MacKenzie on 10 Mar 2022
Edited: Scott MacKenzie on 10 Mar 2022
First, you probably should move the plot functions outside the while loop. Build up the vectors, then plot!
But, the reason you are seeing only one line is that the two plots are plotting the same line. Below, I demonstrate this by changing the line specs (and moving the plot function outside the loop). You can see the first line plotted in red and the 2nd line plotted on top using a thick black dashed line.
t=0;
V = 0;
dt = 0.1;
S = 3;
CD = 0.1;
T = 10000;
m = 500;
plot(t,V)
hold on
while t<=5
t(end+1)=t(end)+dt;
F = T - CD*V(end)^21225*S/2;
V(end+1) = V(end) + dt*F/m;
end
plot(t,V, '-r')
while t <=10
t(end+1) = t(end)+dt;
F = T - CD*V(end)^21225*S/2;
V(end+1) = V(end) + dt*F/m;
end
plot(t,V, '--k', 'LineWidth', 5)
xlabel('Time (s)')
ylabel('Speed (m/s)')
title('Plot of speed against time')

VBBV
VBBV on 10 Mar 2022
clear
close all
t=0;
V = 0;
dt = 0.1;
S = 3;
CD = 0.1;
T = 10000;
m = 500;
% plot(t,V)
hold all
while t<=10
if t<=5
t(end+1)=t(end)+dt;
F = T - CD*V(end)^21225*S/2;
V(end+1) = V(end) + dt*F/m;
plot(t,V,'-b')
% hold on
else
t(end+1) = t(end)+dt;
F = T - CD*V(end)^21225*S/2;
V(end+1) = V(end) + dt*F/m;
plot(t,V,'-r')
% hold on
end
t = t +1;
end
xlabel('Time (s)')
ylabel('Speed (m/s)')
title('Plot of speed against time')

Community Treasure Hunt

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

Start Hunting!