Double pendulumΒΆ
This example uses the sequence() and subdraw() animations to animate plot plot objects.
This example is a Diplotocus implementation of this animation from the matplotlib docs.
We first define a function get_pendulum_positions() that returns the double pendulum nodes positions for a range of times.
Then we generate the positions, create a plot() object p that displays the double pendulum and another plot() object trace that displays the last 5 positions of its end node:
import diplotocus as dpl
G = 9.8 # acceleration due to gravity, in m/s^2
L1 = 1.0 # length of pendulum 1 in m
L2 = 1.0 # length of pendulum 2 in m
L = L1 + L2 # maximal length of the combined pendulum
M1 = 1.0 # mass of pendulum 1 in kg
M2 = 1.0 # mass of pendulum 2 in kg
t_stop = 10 # how many seconds to simulate
x0,x1,x2,y0,y1,y2 = get_pendulum_positions(G,L1,L2,M1,M2,t_stop)
xs = np.stack((x0,x1,x2),axis=-1)
ys = np.stack((y0,y1,y2),axis=-1)
tl = dpl.Timeline(xlim=(-L,L),ylim=(-L,1))
p = dpl.plot(xs,ys,marker='o',lw=2,ls='-',zorder=2)
p.sequence(600)
trace = dpl.plot(x2,y2,lw=4,ls='-',zorder=1)
trace.subdraw(-5,0,600)
tl.animate((p,trace))
tl.save_video('../../_static/examples/pendulum.mp4')