0

The problem I am simulating is a simple pendulum. While I have done it before using PyGame I now decided to use matplotlib's animation tools. It is working but not with the desired effect. Simulating it in real time seems to be working. I have tweeked the interval and amount of frames but the fps is way too low. How do you increase the fps while still playing it in real time. I would greatly appreciate it. Anyway here is my code:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

g = 9.80665
L = 2
mu = 0.1
t = 100
theta_0 = np.pi/3
d_theta_0 = 0

def get_d2_theta(theta,d_theta):
    return -mu*d_theta-(g/L)*np.sin(theta)

def theta(t):
    theta = theta_0
    d_theta = d_theta_0
    delta_t = 1./60
    for time in np.arange(0,t,delta_t): 
        d2_theta = get_d2_theta(theta,d_theta)
        theta += d_theta*delta_t
        d_theta += d2_theta*delta_t
    return theta

x_data = [0,0]
y_data = [0,0]

fig, ax = plt.subplots()
ax.set_xlim(-2, 2)
ax.set_ylim(-2.5,1)
line, = ax.plot(0, 0)

def animation_frame(i):
    x = L*np.sin(theta(i))
    y = -L*np.cos(theta(i))

    x_data[1] = x
    y_data[1] = y

    line.set_xdata(x_data)
    line.set_ydata(y_data)
    return line, 

animation = FuncAnimation(fig, func=animation_frame, frames=np.arange(0, 60, (1./60)),interval = 10)
plt.show()

casiofx82
  • 26
  • 2

0 Answers0