Aug-08-2018, 05:06 PM
(This post was last modified: Aug-08-2018, 05:06 PM by Quantum_Theorist.)
I haven't been programming for a long time, but, I am working on a project where I am animating a Simple Pendulum, but for some reason, only a picture of where the pendulum starts is output, as well as an error. Here's my code:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
l, m = 1, 1
g = 9.81
def deriv(y, t, l, m):
theta, z = y
thetaDot = z
zDot = -g/l*np.sin(theta)
return zDot, thetaDot
def calc_E(y):
thD = y
V = m*g*y
T = 1/2*(l**2)*(thD**2)
return V + T
tmax, dt = 30, 1e-4
t = np.arange(0, tmax + dt, dt)
y0 = np.array([3*np.pi/7, 0]) # Initial conditions for theta and thetaDot
y = odeint(deriv, y0, t, args=(l, m)) # Numerically integrate to find solution to differential equation
theta = y[:, 0]
# Next: Convert to Cartesian coordinates (bob positions)
x = l*np.sin(theta)
y = -l*np.cos(theta)
r = 0.07 # Radius of bob circle
fps = 15
di = int(1/fps/dt)
fig = plt.figure(figsize=(8.3333, 6.25), dpi=72)
ax = fig.add_subplot(111)
def make_plot(i):
ax.plot([0, x[i], 0], [0, y[i], 0], lw = 2, c='k')
c0 = Circle((0, 0), r/2, fc='k', zorder=10)
c1 = Circle((x[i], y[i]), r, fc='b', ec='b', zorder=10)
ax.add_patch(c0)
ax.add_patch(c1)
ax.set_xlim(-1.5*l + r, 1.5*l + r)
ax.set_ylim(-1.5*l + r, 1.5*l + r)
ax.set_aspect('equal', adjustable='box')
plt.axis('off')
plt.savefig('frames/_img{:04d}.png'.format(i//di), dpi=72)
plt.cla()
for i in range(0, t.size, di):
print(i // di, '/', t.size // di)
make_plot(i)
