Skip to content

Instantly share code, notes, and snippets.

@Nikolaj-K
Last active August 1, 2021 23:27
Show Gist options
  • Save Nikolaj-K/87a179be4e64a9eb2d94262f92b2cb66 to your computer and use it in GitHub Desktop.
Save Nikolaj-K/87a179be4e64a9eb2d94262f92b2cb66 to your computer and use it in GitHub Desktop.
How to compute velocity and acceleration from a stream of points
"""
Code discussed in this video:
https://youtu.be/qA58ggk9fPA
And here's the links mentioned:
Torque-free motion gif's:
Integrated motion:
https://gfycat.com/costlyhardtofinddolphin &
https://gfycat.com/warmblindbear
Like in the video:
https://gfycat.com/commonshorttermaegeancat
https://gfycat.com/wanannualfeline
https://gfycat.com/deadhopefuldogwoodtwigborer
https://gfycat.com/baggydownrightbongo
Johnsons animation:
https://youtu.be/s9wiRjUKctU
Sperical pendulum gif's:
https://gfycat.com/highscalyhogget
https://gfycat.com/bleakcarefulindianspinyloach
Wikipedia:
https://en.wikipedia.org/wiki/Moment_of_inertia#Angular_momentum_2
https://en.wikipedia.org/wiki/Finite_difference
"""
def finite_backward_difference(ys, h):
# https://en.wikipedia.org/wiki/Finite_difference
assert h < len(ys), f"{h}, {len(ys)}"
FRONT_IDX = -1
end_idx = FRONT_IDX - h
y_front = ys[FRONT_IDX]
y_end = ys[end_idx]
dy = np.array(y_front) - y_end
return dy
def backward_derivatrive(ys, h):
dy = finite_backward_difference(ys, h)
return dy / h
def backward_second_derivatrive(ys, h):
assert 2 * h < len(ys), f"{2 * h}, {len(ys)}"
# Take the best available two backwards first derivatives which are h apart
# (those are (-1) to (-1)-h resp. (-1)-h to (-1)-2*h
dy_front = backward_derivatrive(ys, h)
dy_end = backward_derivatrive(ys[:-h], h)
padding = (h - 1) * [None]
dys = [dy_end] + padding + [dy_front]
return backward_derivatrive(dys, h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment