Simulating Simple pendulum Behavior using Python
OBJECTIVE
The objective of this challenge is to simulate the transient behavior of the simple pendulum with help of second-order ODE in which could describe the ball behavior w.r.t time.
PROCEDURE
According to this equation which is below,
this ODE represents the motion of simple pendulum
According to this equation, the inputs are
Given inputs
- damping coefficient=0.05
- mass = 1kg
- gravity = 9.81
- length=1
By deriving the given equation we get this final equation as
`d/dt([theta_1],[theta_2])=([theta_2],[-b/mtheta_2-g/lsintheta])`
having this equation substituting the value initially,
And by defining the user-defined function in the program to call the function with arguments and the certain internal calculation as of the equation obtained through the given ODE\'s simple pendulum equation.
In which to determine the behaviour with respect to time respectively the for loop is used to iterate the certain functional characteristics that can be carried through
PROGRAM
import matplotlib.pyplot as plt
import math
import numpy as np
from scipy.integrate import odeint
#inputs
d_c=0.05 #damping coefficient
m=1 # mass of the ball
g=9.81 #garvity
l=1 #length of the link
x,y=0,0 # co-ordinates of the link connected to
#initial condition
theta_0=[math.radians(0),3]
#time points
t=np.linspace(0,20,150) #linspace used to create numeric sequence with the defined limit
def model (theta,t,d_c,g,l,m): #this is user defined function to generate designated output
theta1=theta[0]
theta2=theta[1]
d_theta1_dt=theta2
d_theta2_dt= (-(d_c/m)*theta2)-((g/l)*math.sin(theta1))
dtheta_dt= [d_theta1_dt,d_theta2_dt]
return dtheta_dt
#solve
theta=odeint(model,theta_0,t,args=(d_c,g,l,m)) #calling the user defined function
#plotting section to demonstrate the displacement and the velocity of the pendulum
plt.plot(t,theta[:,0],\'b-\')
plt.plot(t,theta[:,1],\'r--\')
plt.xlabel(\'time\')
plt.ylabel(\'plot\')
plt.legend([\'1. displacment\',\'2.velocity\'])
plt.show()
ct=1
for i in theta[:,0]: #iteration to define the characteristics of the pendulum to be plotted
#this plotting section is to demonstrate the characteristics behavior of the simple pendulum according to the given condition
x1=l*math.sin(i)
y1=-l*math.cos(i)
plt.figure()
plt.plot([-0.5,0.5],[0,0])
plt.plot((x,x1),(y,y1))
plt.plot(x1,y1,\'o\',markersize=20) #\'o\' represnts the marker pen and the markersize defines the marker pointer size
plt.xlim(-1.2,1.2)
plt.ylim(-1.2,0.5)
plt.savefig(str (ct)+\'.png\')
ct=ct+1
OUTPUT
CONCLUSION
As the program was written according to the displacement and velocity of the simple pendulum behavior is carried out as the motion of the simple pendulum is analyzed in which by using the given data.