Challenge

Week 3 - Solving second order ODEs

AIM:

To solve the given ODE using python, store the solved values in variables and then use the values to create an animation of the motion of a simple pendulum(whose motion is represented by the given ODE).

PROBLEM AND THEORY:

The motion of a lot of objects just cant be explained using simple linear/polynomial equations. Some motions are complex and show periodic variations with time. The motion of a simple pendulum is such an example. Its motion can be defined by an ODE that best represents the variation of its angular dispalcement and velocity with respect to time. It can be represented as follows:

CODE:

#Soving Second Order ODE for the motion of a pendulum
import math
import matplotlib.pyplot as plt 
import numpy as np
from scipy.integrate import odeint

#Defining the function
def model(theta,time,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
b=0.05 #damping coefficient
g=9.81 #gravity
l=1  #length of the string
m=1    #mass of the bob


x0=0
y0=0

# Initial displacement and angular velocity of the bob
theta_start=[0,3]
# creating the time array to creat time points
t = np.linspace(0,10,150)

# Calling the function and storing the value in a variable 
theta = odeint(model,theta_start,t,args=(b,g,l,m))
# extracting the values for angular velocity and angular displacement
angular_disp = theta[:,0]
angular_vel = theta[:,1]

#Plotting the figures
plt.figure(1)
plt.plot(t,angular_disp,label='angular displacement')
plt.plot(t,angular_vel,label='angular velocity')
plt.xlabel('time')
plt.ylabel('variation')
plt.legend(loc='upper right', frameon=True)

plt.show()
x1=[]
y1=[]
counter=1
# using the for loop to access the position of bob at corresponding angular displacement
for d in angular_disp:
 
 x1 = (l*math.sin(d))
 y1 = -(l*math.cos(d))

 # generating unique file names
 filename = str(counter)+'.png'
 plt.figure()
 plt.plot([x0,x1],[y0,y1],linewidth=2)

 # defining the axis of the frame to scale it
 plt.xlim([-1.2,1.2])
 plt.ylim([-1.5,0])

 # forming the bob
 plt.plot(x1,y1,'r.',markersize=30)
 # saving each figure with its unique filename
 plt.savefig(filename)
 
 counter=counter+1
 

CODE EXPLANATION:

To create the motion of an oscillation pendulum, one has to solve the 2nd degree differential equation the oscillation with the given conditions of time, length, mass and damping coefficient to solve for the displacement and velocity.

We can use the function odeint from the module scipy with which we can solve diffential equations of required order with ease. Hence, at the begining of the program, we specify the required constraints and initial values of the ODE as inputs and following it, we use the odeint function and create another variable matrix, "t" to genetate time points for the motion plot, and then displacement and velocity solutions obtained from the odeint function in another matrix, "theta".

Following that, we extract and store the values of displacement and velocity in individual arrays to easily access them and then plot a graph visualizing the variation of displacement and velocity with time.

After solving the ODE and obtaining the solution vector for displacement and velocity, all we have to do is, fix one end point of the rod and place the other end according to the variation of displacement that had been obseved via solving the ODE.

To plot the instantenous position of the rod, we set the cordinates of the bob to:

x = l(sin(theta))

y = l(cos(theta))

Then, we use the plot command along with the for loop to create plots for each time step created to joint the two ends of the string. Now, we have to save each plot obtained. And to to so, we use use a counter to generate unique file names for each plot.

After saving the obtained plots, we convert the still images into a GIF using image Magick programe.

PLOTS AND RESULTS:

YOUTUBE LINK FOR THE ANIMATION:

https://www.youtube.com/watch?v=5TmIVzBc2Hw&feature=youtu.be

RESULTS AND DISCUSSION:

Hence, from the above program, the animation of the pendulum motion and the variation of the angular displacement and angular velocity of the pendulum were generated.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.