-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_fixed_plot.py
More file actions
75 lines (62 loc) · 2.13 KB
/
simple_fixed_plot.py
File metadata and controls
75 lines (62 loc) · 2.13 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
63
64
65
66
67
68
69
70
71
72
73
74
75
#math only for simplest pendulum case with fixed pivot
from scipy.integrate import odeint
from matplotlib import pylab as plt
import numpy as np
##############initial conditions###################
theta0 = .2 #initial angle in radians, theta=0 is straight up
thetadot0 = 0.3 #initial angular velocity of pend
atheta0 = 0.0 #initial angular accel
###################################################
#################Constants#########################
m = 1 #mass of pendulum (kg)
g = 9.8 #gravitational accel (m/s^2)
r = 1 #length of pendulum arm (m)
###################################################
theta = theta0
thetadot = thetadot0
atheta = atheta0
def rhs(q, t):
theta, thetadot = q
if theta < 0: ##keep theta between 0 and 2pi
theta += 2*np.pi ##
if theta > 2*np.pi: ##
theta -= 2*np.pi ##
atheta = (g/r)*np.sin(theta) #equation of motion for simple pendulum
return [thetadot, atheta]
T=10.
steps= 500. #correspond to dt=.02??
q0 = ([theta0, thetadot0])
ts = np.linspace(0, T, steps)
q1,q2 = odeint(rhs, q0, ts).T
plt.plot(ts, q1, color='r')
#print(len(ts))
plt.xlabel('t')
plt.ylabel('Initial theta: '+str(theta0)+'\nInitial vel: '+str(thetadot0))
plt.title('Comparison of Methods for Simple Pendulum Case\nODEINT: angle theta (from vertical) is red, angular velocity is green \n MANUAL: angle is pink, velocity is blue')
plt.plot(ts, q2, color='g')
#duration = 30.0
dt=.02
#i=0
count = 0
Q1 = np.arange(0,500, dtype = float)
#print(len(Q1))
Q2=np.arange(0,500,dtype=float)
Q1[0]=theta0#+.2
#print(theta0)
#print(Q1[0])
Q2[0]=thetadot0#+.2
theta = theta0
while count < 500:
count+=1
#if theta <0: ##keep theta between 0 and 2pi for purpose of case handling in accelerate_slider()
#theta += 2*np.pi ##
#if theta > 2*np.pi: ##
#theta -= 2*np.pi ##
atheta = (g/r)*np.sin(theta)
thetadot += atheta*dt
theta += thetadot*dt
if count < 500:
Q1[count]= theta#+.2
Q2[count]=thetadot#+.2
plt.plot(ts, Q1, color='pink')
plt.plot(ts, Q2, color='blue')