How to find a velocity when you have time and position values in an array?
13 views (last 30 days)
Roger Stafford on 21 Feb 2018
Edited: Roger Stafford on 21 Feb 2018
Let x and y be row vectors of the same length where x gives successive values of the independent variable and y the corresponding dependent variable values - in this case x values are times and y values distances. It is not necessary that x values be equally-spaced. To get a second order approximation of the derivative at each point, do this:
x1 = x([3,1:end-1]); x2 = x([2:end,end-2]);
y1 = y([3,1:end-1]); y2 = y([2:end,end-2]);
dydx = ((y2-y).*(x-x1).^2+(y-y1).*(x2-x).^2)./((x2-x).*(x-x1).*(x2-x1));
The row vector dydx will give approximate values of the derivative of y with respect to x. This is a vectorized solution - no for loops or while loops are necessary.