SOLVING SECOND ORDER ODE'S
AIM: TO SIMULATE THE TRANSIENT BEHAVIOUR OF A SIMPLE PENDULUM AND TO CREATE AN ANIMATION OF IT’S MOTION USING PYTHON
THEORY: In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum.
The way the pendulum moves depends on Newton's second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the "bob" w.r.t time.
GOVERNING EQUATIONS:
- Ordinary Differential Equation (ODE) representing equation of motion of a simple pendulum with damping –
`(d^2 θ)/(dt^2 )+ b/m×(dθ)/dt+ g/L sinθ=0`
where,
g = gravity (m/s2)
L = length of the pendulum (m)
m = mass of the ball (kg)
b = damping coefficient
OBJECTIVES:
- To study the transient behavior of the pendulum system.
- Animate the motion of the simple pendulum.
INPUT VALUES:
L = 1 m
m = 1 kg
b = 0.05
g = 9.81 m/s2
FUNCTION PROGRAM:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
# Function that returns dz/dt
def model(theta,t,b,g,l,m):
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 = 1
# Initial condition
theta_0 = [0,3]
# Time points
t = np.linspace(0,20,300)
# Solve ODE
theta = odeint(model,theta_0,t,args = (b,g,l,m))
# Plot results
plt.plot(t,theta[:,0],'b-',label = r'$\frac{d\theta_1}{dt}=\theta2$')
plt.plot(t,theta[:,1],'r--',label = r'$\frac{d\theta_2}{dt}=-\frac{b}{m}\theta_2-\frac{g}{L}sin\theta1$')
plt.ylabel('Plot')
plt.xlabel('Time')
plt.legend(loc='best')
plt.show()
# Animation using image stitching
# Defining counter variable
ct = 1
for th in theta[:,0]:
x_start = 0
y_start = 0
x_end = l*math.sin(th)
y_end = -l*math.cos(th)
# Naming the figures using the counter variable and saving them in '.png' format
filename = 'test%05d.png' %ct
ct = ct + 1
# Plotting figures and saving them with particular filename
plt.figure()
#Plotting the support
plt.plot([-1,1],[0,0], linewidth=4, color='red')
#Plotting the string through which the bob is attached
plt.plot([x_start,x_end],[y_start,y_end], linewidth=2, color='blue')
# Plotting the bob
plt.plot(x_end,y_end, marker='o', markersize=25, color='green')
plt.xlim([-2,2])
plt.ylim([-1.5,0.5])
plt.savefig(filename)
EXPLANATION OF PROGRAM:
- Initially, writing a function program where a specified function is mentioned covering all the parameters and the variables.
- The given values are specified and the initial condition is imposed.
- Timespan over which the pendulum’s transient behavior has to be observed is also defined.
- Now, the 'odeint' command acts as a solver for the first-order differential equations extracted out of a single second-order differential equations
- Animation of the pendulum motion is done using ImageMagick by stitching the images or plots obtained.
TRANSIENT BEHAVIOUR:
PLOTS:
PLOTS SAVED WITH A DEFINITE FILENAME FORMAT INTO ANIMATION FOLDER
ANIMATION: The link for the animation has been mentioned below.
CONCLUSION: Establishing a clear view of the transient behavior of the simple pendulum by solving the second-order ODE defining its motion. Also, the animation of the pendulum motion is observed by stitching the successive plots into a gif and thereby converting into a '.avi' file format.