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

23 ビュー (過去 30 日間)
Samson David Puthenpeedika
Samson David Puthenpeedika 2021 年 11 月 14 日
コメント済み: Dave B 2021 年 11 月 14 日
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
Samson David Puthenpeedika 2021 年 11 月 14 日
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 Center および File ExchangePolar Plots についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by