Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
1931 views
ubuntu2004
Kernel: Python 3 (system-wide)

Methodology

1 Simple Pendulum

1.1 Required Packages

To model the simple pendulum we require three packages. Numpy as np to ... matplotlib.pyplot as plt to allow us to plot our results and from scipy.integrate import odeint to allow us to solve ODEs.

import numpy as np import matplotlib.pyplot as plt from scipy.integrate import odeint

1.2 SHM function

We convert the simple harmonic motion (SHM) ODE of a pendulum into two, first order ODEs.

We define the angular velocity as omega(t) = theta'(t)

z'(t) = -(g/L)*(sin(theta(t)))

def model(y,t,g,L): theta, omega = y dydt = [omega, -(g/L)*(np.sin(theta))] return dydt

1.3 inital conditions and plotting results

g = 9.81 L = 2 #initial conditions #sets that at time=0, angular velocity is 3 radians per second y0 = [0-0.1,0.0] #time points t = np.linspace(0,10,101) #solve ODE sol = odeint(model, y0, t, args=(g,L))

1.4 Plotting Results

plt.plot(t, sol[:, 0], 'b', label='theta(t)') plt.plot(t, sol[:, 1], 'g', label='z(t)') #plt.plot(sol[:,0], sol[:,1], 'r', label='special') #Phase plot plt.legend(loc='best') plt.grid() plt.show()
Image in a Jupyter notebook