-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPendulum.py
More file actions
62 lines (46 loc) · 1.17 KB
/
Pendulum.py
File metadata and controls
62 lines (46 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
import math
from math import sin
import matplotlib.pyplot as plt
a=0.0
b=100.0
h = 1e-2
timepoints=np.arange(a,b,h)
g= 9.8
l= 0.1
x = [179,0]
r = np.array(x)
#r=np.arange(179,0, -1)
rpoints=[]
def f(r,t):
theta=r[0]
omega=r[1]
ftheta = omega
fomega = -(g/l)*(sin((theta)))
return np.array([ftheta,fomega],float)
#RungaKutta
for t in timepoints:
rpoints.append(r[0])
k1 = h*f(r,t)
k2 = h*f(r+0.5*k1,t+0.5*h)
k3 = h*f(r+0.5*k2,t+0.5*h)
k4 = h*f(r+k3,t+h)
r= r+ (k1+2*k2+2*k3+k4)/6
plt.plot(timepoints,rpoints,'red')
import matplotlib.animation as animation
fig = plt.figure(figsize=(5,5))
ax = plt.axes(xlim=(-1, 1), ylim=(-1, 1))
pendulum = plt.Circle((.1, .1), radius=0.02, fc='r')
def init():
pendulum.center = (0.1, 0.1)
ax.add_patch(pendulum)
return pendulum,
def animate(i):
x = l*np.cos(rpoints[i]- (np.pi/2))
y = l*np.sin(rpoints[i]-(np.pi/2))
pendulum.center = (x, y)
return pendulum,
anim = animation.FuncAnimation(fig, animate,
init_func=init,frames=360,interval=20,blit=True)
anim.save('Julianospendulum.mp4')
plt.show()