Solving the second-order ODE using Python
Solving the second-order ODE using Python
Program:
import math
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import numpy as np
#function that return 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
#intial condition
theta_0 = [math.pi/2,0]
# time points
t = np.linspace(0,10,150)
#solving ode
theta = odeint(model,theta_0,t,args = (b,g,l,m))
#ploting
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.xlabel(\'time\')
plt.ylabel(\'plot\')
plt.legend(loc=\'best\')
plt.show()
#Animation of Pendulum
ct = 1
for i in theta[:,0]:
x1 = 0
y1 = 0
x2 = l*math.sin(i)
y2 = -l*math.cos(i)
plt.figure()
filename = str(ct)+\'.png\'
ct = ct+1
plt.hlines(0,-0.5,0.5)
plt.plot([x1,x2],[y1,y2])
plt.plot(x2,y2, linestyle=\'\',marker = \'o\',markersize= 10,zorder=3)
plt.xlim([-1.5,1.5])
plt.ylim([-1.5,1.5])
plt.savefig(filename)
Description :
Step 1:
The required libraries are included here in this step such as math,matplotlib.pyplot as plt and from scipy.integrate and imported as odeint and numpy as np
import math
import matplotlib.pyplot as plt
from scipy.integrate import odeint
import numpy as np
Step 2:
Here function is defined as model where the derivation of ODE taken place. and returned the ode\'s to calculate.
#function that return 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
step 3:
Requried inputs are given here such as damping coefficient, acceleration due to gravity, length of the string and initial conditions of theta, time points and to calculate theta we used the function and librabary by giving the inputs hence the calculation is done.
b = 0.05
g = 9.81
l = 1
m = 1
theta_0 = [math.pi/2,0]
t = np.linspace(0,10,150)
theta = odeint(model,theta_0,t,args = (b,g,l,m))
Step 4:
Here the plotting Time and the plot is taken placed and to we used the command plt.plot and to show the plot we used plt.show().
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.xlabel(\'time\')
plt.ylabel(\'plot\')
plt.legend(loc=\'best\')
plt.show()
Step 5:
Here the animation of pendulum is coded, by starting with the counter were as ct = 1 and for loop is used to have the values of theta to make the lines and the images are created individually using counter we save the file accordingly and to plot to define hlines we used the command plt.hlines and to plot solid we used line style and marker as \'o\' and defined the size of marker and zorder and axis limites are provided by plt.xlim and plt.ylim
ct = 1
for i in theta[:,0]:
x1 = 0
y1 = 0
x2 = l*math.sin(i)
y2 = -l*math.cos(i)
plt.figure()
filename = str(ct)+\'.png\'
ct = ct+1
plt.hlines(0,-0.5,0.5)
plt.plot([x1,x2],[y1,y2])
plt.plot(x2,y2, linestyle=\'\',marker = \'o\',markersize= 10,zorder=3)
plt.xlim([-1.5,1.5])
plt.ylim([-1.5,1.5])
plt.savefig(filename)
Step 6:
The individual images are converted into GiF using online GIF converter website and later it converted into mp.4 formated and uploded.
RESULTS :
Errors :
I haven\'t taken the screenshots, but remember by an error that I faced, its while importing the library to from scipy.integrate import odeint it gave me installation error later I installed it and runner successfully and error in ploting were
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$\') in this i forget to mention {dt} in first line it runed but show no figure these are the errors i found while coding. from next challanges i post my errors as screenshot also.