r/Python • u/MisterBlggs gitlab.com/MisterBiggs • Jul 26 '19
Double Pendulum made with Processing.py
https://gfycat.com/feistycompetentgarpike27
u/JackdeAlltrades Jul 26 '19
16
u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19
It goes on forever so I had to end it somewhere!
6
2
2
15
u/pa7x1 Jul 26 '19
Very cool! If you are interested you could do some simulations to measure the Lyapunov exponent that demonstrate the chaotic behavior of the double pendulum.
Select a starting point and fix a simulation time (the longer the better but keep it manageable for your computation). Simulate the double pendulum and record its position at time t. Then rinse and repeat with slight changes of the initial conditions. You should observe that very small changes of the initial conditions result in very different positions at time t.
8
u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19
I'm taking Dynamics next semester and the end of the class is a double pendulum simulation. Apparently it's pretty in depth so I'll probably post on this sub again if I manage to do it in Python instead of Matlab like the rest of my University.
6
Jul 26 '19
Holy crap universities are still using Matlab? I haven't used it once, not even octave, outside of a university in 2010. But then I'm not so much with the science.
3
u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19
Apparently it's huge in aerospace engineering and I believe space physics uses it heavily too. I use Python every chance I get but unfortunately for most programming assignments either the professor requires Matlab since that's all they know how to grade, or it's a group project and I'm the only person that knows any programming besides Matlab.
4
u/porcelain_robots Jul 26 '19
Can you ELI5 how the bottom part can swing higher than its orginial release point? What's the physics behind this?
4
u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19
I'm probably not the best person to answer it, but one important thing to note about this particular simulation is that there's no loss of energy.
If you look when it goes higher than the starting position the radius is smaller than when it started. So, the first ball didn't swing as high but the second was able to flip around since it has some of the energy that the first ball originally had.
2
Jul 27 '19 edited Jul 27 '19
[removed] — view removed comment
1
u/porcelain_robots Jul 29 '19
My understanding is that energy can be conserved if it's a smaller radius for the centripetal motion.
1
Jul 29 '19
[removed] — view removed comment
1
u/porcelain_robots Jul 30 '19
Yeah, you're right. There's no way they can both swing higher than their original point at the same time.
2
Jul 26 '19
Is Processing.py a Processing plugin for Python or a Python plugin for Processing?
2
u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19
According to the getting started tutorial its a mode for the processing IDE. I think that would make it a Python plugin for Processing? It still does everything in Java in the background.
1
2
u/thekalmanfilter Jul 27 '19
So this is the output that shows up in the console?
1
u/MisterBlggs gitlab.com/MisterBiggs Jul 27 '19
Processing has its own editor and it opens a window with the animation similar to how matplotlib works when you use it to plot something.
To get the video I saved each frame individually and put them all together using Blender.
1
u/thekalmanfilter Jul 27 '19
THANK YOU for the insight! I always wonder how people do animations or programs that track movement on video “using python” when python is just lined of code on a screen. I’m also a bit dyslexic/ autistic and take things very literally so as a beginner (barely started learning) I have trouble understanding how we move from lines on a screen to actual pictures and videos etc. Thanks again!
75
10
9
u/ThortheAssGuardian Jul 26 '19
Very cool. Makes me wonder if you could model such a pendulum in 3D and use the line tracing as a model for a crazy looking 3D printed fruit bowl.
7
Jul 26 '19
Well, the problem with a double pendulum is that only the inner mass stays on a circular path (in 2D, or a bowl in 3D). The outer mass will move pretty randomly throughout the volume of the bowl, so you might not be able to put anything into it.
This can be somewhat solved by making the inner pendulum longer than the outer one, but it's still gonna be a hard print because 3d printers cannot print random loops in the air, every layer has to be supported by the one below it.
Still, it's a cool idea and I might try to do it later this summer :)
5
u/est94 Jul 26 '19 edited Jul 26 '19
Ok, hear me out: what if you modeled this two pendulum system, but had pendulum 2 tethered to a frictionless sphere/ellipsoid? So in 2D it would look exactly like OP’s gif, but in 3D it would create a useable bowl? You would just calculate the 3D coordinates of pendulum 2 using the geometric constraints. Maybe that would force the length of the line between P1 and P2 to be variable.
Edit: in my idea, pendulum 1 (P1) keeps its 2D constraints, and pendulum 2(P2) can move freely in 3D, but angle P1-P2 must be between 90° and 270°. So theoretically P2 would move along a tire-shaped hollow toroid. In reality it would create a real cool, wild looking bowl. I think.
Edit 2: the shape would only be a weird toroid if you kept the length of P2 constant. If it was variable, you could have it travel within a sphere, always tangent to the inner surface of the sphere.
4
Jul 26 '19 edited Jul 26 '19
Yes, that's a nice way to get a working model. I'm not sure if the resulting movement would be 'cool', though.
Like in the 2D case, if you constrained the outer mass to stay on a circular path, then it would just move back and forth like a regular pendulum.
The 3D case has more degrees of freedom, but the behaviour might be similar.
edit: I missed your edit while writing out my replay. I really like the idea to constrain the angle of P2!
A constant length makes for a way easier simulation, but the second option with variable length could basically be solved with a projection from P2 to the sphere afterwards. Really cool!
4
Jul 26 '19
2
1
1
u/3nigma42 Jul 26 '19
Nice work, did you include any type of friction
1
u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19
Nope. I just used the same equation as Daniel Shiffmans video. I could be lazy and just multiply the velocity of the pendulum by .999 or something every frame so it would eventually stop but that's just making a random guess so I just left it frictionless.
1
u/-walter-sobchak- Jul 26 '19
Why doesn’t it ever come to rest?
2
u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19
This is using code to simulate the double pendulum with gravity. It doesn't account for any air resistance or anything like that so the system never loses energy.
1
Jul 26 '19
As someone that teaches processing.py I want to say how much I hate processing.py. 😂
1
u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19
I think my only complaint would be the editor, but I didn't get that deep into it.
1
Jul 26 '19
It’s actually not python, it’s python syntax that gets converted into java on the backend.
1
u/_sachin_reddy_ Jul 27 '19
Would've been more satisfying if it drew something symmetric. Good job though. Guess things don't tend to be symmetric always.
3
u/MisterBlggs gitlab.com/MisterBiggs Jul 27 '19
One of the cool things about the double pendulum is that it's chaotic, which is kinda the opposite of satisfying but very interesting in its own right.
3
Jul 26 '19
Huge fan of processing.org. I used it in college in my art degree over a decade ago. Super excited to play with this, thanks for sharing!
3
3
u/poditoo Jul 27 '19
I am the only one who gets too excited when the second pendulum makes a full loop?
It's like watching soccer, you see the angle of attack is right, the speed seems ok and it's getting close and SCOOOOOOOOOOORE.
Edit oh and none of the players are overpaid.
5
u/Thesludger Jul 26 '19
Pretty impressive. I wish I could create stuff like that as well
18
1
u/mahmud41 Jul 26 '19
curious to know if it is following actual physics. Anyone else?
0
Jul 26 '19
[deleted]
2
u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19
If you watch Daniel Shiffmans video this is based on a formula that someone else derived, and it doesn't account for any loss of energy so it's definitely not a great simulation but it gets the idea across and it's pretty.
2
2
2
2
2
u/BrandNewThanos Jul 27 '19
I was interested in making designs using Processing, but I didn't know java. But thanks for letting us know about processing.py, I will be able to do it via python.
2
u/TimeTraveller-San Jul 26 '19
Reminds me of chaos theory
8
Jul 26 '19
Isn't a two pendulum system chaotic?
8
u/PhyzzyKitty Jul 26 '19
It is
2
Jul 26 '19
I was gonna say that... reminded me of Jurassic park too lol
5
u/PhyzzyKitty Jul 26 '19
The key to time travel may be the inverted Mobius strip, but cloning dinosaurs? That's double pendulum.
2
3
1
1
1
1
1
u/Silencer306 Jul 26 '19
This is interesting. I’d be interested in knowing if you can do this with more pendulums.
1
1
1
1
1
u/jimflann Jul 27 '19
What's that sub where videos end too early? I was hoping to see it create a perfect curve. Very neat, kudos!
1
1
u/FlowingLiquidity Oct 08 '19
Shiffman's tutorials are the best. I made the double pendulum swing yesterday. However, I'm trying to build in an SVG export so I can draw the created drawings on my DIY pen plotter but sadly I cannot find a way to create the full rendered image. My svg export only keeps exporting a single frame of the animation.
1
1
1
1
-1
0
0
-1
100
u/MisterBlggs gitlab.com/MisterBiggs Jul 26 '19 edited Jul 26 '19
I noticed Processing had an interface for Python: Processing.py. So, I figured I would try to port Daniel Shiffman from the Coding Trains Double Pendulum Code over to Python and it was ridiculously easy. I'm surprised I hadn't heard of the Python version of Processing so I figured I would share with Reddit.
Python code here