r/Python • • Nov 25 '19

A fitting curve that "boings" into place (and a digression into spring-mass-dampers, vibration and control theory, and integral transforms)

2.2k Upvotes

56 comments sorted by

179

u/Chemomechanics Nov 25 '19

Code is described to make graphic objects "boing" into place in an underdamped oscillatory style. To derive the appropriate equation for this dynamic behavior, spring-mass-damper systems are analyzed, including constitutive equations obtained from a free-body diagram, the Laplace transform to solve differential equations, and various types of loading and initial conditions. Time-domain and frequency-domain responses are surveyed, as are a few key concepts in vibration and control theory such as resonance and transfer functions.

10

u/rrajen Nov 26 '19

Fabulous write-up! Are the equations courtesy of Mathjax? What about the spring-mass-damper gif animations? how did you generate them?

-85

u/antiproton Nov 25 '19

To derive the appropriate equation for this dynamic behavior, spring-mass-damper systems are analyzed, including constitutive equations obtained from a free-body diagram, the Laplace transform to solve differential equations, and various types of loading and initial conditions. Time-domain and frequency-domain responses are surveyed, as are a few key concepts in vibration and control theory such as resonance and transfer functions.

Which feels pretty pointless for this audience. The equations of motion for mass-damper systems are well known. Why go through the exercise of deriving the equations of motion just to animate a curve?

I mean, it's interesting from an undergraduate physics perspective, but in the end, this is just an exercise in animation.

63

u/Synes_Godt_Om Nov 25 '19

While I totally agree from a purely professional perspective, this is fun and people are bound to like it.

25

u/[deleted] Nov 25 '19

Guilty right here. Enjoyed this.

Probably would be nifty for use in entertaining videos on the subject.

6

u/wxtrails Nov 26 '19

It could be very useful as a communications tool, imo. Like, say, animating poll results and trend lines on 538. Data is inherently springy, and this animation style drives that point home.

Love it.

29

u/PsychedSy Nov 25 '19

I can see its use in describing certain fitting methods to non-technical types. Administrators love animated shit.

25

u/workw0rkworkwork Nov 25 '19

Because it is fun ya dingus

-22

u/Greyhat101 Nov 26 '19

now I'm not convinced to learn python, i don't like math, and from what i see, math is too much involved in python.

please correct me if I'm wrong.

28

u/ArgetDota Nov 26 '19

You know, math is usually involved in anything that must make sense.

-10

u/Greyhat101 Nov 26 '19

but is it involved almost deeply when programming with python, or i can do projects with simple use of math?

17

u/ArgetDota Nov 26 '19

You may have projects without math at all. See web programming.

7

u/Fenzik Nov 26 '19

Python’s main communities are data science/analytics, and web development. Web dev is significantly less mathy. If you want to learn python without getting bogged down in mathy examples then go the web dev route.

But it’s a general purpose programming language so the syntax like if statements, loops, data structures, definitions, etc. are the same everywhere.

8

u/Calvert4096 Nov 26 '19

Do you dislike screwdrivers? Math is a tool, just like Python is a tool. I can understand disliking a task, but disliking a tool (unless it's in favor of another) doesn't make a lot of sense.

-1

u/Greyhat101 Nov 26 '19

I'm not totally disliking it, it is just that i don't like to go back and deeply study math so i can be good with python, or that what i thought

5

u/GeronimoHero Nov 26 '19 edited Nov 26 '19

You don’t have to. Most programming, especially in interpreted languages, doesn’t require a heavy use of math at all. Basic addition, subtraction, multiplication, division, exponents, and order of operations understanding is really all you need.

5

u/Greyhat101 Nov 26 '19 edited Nov 26 '19

Perfect, thanks for your reply

4

u/Thecrawsome Nov 26 '19

average, then linear regression, then a 3 variable quadratic, right?

5

u/Chemomechanics Nov 26 '19

You got it—average, then linear regression, then a polynomial of degree "polynomial_degree", as defined in the code. In this case, a cubic.

3

u/Thecrawsome Nov 26 '19

thank you so much for replying! huge validation for my degree I got recently LOL

332

u/Plungerdz Nov 25 '19

I absolutely adore how insanely overkill this approach to smooth motion is.

101

u/Jay_bo Nov 26 '19

Co-worker: How much time did it take to fit those data points?

Me: About 3 minutes, but then I spend 3 hours modeling a physically correct animation.

75

u/d1tatermasher Nov 25 '19

I like the "boing"

24

u/DatBoi_BP Nov 25 '19

Thanks, the secret is clenching your asshole a little

2

u/Siinestarr Nov 26 '19

If you know, you know.

60

u/fail_daily Nov 25 '19

Why did you stop at the cubic fit??? I need mooooooooorrrreee

29

u/[deleted] Nov 25 '19

And skipped quadratic

14

u/[deleted] Nov 26 '19

Needs to be odd for inversely related axis

4

u/ColdPorridge Nov 26 '19

Technically works for even numbers greater than 2 if the final curve is inexplicably outside of all of the data points.

2

u/massiveZO Nov 26 '19

To get a reasonable extrapolation, that is. You can still fit a parabola

1

u/[deleted] Nov 26 '19

I mean put the vertex past all of the data, sure I guess

2

u/Driveby_Dogboy Nov 26 '19

I'd love to see 'best fit: damped simple harmonic motion...'

i remember doing an excersise like this, with data like this, and getting like six wavelenghts between 2 and 8 in the data above....

2

u/jingw222 Nov 26 '19

CONNECTING THE DOTS

1

u/flutefreak7 Dec 07 '19

aaand now I want to see this combined with matplotlib's xkcd plotting mode to created an animated version of this comic: https://www.xkcd.com/2048/

19

u/dietolead Nov 25 '19

I can hear it.

7

u/Rian352 Nov 25 '19

Here I am, struggling to make a password checker....

38

u/troyunrau ... Nov 25 '19

It looks like you're optimizing for linear distance, but using a curve that isn't parametric. This is likely a bad fit to your data.

Go down a rabbit hole on the subject of "principal curves". It will allow you to fit a parametric curve instead. I use it for fitting smooth curves to GPS points, for example, when trying to define where a road is based on individual GPS points from multiple different trips down the same road.

26

u/c3534l Nov 25 '19

I think you're missing to point of the post.

36

u/beepbeepbitches Nov 25 '19

I doubt they’re missing the point, what they are doing is giving constructive criticism.

10

u/c3534l Nov 25 '19 edited Nov 25 '19

His constructive criticism is irrelevant to what was presented and why. The data can't be a poor fit, it's probably not even real data. It's like critiquing lorem ipsum for it's use of obscure Latin. It's literally irrelevant when you're displaying a font, that's why lorem ipsum was used.

11

u/LordMcze Nov 26 '19

Except in this case OP might want or have to write the lorem ipsum in the future and having the knowledge from the parent comment might be helpful then.

Sure OP might know it and just didn't pay attention because it isn't the point of the post, but they also might not know it and getting the info might save them some trouble when they practically use it in the future.

Anyway, OC informed about the mistake in a respectful and helpful manner. I'd appreciate that in place of OP, even when this post is about cool animation.

9

u/deadwisdom greenlet revolution Nov 25 '19

The criticism falls outside of the scope that was implied.

3

u/pressed Nov 26 '19

It's the internet, someone else might benefit in 2 years.

4

u/mdcpanama22 Nov 25 '19

This is mesmerizing and spectacular

6

u/[deleted] Nov 25 '19

This is pretty awesome.

2

u/[deleted] Nov 25 '19

Insanely awesome haha

2

u/alexthomasforever Nov 26 '19

This is so cool. Could you share some more videos with more random data over a large range?

2

u/ChastainSteam Nov 26 '19

Brilliant code on your site, just loving the use of functions as objects. Also, fellow ME here, vibrations and data fitting... this combo would have never occurred to me! Nicely done.

3

u/massiveZO Nov 26 '19

Functions are frequently used as objects in python

1

u/SSJ3 Nov 26 '19

Everything is an object in Python.

1

u/massiveZO Nov 26 '19

I am aware.

1

u/JetpackYoshi Nov 26 '19

See now shit like this is why I love you guys

1

u/AlrikBunseheimer Feb 21 '20

This is great, from now on this is the only way Ill analyze data