-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlagrange.py
More file actions
51 lines (38 loc) · 1.19 KB
/
lagrange.py
File metadata and controls
51 lines (38 loc) · 1.19 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
# #laGrange need to find the kinetic at https://www.youtube.com/watch?v=SFpJbmHIUB0&index=22&list=PL2ym2L69yzkZJ1fY3SQ1JCyvZIoJYXQGZ&ytbChannel=Freeball
# kinetic = 0.5*ml^2*thetaDot^2
# potential = m*g*l(1-cosTheta)
# thetaDDot + g/l sin theta = 0
import numpy as np
from numpy import sin, cos
from scipy.integrate import odeint
from matplotlib import pyplot as plt
# define the equations
def equations(y0, t):
theta, x = y0
f = [x, -(g/l) * sin(theta)]
return f
def plot_results(time, theta1, theta2):
plt.plot(time, theta1[:,0])
plt.plot(time, theta2)
s = '(Initial Angle = ' + str(initial_angle) + ' degrees)'
plt.title('Pendulum Motion: ' + s)
plt.xlabel('time (s)')
plt.ylabel('angle (rad)')
plt.grid(True)
plt.legend(['nonlinear', 'linear'], loc='lower right')
plt.show()
# parameters
g = 9.81
l = 1.0
time = np.arange(0, 10.0, 0.025)
# initial conditions
initial_angle = 130.0
theta0 = np.radians(initial_angle)
x0 = np.radians(0.0)
# find the solution to the nonlinear problem
theta1 = odeint(equations, [theta0, x0], time)
# find the solution to the linear problem
w = np.sqrt(g/l)
theta2 = [theta0 * cos(w*t) for t in time]
# plot the results
plot_results(time, theta1, theta2)