Calculate velocity from position and time

61 visualizaciones (últimos 30 días)
Kelly Harmison
Kelly Harmison el 28 de Feb. de 2018
Editada: alper yeldan el 14 de Sept. de 2023
I have to write a program to calculate the velocity given position and time in two arrays.

Respuesta aceptada

KSSV
KSSV el 28 de Feb. de 2018
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 comentarios
Kelly Harmison
Kelly Harmison el 1 de Mzo. de 2018
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 el 14 de Sept. de 2023
Editada: alper yeldan el 14 de Sept. de 2023
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');

Iniciar sesión para comentar.

Más respuestas (1)

Arms Diether Reyes
Arms Diether Reyes el 6 de Sept. de 2021
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?

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by