To demonstrate the transient behavior of a simple pendulum using Python.
Objective: To show the transient behaviour of a simple pendulum using Python.
Given data:
In the above equation,
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Write a program in Python that will simulate the pendulum motion, just like the one shown in the start of this challenge.
use,
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Procedure:
Derivation of a equation:
We had second order ODEs so we have converted in the series of first order ODEs to solve it.
Now, we will try to solve it using using Python programming.
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
#function that return d0/dt
def model(theta,t,b,g,l,m):
theta1= theta[0]
theta2= theta[1]
dtheta1_dt = theta2
dtheta2_dt = -(b/m)*theta2 -(g/1)*math.sin(theta1)
dtheta_dt = [dtheta1_dt, dtheta2_dt]
return dtheta_dt
l=1 #metre
m=1 #kg
b=0.05 #damping co-efficient
g=9.81 #m/s2
#intital condition
theta_0 = [0,3]
#timepoints
t = np.linspace(0,20,150)
#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\theta2}{dt}=-\frac{b}{m}\theta_2-\frac{g}{L}sin\theta_1$')
plt.ylabel('plot')
plt.xlabel('time')
plt.legend(loc='best')
plt.savefig('Time_plot')
plt.show()
ct = 1
for i in theta[:,0]:
x0 = 0
y0 = 0
x3 = -1.5
y3 = 0
x4 = 1.5
y4 = 0
x1 = l*math.sin(i)
y1 = -l*math.cos(i)
filename = str(ct)+ '.png'
ct = ct+1
print(filename)
plt.figure()
plt.plot([x0,x1],[y0,y1])
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.plot([x3,x4],[y3,y4])
plt.plot(x1,y1,'o', markersize=20)
plt.savefig(filename)
Explaination of coding:
Math module
We need to import MATH library when our code contains any trigonometric functions like sine, cosine or if we use mathematical constants like 'pi'.
Plotting module
We need to import plotting module if we want our code to plot a graph.
Numpy Module
To give a power to each element of an array or to perform any mathematical operations (like addition or subtraction) either on every element of an array or between the arrays, we need to import the numpy library.
Scipy Module
Before importing this module, we have to first install Scipy Module. To install scipy module, we need to go to the command prompt (CMD) and make use of PIP command.
Under scipy module, there are many inbuilt functions such as,
● ODEINT
● CURVE_FIT
And if we try to use these inbuilt functions without importing the scipy module, they will not work and will end up with an error.
ODEINT is basically a function which will integrate Ordinary Differential Equation (ODE) or a system of Ordinary Differential Equations.
Then we will define a function:
A Function is a template. It is a set of code that we can use repeatedly.
Suppose there is some set of code which is needed to be used repetitively in the main program then instead of writing over and over causing the the main program to get lengthy, we can
create a function for that part of code and then call the function by its name and give the required inputs.
For Eg in this particular coding: def model(theta,t,b,g,l,m):
model:- function name.
inside () :- Input arguments
Afterwards We need to tell the Function explicitly to RETURN the value for the particular variable once the calculation is done. So that we can get to see all the stored values while printing
the values in the command window.
Graph plotting methods:
- Whenever we want to define more than one value in a single variable we have to use an array and numpy.linspace is also the same thing it stores value in a range like this;
numpy.linspace (lower limit, upper limit, no. of division)
This works acc. to a formula:
Size of division = (upper limit-lower limit)/(no. of division-1)
3. Use an empty array for storing a value which we get from FOR loop, theta1 & theta2 will store 10 values.
4. Append is a command that will continuously append the value of a variable. It keeps on adding the values into an existing array.
so for that, we have to define an empty array for that variable which we are going to use append command. Temp is a variable in which we store values from append command.
5. use plt.plot() command to plot the graph of the given result and use plt.figure() command.
For proper animation we use .plt.xlim & plt.ylim command.
Errors received while compiling:
1. Syntax error:
2. Invalid syntax: by mistake extra space was given so got one error.
3. wrong brackets given:
4. Typo in calling plotting module:
5. Missed to define the time step in the coding:
6. Didn't differentiated theta1 and theta 2 and defined value under one name "theta" so got the error.
Results:
theta1 = displacement & theta2 = velocity.
In order to create an animation out of FRAMES we can make use of a software called ImageMagick.
But to get a smooth animation, all the images must be stitched sequentially. In some cases, the images may get randomly stitched like shown below.
Output animation of a simple pendulum:
Conclusion: Understood the concept of solving ODE in a python. We can breakdown the second order ODEs into the series of first order and can easily be solved abd resulted can be predicted and can easily be checked which value is affecting the results in what manner beacuse programming makes it easy to visualize the problems.