MATLAB Answers

# How to get a velocity graph from recorded displacements and times?

3 views (last 30 days)
Henry Bowden on 30 Apr 2021
Edited: Scott MacKenzie on 6 May 2021
I wasn’t sure if this was the place to be asking this, but here we go.
So I have an object that is moving in a straight path, and I recorded the distance from the start, and the times associated with each of these. I want to get either a plot of the velocity at various points along the path, or an equation for the velocity.
I know how to plot the displacement vs. time, using simple arrays then plot(x,t); but was wondering how I could go from there to get the varying velocity, and possibly acceleration (not as vital).
I know I don’t really have any code right now, which is why I wasn’t sure if this was the place to ask, but anything is helpful! Thanks!
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (2)

Mathieu NOE on 30 Apr 2021
hello
to get velocity , you need the first derivative of the displacement,
for acceleration, it's the second derivative
example below
clc
clearvars
Fs = 10;
dt = 1/Fs;
t = 0:dt:5;
omega = 2*pi*0.5;
% "true" displacement / velocity / acceleration
x = sin(omega*t);
xp = omega*cos(omega*t);
xpp = -x*omega^2;
% computed velocity / acceleration
[velocity, accel] = firstsecondderivatives(t,x);
% raw data + first & second derivatives plot
figure(1),
subplot(2,1,1),plot(t,xp,'+b',t,velocity,'or');
legend('velocity (true)', 'velocity (computed)');
subplot(2,1,2),plot(t,xpp,'+b',t,accel,'or');
legend('accel(true)', 'accel (computed)');
function [dy, ddy] = firstsecondderivatives(x,y)
% The function calculates the first & second derivative of a function that is given by a set
% of points. The first derivatives at the first and last points are calculated by
% the 3 point forward and 3 point backward finite difference scheme respectively.
% The first derivatives at all the other points are calculated by the 2 point
% central approach.
% The second derivatives at the first and last points are calculated by
% the 4 point forward and 4 point backward finite difference scheme respectively.
% The second derivatives at all the other points are calculated by the 3 point
% central approach.
n = length (x);
dy = zeros;
ddy = zeros;
% Input variables:
% x: vector with the x the data points.
% y: vector with the f(x) data points.
% Output variable:
% dy: Vector with first derivative at each point.
% ddy: Vector with second derivative at each point.
dy(1) = (-3*y(1) + 4*y(2) - y(3)) / (2*(x(2) - x(1))); % First derivative
ddy(1) = (2*y(1) - 5*y(2) + 4*y(3) - y(4)) / (x(2) - x(1))^2; % Second derivative
for i = 2:n-1
dy(i) = (y(i+1) - y(i-1)) / (x(i+1) - x(i-1));
ddy(i) = (y(i-1) - 2*y(i) + y(i+1)) / (x(i-1) - x(i))^2;
end
dy(n) = (y(n-2) - 4*y(n-1) + 3*y(n)) / (2*(x(n) - x(n-1)));
ddy(n) = (-y(n-3) + 4*y(n-2) - 5*y(n-1) + 2*y(n)) / (x(n) - x(n-1))^2;
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Scott MacKenzie on 30 Apr 2021
Edited: Scott MacKenzie on 6 May 2021
I think this is potentially very simple. Let's remember, velocity is the distance traveled divided by the time taken. If you have data for the distance from home at set points in time, then calculating and plotting the distance and point-to-point velocity are straight forward:
% time points in seconds when position is measured
t = 0:10;
% distance from home at each time point
d = [0 5 12 23 37 41 57 67 72 74 79];
% compute the change in distance from each time point to the next
deltaD = diff(d);
% compute the velocity between time points
v = [0 deltaD / 1]; % NOTE: measurements are 1 second apart
% plot distance and velocity vs time
tiledlayout(2,1);
nexttile;
plot(t,d); % distance
xlabel('Time (s)', 'fontsize', 11);
ylabel('Distance from start (m)', 'fontsize', 11);
nexttile;
plot(t,v); % velocity
xlabel('Time (s)', 'fontsize', 11);
ylabel('Velocity (m/s)', 'fontsize', 11); ##### 0 CommentsShowHide -1 older comments

Sign in to comment.

R2017b

### Community Treasure Hunt

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

Start Hunting!