Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
1931 views
ubuntu2004
1
#Import recquired models
2
import numpy as np
3
import matplotlib.pyplot as plt
4
5
#This allows us to code for ODEs
6
from scipy.integrate import odeint
7
8
#Setting values
9
g = 9.81
10
L = 2
11
12
#Function- We convert the SHM ODE of a pendulum into 2 first order ODEs
13
def model(y,t,g,L):
14
15
#theta'(t) = omega(t)
16
#omega'(t) = -(g/L)*(sin(theta(t)))
17
18
theta, omega = y
19
dydt = [omega, -(g/L)*(np.sin(theta))]
20
return dydt
21
22
23
#initial conditions
24
#sets that at time=0, angular velocity is 3 radians per second
25
y0 = [0-0.1,0.0]
26
27
#time points
28
t = np.linspace(0,10,101)
29
30
#solve ODE
31
sol = odeint(model, y0, t, args=(g,L))
32
33
#plotting results
34
plt.plot(sol[:,0], sol[:, 1], 'b')
35
plt.ylabel('Angular velocity (radians per seconds)')
36
plt.xlabel('Angular displacement (radians)')
37
plt.title('Fig 3')
38
plt.grid()
39
plt.show()
40
plt.show()
41
42