Solving second order ODEs using python
Aim:
- To write a python program to solve the give 2nd order ode function.
- To plot the result obtain while solving the ode.
Problem statement:
ODE represents the equation of motion of a simple pendulum with damping,
where,
g = gravity is 9.81 m/s2
L = length of the pendulum in 1m,
m = mass of the ball in 1kg,
b=0.05 damping coefficient.
The above equation is solved with respect to time of 0 to 20 seconds.
Program:
# Program to solve the 2nd order differential equation for pendulum
#importing the library
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
def equat(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
# Input datas
# Damping coefficient
b = 0.05
# Gravitational constant(m/s^2)
g =9.81
# Length of the string(m)
l = 1
#Mass of the ball(kg)
m = 1
# Initial conditions
theta_0 =[0,3]
# Generating time seconds
t = np.linspace(0,20,150)
# Using python ode solver
theta = odeint(equat,theta_0,t,args =(b,g,l,m))
#Plot results
plt.plot(t,theta[:,0])
plt.plot(t,theta[:,1])
plt.xlabel('time')
plt.ylabel('plot')
plt.show()
# Creating the pendulum movement
ct = 1
for i in theta[:,0]:
x0 = 0
y0 = 0
x1 =l*math.sin(i)
y1 =-l*math.cos(i)
x2 = -1.5
y2 = 0
x3 = 1.5
y3 = 0
# Saving each movement location of ball with different file name
filename = str(ct) + '.png'
ct = ct+1
plt.figure()
# Plotting the string
plt.plot([x2,x3],[y2,y3])
# Creating the ball in the plot
plt.plot([x0,x1],[y0,y1],x1,y1,'o',markersize = 20, color = 'blue')
# Setting the x and y limits
plt.xlim([-2,2])
plt.ylim([-2,2])
# Title for the plot
plt.title('pendulum')
plt.legend()
# Saving the file
plt.savefig(filename)
Results:
Plot
Simple pendulum animation:
The resulted plot otained at every loop is saved with different name and the files are stitchced together to form the below animated image.
Explanation:
Math module is used hence we need to solve the trignometry functions.
Numpy module is used to execute the linspace cmd
matplotlib is used for plotting the results.
scipy is used which perform the integration part which is most basic obective of this project.
Stepwise explanation:
- All the required modules are imported.
- Created the called function with required input parameters.
- In this called function the dtheta_dt is calculated and the value of it is returned to the calling function.
- Input datas such as damping coefficient, length of the string, gravtional constant, mass of the ball are determined.
- Initial conditions and the time condtions are given.
- Use the ode solver
- Plot the time vs plot wave graph.
- Using for loop creating the pendulum movement and save the each plot obtained at every loop is saved with different name.
- Finally, the obtained pendulum movement plots are stiched together with image to gif converter externally.
Conclusion:
- Given ode equation is solved.
- Movement of the pendulum with respect to time is plotted.
- The obtained animation is seems to be perfect.