Solving second order ODEs Using Python
Many application requires the use of the second-order differential equation. The equation for the position of a simple pendulum is one of its examples. In this work, the position of the freely oscillated pendulum was observed using python programming. The below-shown equation is used for the motion of the simple pendulum.
`(d^2 θ)/(dt^2 )+b/m (dθ)/dt+g/l sin θ=0`
OBJECTIVE
- To write a Python program for a second-order ODE that describes the position of a simple pendulum.
PYTHON SCRIPT
import math
import matplotlib.pyplot as pyplot
import numpy as np
from scipy.integrate import odeint
# function that returns dz/dt
def model(theta,t,b,g,l,m):
theta_1 = theta[0]
theta_2 = theta[1]
dtheta1_dt = theta_2
dtheta2_dt = (-b/m)*theta_2 - (g/l)*math.sin(theta_1)
dtheta_dt = [dtheta1_dt, dtheta2_dt]
return dtheta_dt
b = 0.05
g = 9.81
l = 1
m = 1
#initial_condition
theta_0 = [math.pi/3, 0]
#time points
t = np.linspace(0,20,150)
#Solve ODE
theta = odeint(model, theta_0, t, args = (b,g,l,m))
#plot results
pyplot.plot(t,theta[:,0], 'b-', label = r'$frac{dtheta_1}{dt}= theta_2$')
pyplot.plot(t, theta[:,1], 'r--', label=r'$frac{dtheta_2}{dt}=-frac{b}{m}theta_2-frac{g}{l}sintheta_1$')
pyplot.ylabel('Plot')
pyplot.xlabel('Time')
pyplot.legend(loc='best')
pyplot.show()
OUTPUT

Figure 1: Output of the scripted python program.
CONCLUSION
The equation of motion for a simple pendulum was used for python programming. In the program, the motion of the simple pendulum was analyzed for unit mass and length with a damping coefficient of 0.05. The output can be in fig. 1.