How do i Plot average velocity from given velocity time data?

7 次查看(过去 30 天)
Below is the given question. I am not able to plot the below underlined part of the question.
Determine the distance traveled from a velocity function v(t) where the velocity is
explicitly given at the following time points:
t= [1 2 3.25 4.5 6 7 8 8.5 9 10];
v= [5 6 5.5 7 8.5 8 6 7 7 5];
Use the trapezoidal rule. In addition, determine the average velocity
Display in a figure with 2 subplots
• in the upper subplot showing the traveled distance over time as a black solid line,
• in the lower subplot showing the velocity data over time as a blue solid line and the
average velocity as a red dashed line over the time range
• with the corresponding titles, labels of the axes and legend (containing the average
velocity value).
t= [1 2 3.25 4.5 6 7 8 8.5 9 10];
v= [5 6 5.5 7 8.5 8 6 7 7 5];
d=trapz(t,v);
disp("Distance Travelled= "+d);
Distance Travelled= 60.125
D=cumtrapz(t,v);
Vavg=(D(10)-D(1))/(t(10)-t(1));
disp("Average velocity= "+Vavg);
Average velocity= 6.6806
subplot(2,1,1)
plot(t,D,'k');
xlabel("Distance");
ylabel("Time")
title("Travelled Distance over Time");
subplot(2,1,2)
plot(t,v,'b',"DisplayName"," velocity");
hold on
plot(t,Vavg,'r',"DisplayName","Avg Velocity");
hold off

采纳的回答

Dave B
Dave B 2021-11-14
编辑:Dave B 2021-11-14
You were very close, you don't see it because you've plotted a vector t against a scalar Vavg and MATLAB has interpreted this as creating 10 points (but there's no marker, so it's 10 lines each having no x or y span, so they're infinitely small and you can't see them).
plot(t([1 end]),[Vavg Vavg],'r',"DisplayName","Avg Velocity");
Or you can take the modern MATLAB approach and use yline:
yline(Vavg,'r',"DisplayName","Avg Velocity")
  2 个评论
Samson David Puthenpeedika
oh Yes thankyou . I was little confused thinking there would be multiple values for average velocity but now i understand. Thankyou so much.
Dave B
Dave B 2021-11-14
Happy to help. Just in case it comes up in the future, I thought it might be worth noting that you could also have done:
plot(t,repelem(Vavg, numel(t)),'r',"DisplayName","Avg Velocity");

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by