why is my plot only showing 1 diagonal line
2 次查看(过去 30 天)
显示 更早的评论
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')
0 个评论
回答(2 个)
Scott MacKenzie
2022-3-10
编辑:Scott MacKenzie
2022-3-10
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')
0 个评论
VBBV
2022-3-10
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')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scatter Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!