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

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

 Risposta accettata

Dave B
Dave B il 14 Nov 2021
Modificato: Dave B il 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");
Or you can take the modern MATLAB approach and use yline:
yline(Vavg,'r',"DisplayName","Avg Velocity")

2 Commenti

oh Yes thankyou . I was little confused thinking there would be multiple values for average velocity but now i understand. Thankyou so much.
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");

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by