# implement a matlab function that takes time , and as inputs and returns acceleration, , velocity, , and displacement, in matlab

Here's a simple implementation of the required function:

main.m
function [a,v,d] = calc_motion(t, y)
% Calculates acceleration, velocity, and displacement for given time and displacement vectors

n = length(t); % get number of time steps

dt = diff(t); % calculate time intervals (delta t)
dt = [dt(1), dt]; % add a value for first time interval

dv = diff(y)./dt; % calculate velocity change (delta v)
dv = [dv(1), dv]; % add a value for first velocity

da = diff(dv)./dt; % calculate acceleration change (delta a)
da = [da(1), da]; % add a value for first acceleration

v = cumsum(dv.*dt); % calculate velocity by integrating acceleration
a = cumsum(da.*dt); % calculate acceleration by integrating velocity
d = cumsum(v.*dt); % calculate displacement by integrating velocity
end
774 chars
19 lines

To use this function, call it with your vectors of time t and displacement y as inputs:

main.m
t = [0, 1, 2, 3, 4, 5]; % time intervals (in seconds)
y = [0, 1, 3, 6, 10, 15]; % displacement (in meters)

[a, v, d] = calc_motion(t, y); % call the function to calculate acceleration, velocity, and displacement
213 chars
5 lines

The output variables a, v, and d will contain the calculated values for acceleration, velocity, and displacement, respectively.

### related categories

gistlibby LogSnag