SOLVING SECOND ORDER ODE OF PENDULUM USING PYTHON
AIM
To find the second order ODE for the pendulum using the python programming.
PROCEDURE:
- Import the necessary library functions from the python library.
- Define the function for the input values and give data to each values.
- Now make the basic pendulum functional formula and write it down in the function.
- Use linspace command which helps you to get the desired range of values.
- Now,solve the ODE for the program.
- Create the plots for the x and y axis.
- Then write down the header for the axis.
- After doing this,create animation by giving the limits for the pendulum oscillations by providing the initial and final lemgth of the pendulum.
- Then stich the plot with single file and upload the gif file in youtube.
- To convert png as gif file use image magick.
PENDULUM MOTION:
A simple pendulum consists of a relatively massive object hung by a string from a fixed support. It typically hangs vertically in its equilibrium position. The massive object is affectionately referred to as the pendulum bob. When the bob is displaced from equilibrium and then released, it begins its back and forth vibration about its fixed equilibrium position. The motion is regular and repeating, an example of periodic motion. Pendulum motion was introduced earlier in this lesson as we made an attempt to understand the nature of vibrating objects. Pendulum motion was discussed again as we looked at the mathematical properties of objects that are in periodic motion. Here we will investigate pendulum motion in even greater detail as we focus upon how a variety of quantities change over the course of time. Such quantities will include forces, position, velocity and energy - both kinetic and potential energy.
PENDULUM DIAGRAM:
PENDULUM FORMULA:
The differential equation which represents the motion of a simple pendulum is
- {\\displaystyle {\\frac {d^{2}\\theta }{dt^{2}}}+{\\frac {g}{\\ell }}\\sin \\theta =0}
PROGRAM FOR PENDULUM:
#SIMPLE PROGRAM FOR FINDING THE SECOND ORDER ODE FOR PENDULUM MOTION USING PYTHON
import math
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import numpy as np
def model(theta,t,b,g,m,l):
theta1 = theta[0]
theta2 = theta[1]
dtheta1_dt = theta2
dtheta2_dt = (-b/m)*theta2 -(g/l)*math.sin(theta1)
dtheta_dt = [dtheta1_dt , dtheta2_dt]
return dtheta_dt
b = 0.05
g = 9.81
l = 1
m = 0.1
theta_0 = [0,3]
t = np.linspace(0,20,400)
#SOLVING ODE:
theta = odeint(model,theta_0,t,args = (b,g,l,m))
#PLOTTING RESULTS:
plt.plot(t,theta[:,0],\'g\' ,label=r\'displacement\')
plt.plot(t,theta[:,1],\'b\' ,label=r\'velocity\')
plt.xlabel(\'plot\')
plt.ylabel(\'Time\')
plt.legend()
plt.savefig(\'output.png\')
plt.show()
#ANIMATION STEP:
ct = 1
for theta in theta[:,0] :
x0 = 0
y0 = 0
x1 = l*math.sin(theta)
y1 = -l*math.cos(theta)
plt.figure()
plt.plot([x0,x1],[y0,y1],\"red\")
plt.plot(x1,y1,\'o\',markersize=8)
plt.xlim([-1,1])
plt.ylim([-1.5,1])
filename = str(ct) + \'.png\'
plt.savefig(filename)
ct = ct+1
OUTPUT FOR THE PENDULUM:
YOU-TUBE LINK:
REFERENCES: