Program to Evualate the motion of simple pendulam by solving the ODE using python
AIM:
Evualate the motion of simple pendulam by solving the ODE using Python
OBJECTIVE:
1.Write a program to Evualate the motion of simple pendulam by solving the ODE using python.
2.Simulate the motion of simple pendulam.
PROBLEM STATEMENT:
This is the second order ODE given for damped oscillatory motion of simple pendulam
Where,
g=Gravity
L=Length of the Pendulam
m=Mass of the ball
b=Damping Coefficient
Now using the python we will code for solving the ODE
PROGRAM:
The code used
#Solving 2nd order ODE
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
#Function
def model(theta,t,b,l,g,m):
theta1 = theta[0]
theta2 = theta[1]
dtheta_1 = theta2
dtheta_2 = -(b/m)*theta2-(g/l)*math.sin(theta1)
dtheta =[dtheta_1,dtheta_2]
return dtheta
#Input Parameters
b = 0.05 #Damping co-efficient
g = 9.81 #Gravity
m = 1 #Mass
l =1 #Length of the pendulam
#Initial conditions
theta_0 = [0,3] #First Value is displacement and Second values is velocity
#Time Points
t = np.linspace(0,20,150)
theta = odeint(model,theta_0,t,args=(b,l,g,m))
plt.plot(t,theta[:,0],'b-')
plt.plot(t,theta[:,1],'r--')
plt.ylabel('plot')
plt.xlabel('time')
plt.show()
#Animation Step
ct = 1
x0 = 0
y0 = 0
for i in theta[:,0]:
x1 = (l*math.sin(i))
y1 = (-l*math.cos(i))
filename = 'pendulam'+str(ct)+'.png'
plt.figure()
plt.plot([-1,1],[0,0],"black",linewidth = 10)
plt.plot([x0,x1],[y0,y1],"red")
plt.plot(x1,y1,'o',markersize = 20)
plt.xlim([-1.5,1.5])
plt.ylim([-1.5,1.0])
plt.savefig(filename)
ct = ct+1
SOLUTION/CODE EXPLANATION:
Initially we are importing the required modules using the import command i.e math,numpy,matpltlib.pyplot and odeint.In abouve codes from 8th to 14the line of code we have defined the function model with required arguments,Inside that we have broken the second order ODE to first order and we defined both term into single one called dtheta.After that we have given the input parameters and intial conditions.In initial Conditions the first value represent the displacement and Second values is velocity.To plot the result aginst time the codes from 27th to 33 th is used where linspace command will give the 150 values between 0 and 20.And the odeint command is used to solve the ODE.From 36 to 54 line sthese are coomand to create animation.
RESULT:
By running the abouve code will generate the plot and animation of the pendulam
Find the Youtube link for the Animation:https://youtu.be/iiUePj6oIn0