Skip to content

Commit 07150f8

Browse files
author
Jonas Mrazek
committed
clean up code, add example init-states
1 parent 45d48ee commit 07150f8

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

double_pendulum.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@
2121
M1 = 1.0 # mass of pendulum 1 in kg
2222
M2 = 1.0 # mass of pendulum 2 in kg
2323

24+
# th1 and th2 are the initial angles (degrees)
25+
# w10 and w20 are the initial angular velocities (degrees per second)
26+
27+
# th1(second pendulum), th2 (first pendulum), th1(second pendulum), th2 (second pendulum)
28+
init_tuple = (120.0, 105.0, 120.0000001, 105.0)
29+
#init_tuple = (180.0000001, 180.0, -180.0000001, 180.0)
30+
#init_tuple = (180.0, 90.0000001, 180.0, 90.0)
31+
#init_tuple = (80.0000001, 42.0, 80.0, 42.0)
32+
#init_tuple = (120.0, -90.0, -120.0, 90.0)
33+
34+
th1 = init_tuple[0]
35+
w1 = 0.0
36+
th2 = init_tuple[1]
37+
w2 = 0.0
38+
39+
th1_ = init_tuple[2]
40+
w1_ = 0.0
41+
th2_ = init_tuple[3]
42+
w2_ = 0.0
43+
44+
# create a time array sampled at dt second steps
45+
# duration given in seconds
46+
duration = 100
47+
dt = 0.02
48+
t = np.arange(0, duration, dt)
49+
50+
#defines the number of samples fo which the drawn path should persist (None means forever)
51+
PATH_RANGE = None
52+
2453

2554
def derivs(state, t):
2655

@@ -46,25 +75,6 @@ def derivs(state, t):
4675

4776
return dydx
4877

49-
# create a time array sampled at dt second steps
50-
duration = 200
51-
dt = 0.02
52-
t = np.arange(0, duration, dt)
53-
54-
# th1 and th2 are the initial angles (degrees)
55-
# w10 and w20 are the initial angular velocities (degrees per second)
56-
th1 = 80.0000001
57-
w1 = 0.0
58-
th2 = 42.0000000
59-
w2 = 0.0
60-
61-
th1_ = 80.0000000
62-
w1_ = 0.0
63-
th2_ = 42.0
64-
w2_ = 0.0
65-
66-
PATH_RANGE = None
67-
6878
# initial state
6979
state = np.radians([th1, w1, th2, w2])
7080
state_ = np.radians([th1_, w1_, th2_, w2_])
@@ -83,7 +93,7 @@ def derivs(state, t):
8393
x2_ = L2*sin(y_[:, 2]) + x1_
8494
y2_ = -L2*cos(y_[:, 2]) + y1_
8595

86-
fig = plt.figure(figsize=(11,11))
96+
fig = plt.figure()
8797
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2, 2), ylim=(-2, 2))
8898
ax.set_aspect('equal')
8999
ax.grid()
@@ -102,7 +112,7 @@ def derivs(state, t):
102112
"$\Theta_{12}:=%.7f\degree$ (orange)\n"
103113
"$\Theta_{22}:=%.7f\degree$ (orange)"
104114
%(th1,th2,th1_,th2_))
105-
init_text = ax.text(0.05, 0.78, init_template, bbox=dict(boxstyle="round", fc="white"),size='smaller',transform=ax.transAxes)
115+
init_text = ax.text(0.05, 0.68, init_template, bbox=dict(boxstyle="round", fc="white"),size='smaller',transform=ax.transAxes)
106116

107117
pause = False
108118
def onClick(event):
@@ -153,4 +163,4 @@ def animate(i):
153163

154164
# saving animation
155165
# takes a whila and ffmpeg needs to be installed
156-
#ani.save("double_pendulum_4.mp4")
166+
#ani.save("double_pendulum_4.mp4")

0 commit comments

Comments
 (0)