TRANSIENT BEHAVIOR OF SIMPLE PENDULUM (using Python)
AIM:
To simulate the transient behavior of a damped simple pendulum.
OBJECTIVES:
- To simulate and create an animation of the motion of the damped simple pendulum.
- To understand how to solve ordinary differential equations in Python using the scipy module.
INTRODUCTION:
In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum.
The motion of the pendulum is based on ‘Newton’s second law of motion.’ Solving this equation in Python can help us to know how the module scipy and its class odeint works and what are the parameters it require.
EQUATIONS USED:
The 2nd degree equation alongwith the matrix format:
- `(d^2theta)/dt + b/m (d theta)/dt + g/l sin(theta) = 0`
where b = damping coefficient
m = mass of the bob
g = acceleration due to gravity
l = length of the spring
- Now, if we assume `theta = theta1` and `(d theta1)/dt = theta2`, then solving the ODE in the matrix format as follows:
`d/dt((theta1),(theta2)) = ((theta2),(-b/m theta2-g/l sin(theta1)))`
PROCEDURE OF COMPLETION:
- All the required parameters were initialized and then were used as parameters for the differential equation solver ‘odeint’ imported from ‘scipy.integrate’.
- A function named ‘sp’ was defined to define the second order ODE as a function of two first order DE.
- Then, for the simulation, ‘ArtistAnimation’ class was imported from ‘matplotlib.animation’.
- ‘ffpmeg’ videowriter was also used in the path(if not there) to write the simulation in a ‘.avi’ format.
The program attached below shows the utilization of the above mentioned points:
# PROGRAM TO KNOW ABOUT THE TRANSIENT BEHAVIOUR OF A SIMPLE PENDULUM
# Importing modules
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
import math
# bringing the writer into the path
plt.rcParams['animation.ffmpeg_path'] = 'D:\\softwares\\ffmpeg\\bin\\ffmpeg.exe'
# for ODE integration
from scipy.integrate import odeint
# writing out the animation
writer = anim.FFMpegWriter(fps=15, bitrate=1800)
# initializing necessary parameters for the pendulum
# length of the pendulum (in m)
l = 1
# mass of the bob (in kg)
m = 1
# damping coefficient
b = 0.05
# acceleration due to gravity (in m/s^2)
g = 9.81
# initial conditions
# 1st parameter = angular displacement
# 2nd parameter = angular velocity (in rad/s)
theta = [0, 3]
# time span
t_span = np.linspace(0, 20, 150)
# function to be integrated
def sp(theta, t_span, l, m, b, g):
# initializing parameters
theta1 = theta[0]
theta2 = theta[1]
# calculation
dtheta1_dt = theta2
dtheta2_dt = -(b / m) * dtheta1_dt - (g / l) * math.sin(theta1)
dtheta_dt = [dtheta1_dt, dtheta2_dt]
return dtheta_dt
# solving the ode
t = odeint(sp, theta, t_span, args=(l, m, b, g))
# angular displacement
disp = t[:, 0]
# angular velocity
vel = t[:, 1]
# plotting
plt.figure(1)
plt.plot(t_span, disp)
plt.plot(t_span, vel)
plt.grid()
plt.legend(['Angular displacement', 'Angular velocity'])
plt.xlabel('Time(in s)')
plt.title('TRANSIENT BEHAVIOUR OF SIMPLE PENDULUM')
plt.show()
fig = plt.figure(2)
ax = fig.add_subplot(1, 1, 1)
lns = []
plt.xlim([-1, 1])
plt.ylim([-1.5, 1])
plt.rcParams['font.size'] = 10
plt.title("SIMULATION OF DAMPED SIMPLE PENDULUM")
plt.axis('off')
# animation
for j in range(len(t_span)):
x = l * math.sin(disp[j])
y = -l * math.cos(disp[j])
ln, = ax.plot([0, x], [0, y], 'k', lw=2)
bob, = ax.plot(x, y, 'ro', markersize=15, markerfacecolor='red')
tm = ax.text(0, 0.75, "Timer = %.1fs" % t_span[j])
av = ax.text(0, 0.65, 'Angular Velocity = %.2fm/s' % vel[j])
ad = ax.text(0, 0.55, 'Angular Displacement = %.2fm' % disp[j])
lns.append([ln, bob, tm, av, ad])
movie = anim.ArtistAnimation(fig, lns, interval=30)
movie.save('sp.avi', writer=writer)
OBSERVATIONS:
The transient behaviour of the simple pendulum can be shown below:
As we can observe, due to the damping coefficient, the amplitude of the angular displacement and the angular velocity gradually decreases. Now, it depends upon the damping coefficient. If we want to decrease the amplitude more fast, then a higher damping coefficient can be used.
Simulation of the above mentioned simple pendulum:
CONCLUSION:
The ODEs form an integral part in the field of engineering as many phenomena and motions can be described in the form of ODEs. Python can solve different forms of ODEs which helps us to know about the variables and the graphical representations by these equations can help us analyze a particular scenario in a better way. Also, the creation of simulation in Python also helps us in knowing the behavior of a certain thing under certain conditions which also helps in knowing what can be done to the machine/thing under such circumstances.