Challenge

Week 3 - Solving second order ODEs

AIM : To Solving second order ODEs

Objective :

In the above equation,
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Write a program in Python that will simulate the pendulum motion, just like the one shown in the start of this challenge.
use,
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.

Program/Code :

for CASE 1

"""
# second order ODEs #
"""

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math


#function that returns 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

# Damping Coefficient in kg/s
b=0.05
#Gravity in m/s^2
g=9.81
# Length in metre
l=1
# Mass in Kg
m=1

#initial condition
theta_0 = [0,5]

#time points
t = np.linspace(0,10,150)

#solve ODE
theta = odeint(model,theta_0,t,args = (b,g,l,m))

#plot results
plt.plot(t,theta[:,0],'b-')
plt.plot(t,theta[:,1],'r--')
plt.ylabel('plot')
plt.xlabel('time')
# plt.legend(loc='best')
plt.show()


x0 = 0
y0 = 0

alpha = theta[:,0]

ct = 1
for i in alpha:
x1 = x0 + (l*math.sin(i))
y1 = y0 - (l*math.cos(i))
ct = ct + 1
filename = str(ct) + '.png'
plt.figure()
plt.plot([-1,1],[0,0],'b-',linewidth=2)
plt.plot([x0,x1],[y0,y1])
plt.plot(x1,y1,'o', markersize = 20)
plt.xlim([-2,2])
plt.ylim([-2,2])
plt.title('Simple Pendulum')
plt.savefig(filename)

# clip = (VideoFileClip("video.mp4")
# .subclip((1,22.65),(1,23.2))
# .resize(0.3))
# clip.write_gif("video.gif")

Result / Output Of the code :

Figure -

Case 2 :-

Here, I change some input data like Damping Coefficient [b], initial condition and time.

# Damping Coefficient in kg/s
b=0.1
#Gravity in m/s^2
g=9.81
# Length in metre
l=1
# Mass in Kg
m=1

#initial condition
theta_0 = [0,3]

#time points
t = np.linspace(0,40,200)

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.