Python Report on: - Solve transient behavior of pendulum.
Objective: -Write a program to simulate the transient behavior of a simple pendulum and to create an animation of its motion.
-
In the above equation,
g = gravity in m/s2 = 9.81
L = length of the pendulum in m = 1
m = mass of the ball in kg = 1
b=damping coefficient = 0.05
- Ordinary differential equation are solved in python with scipy.integrate package using function ODEINT. Another python package that solves a differential equation is GEKKO.
- A differential equation related to some function with the derivatives of the function. function generally represents physical quantities and the derivatives represent a rate of change.
- The differential equation defines a relationship between the quantity and the derivative. Differential equations may be studies from several different perspectives.
Step 1: Write a code to solve ODE for a pendulum animation.
\"\"\"
Code by Ravi Thakur
Subject: Write a program to ODE for pendulum motion
\"\"\"
import numpy as np
from scipy.integrate import odeint
import math
import matplotlib.pyplot as pyplot
# Function module
def model(theta,t,b,g,m,l):
theta_1=theta[0];
theta_2=theta[1];
dtheta1_dt = theta_2;
dtheta2_dt = -(b/m)*theta_2 - (g/l)*math.sin(theta_1);
dtheta_dt = [dtheta1_dt, dtheta2_dt];
return dtheta_dt
b = 0.02
g = 9.81
l = 2
m = 0.1
#initial condition
theta_0 = [math.pi/4,0]
#time point
t = np.linspace(0,10,200)
#solve ODE
theta = odeint(model,theta_0,t,args =(b,g,l,m))
#plot results
pyplot.plot(t,theta[:,0],\'b-\',label =r\'$\\frac{d\\theta_1}{dt}=\\theta2$\')
pyplot.plot(t,theta[:,1],\'r--\',label =r\'$\\frac{d\\theta2}{dt}=-\\frac{b}{m}\\theta_2-\\frac{g}{L}sin\\theta_1$\')
pyplot.legend(loc=\'best\')
pyplot.xlabel(\'time\')
pyplot.ylabel(\'Amplitude\')
pyplot.title(\'Time vs. Amplitude Graph\')
pyplot.savefig(\'Graph 1.png\')
pyplot.grid()
pyplot.show()
ct = 1
for theta in theta[:,0]:
x0 = 0
y0 = 0
x1 = -l*math.sin(theta)
y1 = -l*math.cos(theta)
pyplot.figure()
pyplot.plot([-1,1],[0,0],\'black\',linewidth = 15)
pyplot.plot([x0,x1],[y0,y1],\'red\',linewidth=3)
pyplot.xlim(-1.8,1.8)
pyplot.ylim(-2.5,1.5)
pyplot.plot(x1,y1,\'o\',markersize = 35)
pyplot.xlabel(\'X-axis\')
pyplot.ylabel(\'Y-axis\')
pyplot.title(\'Pendulum Motion\')
pyplot.grid()
filename = str(ct)+\'.png\'
pyplot.savefig(filename)
ct=ct+1
Step 2: Results
The plot show Time Vs. Amplitude graph
The plot show pendulum \'.gif\' file animation
Step 3: Commands used
- ODE command used to solve the differential equation for the transient behavior of pendulum.
- Def command used to solve the store the equation in a function format to solve it for each theta value.
- For loop applied so that the value of theta that has been stored in linspace command can be used for every motion of theta.
Step 4: Conclusion
- We learn and understood how to use the ODE command to solve the differential equation in PYTHON.
- We learn how to generate certain range of frames for a certain position of theta.
Step 5: References
- Transient behavior - https://www.sciencedirect.com/topics/computer-science/transient-behavior
- ODEINT - https://apmonitor.com/wiki/index.php/Apps/2ndOrderDifferential
Step 6: YouTube link of a pendulum animation