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

조회 수: 7 (최근 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 CenterFile Exchange에서 Polar Plots에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by