How do i calculate velocity and acceleration from positional data

조회 수: 161 (최근 30일)
jacob gandrup
jacob gandrup 2019년 3월 13일
답변: Umar 2024년 10월 20일
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?

답변 (3개)

Adam Danz
Adam Danz 2019년 3월 13일
편집: Adam Danz 2019년 3월 25일
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 2022년 3월 31일
how do you calculate velocity

Umar
Umar 2024년 10월 20일

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.

카테고리

Help CenterFile Exchange에서 Assembly에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by