Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
1931 views
ubuntu2004
Kernel: Python 3 (system-wide)
#Import recquired models import numpy as np import matplotlib.pyplot as plt #This allows us to code for ODEs from scipy.integrate import odeint #Setting values g = 9.81 L = 2 #Function- We convert the SHM ODE of a pendulum into 2 first order ODEs def model(y,t,g,L): #theta'(t) = omega(t) #omega'(t) = -(g/L)*(sin(theta(t))) theta, omega = y dydt = [omega, -(g/L)*(np.sin(theta))] return dydt #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)) #plotting results plt.plot(t, sol[:, 0], 'b') plt.ylabel('Angular displacement (rad)') plt.xlabel('Time (seconds)') plt.title('Fig 4') plt.grid() plt.show() plt.show()
Image in a Jupyter notebook
#Import recquired models import numpy as np import matplotlib.pyplot as plt #This allows us to code for ODEs from scipy.integrate import odeint #Setting values g = 9.81 L = 2 #Function- We convert the SHM ODE of a pendulum into 2 first order ODEs def model(y,t,g,L): #theta'(t) = omega(t) #omega'(t) = -(g/L)*(sin(theta(t))) theta, omega = y dydt = [omega, -(g/L)*(np.sin(theta))] return dydt #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)) #plotting results plt.plot(t, sol[:, 1], 'b') plt.ylabel('Angular velocity (rad per seconds)') plt.xlabel('Time (seconds)') plt.title('Fig 5') plt.grid() plt.show() plt.show()
Image in a Jupyter notebook
import numpy as np import matplotlib.pyplot as plt import math from scipy.integrate import odeint M=12 m=1 g=9.81 def Atwood(y,t,M,m,g): theta, omega, radius, Rho = y dydt = [omega, (1/radius)*(-g*(np.sin(theta))-2*(Rho*omega)), Rho, (1/(M+m))*(m*radius*(omega**2)-M*g+m*g*(np.cos(theta)))] return dydt y0 = [(1.1*np.pi)/2 ,0 ,1 ,0] t = np.linspace(0,100,10000000) sul = odeint(Atwood, y0, t, args=(M,m,g)) A=12 a=1 g=9.81 def second(y,t,A,a,g): theta, omega, radius, Rho = y dydt = [omega, (1/radius)*(-g*(np.sin(theta))-2*(Rho*omega)), Rho, (1/(A+a))*(a*radius*(omega**2)-A*g+a*g*(np.cos(theta)))] return dydt y1 = [(np.pi)/2 ,0 ,1 ,0] t = np.linspace(0,100,10000000) sal = odeint(second, y1, t, args=(A,a,g)) plt.plot(sal[:,0], sal[:,1], 'r') plt.plot(sul[:,0], sul[:,1], 'g') plt.ylabel('Angular velocity') plt.xlabel('Angular displacement') plt.title('Fig 12 - Phase Plot') plt.grid() plt.show()
Image in a Jupyter notebook