How do i Plot average velocity from given velocity time data?
7 views (last 30 days)
Show older comments
Samson David Puthenpeedika
on 14 Nov 2021
Commented: Dave B
on 14 Nov 2021
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);
D=cumtrapz(t,v);
Vavg=(D(10)-D(1))/(t(10)-t(1));
disp("Average velocity= "+Vavg);
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
0 Comments
Accepted Answer
Dave B
on 14 Nov 2021
Edited: Dave B
on 14 Nov 2021
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");
yline(Vavg,'r',"DisplayName","Avg Velocity")
2 Comments
Dave B
on 14 Nov 2021
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");
More Answers (0)
See Also
Categories
Find more on Polar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!