Integrating discrete values to determine the absolute value of displacement

조회 수: 2 (최근 30일)
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
John D'Errico 2021년 8월 16일
편집: John D'Errico 2021년 8월 16일
  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.
  댓글 수: 4
Coral
Coral 2021년 8월 17일
편집: Coral 2021년 8월 17일
Thank you so much, John. I went ahead and tested out the functions you mentioned, following the steps and trying to understand how they work. This was really informative and I really appreciate the time you've put in to not only put together the functions, but also giving a step-by-step explanation, which is much better than testing and trying random bits of code on MATLAB to try and achieve the desired results. Conceptually, it made sense but I was at a loss as to what functions to employ for this. It's all coming together now that you've explained the coding piece.
I've also just tried to apply the same code on to my dataset and I'm happy to say that it works!
Again, thank you for your help!
Edit: I'm sure there are indeed faster ways, like that function you suggested, but I'm a little old fashioned and prefer to understand what's going on before heading to the shortcuts :)
John D'Errico
John D'Errico 2021년 8월 17일
편집: John D'Errico 2021년 8월 17일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by