Week 3 - Solving second order ODEs
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
#function that return dz/dt
def model(theta,d,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.02
g = 9.18
l = 1
m = 1
# initial Conditions
theta_0 = [0,3]
#Time Point
t = np.linspace(0,20,200)
#Solve ODE
theta = odeint(model,theta_0,t,args = (b,g,l,m))
#Plot Result
#plotting Angular Displacement
plt.plot(t,theta[:,0],'b-',label=r'$frac{dtheta_1}{dt}=theta2$')
#plotting Angular Velocity
plt.plot(t,theta[:,1],'r--',label=r'$frac{dtheta_1}{dt}=-frac{b}{m}theta_2-frac{g}{L}sintheta_1$')
#limiting X-axis
plt.xlim([0,20])
#Limiting Y-axis
plt.ylim([-3,3])
plt.ylabel('plot')
plt.xlabel('time')
plt.legend(loc="best")
plt.show()
#for Counter Variable
ct=1
for Theta in theta[:,0]:
#co-ordinates of Fixed point of string
x0=0
y0=0
#co-ordinate of End point of string
x1= l*math.sin(Theta)
y1= -l*math.cos(Theta)
filename = str(ct) + '.png'
ct = ct + 1
plt.figure()
#for Horizontal line Plotting
plt.plot([-0.5,0.5],[0,0],'g',linewidth=5)
#for Plotting String of Pendulum
plt.plot([x0,x1],[y0,y1],'r')
#Plotting Ball of Pendulum
plt.plot(x1,y1,'o',markersize=30)
#limiting X-axis
plt.xlim([-1.5,1.5])
#limiting y-axis
plt.ylim([-1.5,1.5])
#for Title
plt.title('Animation of Simple Pendulum Using Python')
#Saving each Figure with its file name
plt.savefig(filename)
Comments:-
1, first of all Import numpy module, scipy module, matplotlib.pyplot module & math module.
2,Define a function which is the system of ordinary differential equations.
3,split the Second order ODE into two First order ODEs.
4, Assigning a given values to inputs.(b,g,l,m)
5, Assigning an initial condition at time(t=0) where initial angular displacement is 0 and initial angular velocity is 3 as shown in the question.
6, Time span is 0 to 20 sec. So, Assign 200 equally spaced values from 0 to 20 in variable " t ".
7, "odeint" command is in the scipy module. "odeint" is basically a function that integrate system of ordinary differential equations. For that defined a system('model' function) above which is the system of two first order ODEs.
8, Assign a function , initial conditions , time and input arguments to the "odeint".
9, Plot Angular Displacement & Angular Velocity.
10, Used legend to describe the graph.
11, Use xlim & ylim to limit the X-axis & Y-axis and Use xlabel & ylabel to indicate both axes.
12, Assigning value '1' to Counter Variable 'ct'.
13, theta[:,0] is the set of values of Angular Displacement.
14, Assigning a co-ordinates to fixed point & end point of string of pendulum by using simple trigonometry.
15, ct is the counter variable which is used to assign a number to the filename(with one increment) with every iteration.
16, Plotting Fixed Horizontal Line , String & Ball of Pendulum.
17, Used 'xlim' & 'ylim' to limit the X-axis & Y-axis.
18, Used 'title' to indicate this animation.
19, savefig(filename) is used to save each position of pendulum in seperate figure with its filename.