-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimplep_pendulum1_tomthin123.py
57 lines (56 loc) · 1.67 KB
/
simplep_pendulum1_tomthin123.py
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
#angle Vs time
import matplotlib.pyplot as plt
import numpy as np
tini=0
tfin=15
Nsteps=1000
y0=0#initial position
vy0=1#initial velocity
g=9.8
l=.5#length of pendulum
vy=np.zeros(Nsteps+1)
y=np.zeros(Nsteps+1)
y[0]=y0
vy[0]=vy0
vys=np.zeros(Nsteps+1)
ys=np.zeros(Nsteps+1)
ys[0]=y0
vys[0]=vy0
h=abs(tini-tfin)/Nsteps
t=np.linspace(tini,tfin,Nsteps+1)
def f(x1,x2):
y1=-(g/l)*np.sin(x1)
y2=x2
y3=-(g/l)*x1
return y1,y2,y3
for i in range(1,Nsteps):
#l's is the runge kutta coefficients for angular velocity array
#k's is the runge-kutta coefficients for angle(theta) w/o approx.
#d's is the runge-kutta coefficients for angle(theta) with approx.
k1=h*f(y[i-1],0)[0]
l1=h*f(0,vy[i-1])[1]
k2=h*f(y[i-1]+l1/2,0)[0]
l2=h*f(0,vy[i-1]+k1/2)[1]
k3=h*f(y[i-1]+l2/2,0)[0]
l3=h*f(0,vy[i-1]+k2/2)[1]
k4=h*f(y[i-1]+l3,0)[0]
l4=h*f(0,vy[i-1]+k3)[1]
y[i]=y[i-1]+1/6*(l1+2*l2+2*l3+l4)#angle vs time array
vy[i]=vy[i-1]+1/6*(k1+2*k2+2*k3+k4) #omega vs time array
#solving for theta and omega without approx.
d1=h*f(y[i-1],0)[2]
l1=h*f(0,vy[i-1])[1]
d2=h*f(y[i-1]+l1/2,0)[2]
l2=h*f(0,vy[i-1]+d1/2)[1]
d3=h*f(y[i-1]+l2/2,0)[2]
l3=h*f(0,vy[i-1]+d2/2)[1]
d4=h*f(y[i-1]+l3,0)[2]
l4=h*f(0,vy[i-1]+d3)[1]
ys[i]=y[i-1]+1/6*(l1+2*l2+2*l3+l4)#angle vs time array
vys[i]=vy[i-1]+1/6*(d1+2*d2+2*d3+d4)#omega vs time array
plt.plot(t[0:len(t)-1],y[0:len(t)-1],label=('angle vs time with approx.'))
plt.plot(t[0:len(t)-1],ys[0:len(t)-1],'.',label=('angle vs time w/o approx.'))
plt.xlabel('time(s)')
plt.ylabel('angle(radians)')
plt.legend(loc='upper right')
plt.show()