# Integrating discrete values to determine the absolute value of displacement

7 views (last 30 days)
Coral on 16 Aug 2021
Edited: John D'Errico on 17 Aug 2021
I have a velocity dataset [stored in 700 x 1 double] that i want to get the total displacement of (eg: I don't want net displacement. For total displacement, any negative displacement I would flip it to positive values). Normally, I would use cumtrapz again to gain the displacement, however any positive or negative displacements would cancel each other out. I don't want that.
I know that the area under the velocity curve is supposed to yield the total displacement. What would be the best way to code it such that the negative area is captured as positive area? Please note that I do not have points that are exactly zero (so point 1 may be +0.7 and point 2 may be -0.5, but it does not have a point in the discrete data which shows 0). I cannot simply just make the y outputs as all absolute value, because integrating between +0.7 and +0.5 yields a different area than +0.7 and -0.5. Any help would be greatly appreciated. John D'Errico on 16 Aug 2021
Edited: John D'Errico on 16 Aug 2021
1. Find any consecutive pair of points that change the sign of their velocity.
2. Between such a pair, use (linear) interpolation to locate the time where the velocity was zero.
3. Insert new points at those zeros.
4. Now take the absolute value of all velocities in the new expanded vector of velocities.
5. Use trapz or cumtrapz (as desired) to integrate velocity.
Break "large" problems down into small, managable peices. Then solve each one, and put it all together. A large problem is defined as any problem that is too difficult for you to solve easily.
John D'Errico on 17 Aug 2021
I'll concede the use of sort there may not have been obvious. :) But once you see it done, it should make sense.
The other line of code that may not be completely obvious is the interpolation step. That comes from a line segment connecting two known points, where you know the line segment crosses y==0. Then find the location on the x-axis where the segment must have crossed the axis. As I did, just draw what is happening on paper, write the corresponding equation for the line, then do some algebra. That formula falls out. In fact, the formula applies for any two endpoints on a line, as long as the line is not horizontal. They need not be on opposite sides of the axis, but then the point of intersection will not be between the endpoints on the segment.
I guess I could have derived the formula, given two points (x1,y1), (x2,y2). The equation of that line goes back to some ancient algebra class I must have taken:
syms x1 y1 x2 y2 x y
lineeq = (y - y1) == (y2 - y1)/(x2-x1)*(x - x1)
lineeq = xsol = solve(subs(lineeq,y,0),x,'returnconditions',true)
xsol = struct with fields:
x: [1×1 sym] parameters: [1×0 sym] conditions: [1×1 sym]
xsol.x
ans = xsol.conditions
ans = In that solution, we see the formula I gave, as well as the requirement that y1~=y2, otherwise the line is horizontal and never crosses the axis at all.