Challenge

Week 3 - Solving second order ODEs

AIM: Write a program that will simulate the pendulum motion.

DESCRIPTION: In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum.

Figure1: A simple pendulum.

A simple pendulum is one that can be considered to be a point mass suspended from a string or rod of negligible mass. It is a resonant system with a single resonant frequency.

GOVERNING EQUATIONS: The way the pendulum moves depends on Newton's second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the "ball" w.r.t time.

(dθdt)2+bm⋅dθdt+gL⋅sin(θ)=0(dθdt)2+bm⋅dθdt+gL⋅sin(θ)=0

Parameters of the system will be defined by

g = gravity in m/s2

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

GIVEN:

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s

OBJECTIVES OF THE PROJECT

To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.

Figure2: Given the ODE of the pendulum(damped).

Figure3: Flow of the project.

Program:

import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
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.2
g=9.81
l=1
m=0.1
# Initial condition
theta_0 = [math.pi/2,0]
#time points
t = np.linspace(0,10,150)
# solve ODE
theta = odeint (model,theta_0,t,args=(b,g,l,m))
plt.plot(t,theta[:,0],'b-',label=r'$frac{dtheta_1}{dt}=theta2$')
plt.plot(t,theta[:,1],'r--',label=r'$frac{dtheta2}{dt}=-frac{b}{m}theta_2-frac{g}{l}sintheta_1$')
plt.xlabel('time')
plt.ylabel('plot')
plt.legend(loc='best')
plt.show()
# for animation of pendulum
ct = 1
for i in (theta[:,1]):
x0 = 0
y0 = 0
x1 = 1*math.sin(i)
y1 = -1*math.cos(i)
filename = str(ct) + '.png'
ct = ct + 1
print(filename)
plt.figure()
plt.plot([-1,1],[0,0],color="red",linewidth=4)
plt.plot([x0,x1],[y0,y1])
plt.plot(x1,y1,'o',markersize = 20)
plt.xlim([-1.5,1.5])
plt.ylim([-1.5,1.5])
plt.savefig(filename)

Output:

EXPLANATION OF CODE

  • Give input data as mentioned in the problem, as l,m,b,g.
  • For initialization time and angular velocity as '0' & '3' used respectively.
  • For defining the time span as 20 seconds and dividing it into 500 intervals.
  • By calling the function the differential equation is solved.
  • Plots are plotted with plot function and specified using the legend function in the plot.

Code for animation:


ct = 1
for i in (theta[:,1]):
x0 = 0
y0 = 0
x1 = 1*math.sin(i)
y1 = -1*math.cos(i)
lename = str(ct) + '.png'
ct = ct + 1
print(filename)
plt.gure()
plt.plot([-1,1],[0,0],color="red",linewidth=4)
plt.plot([x0,x1],[y0,y1])
plt.plot(x1,y1,'o',markersize = 20)
plt.xlim([-1.5,1.5])
plt.ylim([-1.5,1.5])
plt.savefig(filename)

Output:

https://drive.google.com/file/d/1b2XR0IZtRF9YXjb-1LdhJcxaGUzpCjp1/view?usp=sharing

Conclusion:

In this challenge, we are successfully simulated simple pendulum by using Second order ODE with the help of python function and through which we create a simulation video.

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.