Python Forum
Simple Pendulum Animation Errors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Pendulum Animation Errors
#1
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  phase portrait - Nonlinear damped pendulum Giovanni_62 1 864 Sep-10-2023, 02:25 PM
Last Post: deanhystad
  double pendulum help. (matplotlib) FramedLink 1 3,008 Nov-08-2018, 11:46 AM
Last Post: FramedLink
  Simple pendulum with variable length Gaelou 1 3,378 Aug-30-2018, 04:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020