How do i calculate velocity and acceleration from positional data

167 views (last 30 days)
I have a data set with x, y and z coordinates. Any suggestions of setting up a script for calculating velocity and acceleration in each direction at each timestamp?

Answers (3)

Adam Danz
Adam Danz on 13 Mar 2019
Edited: Adam Danz on 25 Mar 2019
Here are suggestions as requested
  1. Use your (x,y,z) coordinates to calculate distance: pdist()
  2. Use your time stamps to calcuate duration between time stamps: diff(timestamps)
  3. Velocity is just distance/time so once you have distance, divide by the durations to get velocity.
  4. acceleration is the chnage in velocity divided by the change in time.
Resources like these are great to get you started: https://www.wikihow.com/Calculate-Acceleration
These calculations will give you the net distance, net velocity, and net acceleration. If you need to split those up into directional components in the x, y, z directions, you'll need to apply basic trig which gets a little more complicated. Your question is general so my suggestions are as well. If you get stuck with something specific you could follow up here or ask a new question.

Tsotonenome Djogbessi
Tsotonenome Djogbessi on 31 Mar 2022
how do you calculate velocity

Umar
Umar on 20 Oct 2024

Hi @jacob gandrup ,

To calculate velocity and acceleration from a dataset of x, y, and z coordinates, import the dataset containing the coordinates and timestamps.Use the finite difference method to compute the velocity in each direction. Similarly, compute the acceleration by differentiating the velocity. Plot the velocity and acceleration for better understanding. Display the computed values in a readable format. Here is a detailed MATLAB script that accomplishes these tasks:

% Sample Data Generation
% Assuming we have a time vector and corresponding x, y, z coordinates
time = linspace(0, 10, 100); % 100 timestamps from 0 to 10 seconds
x = sin(time);                % Example x-coordinates
y = cos(time);                % Example y-coordinates
z = time;                     % Example z-coordinates (linear increase)
% Combine the coordinates into a matrix
data = [time', x', y', z'];
% Calculate Velocity
% Velocity is the first derivative of position with respect to time
dt = diff(data(:, 1)); % Time differences
vx = diff(data(:, 2)) ./ dt; % Velocity in x-direction
vy = diff(data(:, 3)) ./ dt; % Velocity in y-direction
vz = diff(data(:, 4)) ./ dt; % Velocity in z-direction
% Append zeros for the first velocity values (as we lose one data point)
vx = [0; vx]; 
vy = [0; vy]; 
vz = [0; vz];
% Calculate Acceleration
% Acceleration is the first derivative of velocity with respect to time
dvx = diff(vx) ./ dt; % Acceleration in x-direction
dvy = diff(vy) ./ dt; % Acceleration in y-direction
dvz = diff(vz) ./ dt; % Acceleration in z-direction
% Append zeros for the first acceleration values
dvx = [0; dvx]; 
dvy = [0; dvy]; 
dvz = [0; dvz];
% Final Results
final_results = table(time', x', y', z', vx, vy, vz, dvx, dvy, dvz, ...
                    'VariableNames', {'Time', 'X', 'Y', 'Z', 'Vx', 'Vy', 'Vz', 
                     'Ax', 'Ay', 'Az'});
% Display the final results
disp(final_results);
% Plotting the results
figure;
% Velocity Plot
subplot(2, 1, 1);
plot(time, vx, 'r', 'DisplayName', 'Vx');
hold on;
plot(time, vy, 'g', 'DisplayName', 'Vy');
plot(time, vz, 'b', 'DisplayName', 'Vz');
title('Velocity in 3D Space');
xlabel('Time (s)');
ylabel('Velocity (units/s)');
legend show;
grid on;
% Acceleration Plot
subplot(2, 1, 2);
plot(time, dvx, 'r', 'DisplayName', 'Ax');
hold on;
plot(time, dvy, 'g', 'DisplayName', 'Ay');
plot(time, dvz, 'b', 'DisplayName', 'Az');
title('Acceleration in 3D Space');
xlabel('Time (s)');
ylabel('Acceleration (units/s^2)');
legend show;
grid on;
% Adjust layout
sgtitle('Velocity and Acceleration Analysis');

Please see attached.

The above MATLAB script helps with your approach to calculate and visualizing velocity and acceleration from a dataset of 3D coordinates. By following the outlined steps and utilizing the provided code, you can effectively analyze motion in three-dimensional space. Feel free to modify the data input section to suit your specific dataset.

Hope this helps.

Please let me know if you have any further questions.

Community Treasure Hunt

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

Start Hunting!