Week 3 - Solving second order ODEs
1.AIM: A program in Python that will simulate the pendulum motion.
2.GOVERNING EQUATIONS IF ANY: `(d^2 θ)/(dt^2 )+b/m dθ/dt+g/L sinθ=0`
This equation represents the motion of a damped pendulum.
3.OBJECTIVES: A programe that solves a second order differential equation. The ODE represents the equation of motion of a simple pendulum with damping. The programe plots graph of angular velocity and angular displacement with time. The programe also simulates the motion of the pendulum with damping.
4.BODY OF THE CONTENT:
#This programe is written in python.
#This programe solves an ode which shows simulation of damped pendulum.
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
#Solving the ode
def pendulum(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
#Inputs
l = 1
m = 1
b = 0.05
g = 9.81
#Initial condition | [Displacement,Angular velocity]
theta_0 = [0,3]
#Time range
t = np.linspace(0,20,200)
#Solution of ode
sol = odeint(pendulum,theta_0,t,args=(b,g,l,m))
print(sol)
#Graphical Plotting
plt.plot(t,sol[:,0],'g-',label=r'$\frac{d\theta2}{dt}=\frac{b}{m}\theta2-\frac{g}{l}$')
plt.plot(t,sol[:,1],'r--',label=r'$\frac{d\theta2}{dt}=\theta2$')
plt.ylabel('Time')
plt.legend(loc='best')
plt.show()
#Simulation of the pendulum
ct = 1
for i in sol[:,0]:
x0 = 0
y0 = 0
x1 = l*math.sin(i)
y1 = -l*math.cos(i)
filename = str(ct) +'.png'
ct = ct+1
plt.figure()
plt.title('Animation')
#Mean line is plotted
plt.plot([0,0],[0,-1],'r--')
#Base line is plotted
plt.plot([-1,1],[0,0],linewidth = 30)
#Thread is plotted
plt.plot([x0,x1],[y0,y1],'green')
#Joining point of thread and base line
plt.plot(0,0,"o",markersize = 10)
#Ball of the pendulum is plotted
plt.plot(x1,y1,"o",markersize = 50)
#Limits of x axis and y axis are given
plt.xlim([-1.5,1.5])
plt.ylim([-1.5,0.5])
#Title of the animation
plt.title('Damped pendulum motion')
#Plots are saved in the current directory
plt.savefig(filename)
5.PRESENTATION OF THE CODE:
Importing the library.
b = Damping coefficient of the pendulum which is an input.
g = Acceleration due to gravity which is an input.
l: Length of the pendulum thread which is an input.
m: Mass of the pendulum ball which is an input.
theta_0 is the initial condition which represents the angular displacement(=0) angular velocity (= 3 rad/sec)
t is the range of time period (0 to 20 seconds) and divided into 200 times
plot command is use to plot between time period and angular displacement
plot command is use to plot between time period and angular velocity
xlim command is use to limiting of X axis
ylim command is use to limiting of Y axis
label command is used for giving name of both the plots with showing of plot colour.
sol variable is used to store the result of the ODE
for loop starts from here.It starts from i = 1 to length of sol
x and y coordinates are defined.
figure command is used to plot the simulation.
Surface line is plot with coordinates (-1,0) and (1,0).
The point of contact of the thread with the surface is plotted with coordinates (0,0) with a marker.
Thread is plotted with coordinates (x0,y) and (x1,y1).
Ball is plotted with coordinate (x1,y1).
Axes limits are defined as suitable.
Image name is defined.
Images are saved in the directory.
6.OUTPUT:
Youtube video link of simulation: https://youtu.be/_vaTs7zOz90
7.ERRORS AND DEBUGGING:
- I did not put the inputs in ode_function programe then I got the following error.
- Also I forgot to return the value of the function at last.
8.CONCLUSION: We have got a simulation of damped pendulum. Also I have obtained the graphs for angular displacement and angular velocity. After the correction of the errors I got the simulation correctly.