Skip to content

Commit 20bdd96

Browse files
anntzertimhoffm
authored andcommitted
Minor cleanup to double_pendulum example. (#12986)
The formulas are of the kind where I think that putting +/- at the beginning of the lines instead of at the end really improves readability.
1 parent 0583079 commit 20bdd96

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

examples/animation/double_pendulum_sgskip.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,28 @@ def derivs(state, t):
2727
dydx = np.zeros_like(state)
2828
dydx[0] = state[1]
2929

30-
del_ = state[2] - state[0]
31-
den1 = (M1 + M2)*L1 - M2*L1*cos(del_)*cos(del_)
32-
dydx[1] = (M2*L1*state[1]*state[1]*sin(del_)*cos(del_) +
33-
M2*G*sin(state[2])*cos(del_) +
34-
M2*L2*state[3]*state[3]*sin(del_) -
35-
(M1 + M2)*G*sin(state[0]))/den1
30+
delta = state[2] - state[0]
31+
den1 = (M1+M2) * L1 - M2 * L1 * cos(delta) * cos(delta)
32+
dydx[1] = ((M2 * L1 * state[1] * state[1] * sin(delta) * cos(delta)
33+
+ M2 * G * sin(state[2]) * cos(delta)
34+
+ M2 * L2 * state[3] * state[3] * sin(delta)
35+
- (M1+M2) * G * sin(state[0]))
36+
/ den1)
3637

3738
dydx[2] = state[3]
3839

39-
den2 = (L2/L1)*den1
40-
dydx[3] = (-M2*L2*state[3]*state[3]*sin(del_)*cos(del_) +
41-
(M1 + M2)*G*sin(state[0])*cos(del_) -
42-
(M1 + M2)*L1*state[1]*state[1]*sin(del_) -
43-
(M1 + M2)*G*sin(state[2]))/den2
40+
den2 = (L2/L1) * den1
41+
dydx[3] = ((- M2 * L2 * state[3] * state[3] * sin(delta) * cos(delta)
42+
+ (M1+M2) * G * sin(state[0]) * cos(delta)
43+
- (M1+M2) * L1 * state[1] * state[1] * sin(delta)
44+
- (M1+M2) * G * sin(state[2]))
45+
/ den2)
4446

4547
return dydx
4648

4749
# create a time array from 0..100 sampled at 0.05 second steps
4850
dt = 0.05
49-
t = np.arange(0.0, 20, dt)
51+
t = np.arange(0, 20, dt)
5052

5153
# th1 and th2 are the initial angles (degrees)
5254
# w10 and w20 are the initial angular velocities (degrees per second)
@@ -91,7 +93,7 @@ def animate(i):
9193
time_text.set_text(time_template % (i*dt))
9294
return line, time_text
9395

94-
ani = animation.FuncAnimation(fig, animate, np.arange(1, len(y)),
95-
interval=25, blit=True, init_func=init)
9696

97+
ani = animation.FuncAnimation(fig, animate, range(1, len(y)),
98+
interval=25, blit=True, init_func=init)
9799
plt.show()

0 commit comments

Comments
 (0)