Calculate velocity from position and time

104 次查看(过去 30 天)
I have to write a program to calculate the velocity given position and time in two arrays.

采纳的回答

KSSV
KSSV 2018-2-28
pos = rand(100,1) ;
t = 1:100 ;
v = zeros(length(t)-1,1) ;
for i = 1:length(t)-1
v(i) = (pos(i+1)-pos(i))/(t(i+1)-t(i)) ;
end
  3 个评论
Kelly Harmison
Kelly Harmison 2018-3-1
Thanks! I wrote a similar code but I was saving the loop values in v instead of v(i) This was very helpful!
alper yeldan
alper yeldan 2023-9-14
编辑:alper yeldan 2023-9-14
There is a way to solve it withouth for loop which gives the same solution.
vel = diff(pos)./diff(t);
You can compare them
pos = rand(100,1) ;
t = 1:100 ;
v = zeros(length(t)-1,1) ;
for i = 1:length(t)-1
v(i) = (pos(i+1)-pos(i))/(t(i+1)-t(i)) ;
end
vel = diff(pos)./diff(t);
figure;
hold on;
plot(t(1:end-1),v,'--r+');
plot(t(1:end-1),vel,'bo');

请先登录,再进行评论。

更多回答(1 个)

Arms Diether Reyes
The driver of a car wishes to pass a truck that is traveling at a constant speed of 20.0 m/s. Initially, the car is also traveling at 20.0m/s and its front bumper is 24.0 m behind the truck’s rear bumper. The car accelerates at a constant 0.0600 m/s^2, then pulls back into the truck’s lane when the rear of the car is 26.0 m ahead of the front of the truck. The car is 4.5 m long and the truck is 21.0 m long. (a) How much time is required for the car to pass the truck? (b) What distance does the car travel during this time? (c) What is the final speed of the car?

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by