Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 946a561

Browse files
author
Jirka Hladky
committed
Added the trace plot of the end point
1 parent b7eb868 commit 946a561

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

examples/animation/double_pendulum.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
import matplotlib.pyplot as plt
1515
import scipy.integrate as integrate
1616
import matplotlib.animation as animation
17+
from collections import deque
1718

1819
G = 9.8 # acceleration due to gravity, in m/s^2
1920
L1 = 1.0 # length of pendulum 1 in m
2021
L2 = 1.0 # length of pendulum 2 in m
2122
M1 = 1.0 # mass of pendulum 1 in kg
2223
M2 = 1.0 # mass of pendulum 2 in kg
23-
t_stop = 5 # how many seconds to simulate
24+
t_stop = 50 # how many seconds to simulate
25+
history_len = 500 #how many trace points to display (trail history)
2426

2527

2628
def derivs(state, t):
@@ -47,8 +49,8 @@ def derivs(state, t):
4749

4850
return dydx
4951

50-
# create a time array from 0..100 sampled at 0.05 second steps
51-
dt = 0.05
52+
# create a time array from 0..t_stop sampled at 0.02 second steps
53+
dt = 0.02
5254
t = np.arange(0, t_stop, dt)
5355

5456
# th1 and th2 are the initial angles (degrees)
@@ -71,22 +73,28 @@ def derivs(state, t):
7173
y2 = -L2*cos(y[:, 2]) + y1
7274

7375
fig = plt.figure(figsize=(5, 4))
74-
ax = fig.add_subplot(autoscale_on=False, xlim=(-2, 2), ylim=(-2, 1))
76+
ax = fig.add_subplot(autoscale_on=False, xlim=(-(L1+L2), (L1+L2)), ylim=(-(L1+L2), 1))
7577
ax.set_aspect('equal')
7678
ax.grid()
7779

7880
line, = ax.plot([], [], 'o-', lw=2)
81+
trace, = ax.plot([], [], ',-', lw=1)
7982
time_template = 'time = %.1fs'
8083
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
84+
history_x, history_y = deque(maxlen=history_len), deque(maxlen=history_len)
8185

8286

8387
def animate(i):
8488
thisx = [0, x1[i], x2[i]]
8589
thisy = [0, y1[i], y2[i]]
8690

91+
history_x.appendleft(thisx[2])
92+
history_y.appendleft(thisy[2])
93+
8794
line.set_data(thisx, thisy)
95+
trace.set_data(history_x, history_y)
8896
time_text.set_text(time_template % (i*dt))
89-
return line, time_text
97+
return line, trace, time_text
9098

9199

92100
ani = animation.FuncAnimation(

0 commit comments

Comments
 (0)