Challenge

Week 3 - Solving second order ODEs

solving second order ode:

Aim:

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

introduction:

second order ode: The differential equation y'' + p(x)y' + q(x)y = 0 is called a second order differential equation with constant coefficients if the functions p(x) and q(x) are constants and it is called a second-order differential equation with variable coefficients if p(x) and q(x) are not constants.

in the unknown y(x) Equation (1) is 1st order seeing that the highest derivative that seems in it is a 1st order derivative. Similarly, equation (2) is a 2nd order because also y appear.

simple pendulum:

The simple pendulum comprises a small bob of mass 'm' suspended by a thin string secured to a platform at its upper end of length L. The simple pendulum is a mechanical system that sways or moves in an oscillatory motion. This motion occurs in a vertical plane and is mainly driven by the gravitational force.

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

In the above equation. we have to create a animation.

procedure:

open a new file in sublime text editor then save it as second.py (.py extension is important)

now write the below python code in the sublime text editor.

then build the code. it gives us 150 different images.

then by using the below link stich all the images to form a gif .

link: ezgif.com/jpg-to-gif

python logic:

d2Ө/dt2+(b/m)d Ө/dt+(g/l)sin Ө = 0 …………(1)

put Ө= Ө1

d Ө/dt = d Ө1/dt= Ө2 ………………………(2)

d2 Ө/dt2 = d2 Ө2/dt2 = d/dt(d Ө1/dt) = d Ө2/dt ………………(3)

then apply the (2) and (3) in (1)

d Ө2/dt + (b/m) Ө2 +(g/l)sin Ө1 = 0 ………………(4)

now, we have to solve equation(2) and (4)

d Ө2/dt = -(b/m) Ө2 –(g/l)sin Ө1

from the above we have calculated the

d Ө1/dt= Ө2

d Ө2/dt = -(b/m) Ө2 –(g/l)sin Ө1

python code:

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


def function(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

	theta1 =  theta[0]
	theta2 =  theta[l]
	dtheta1_dt = theta2
	dtheta2_dt =  -(b/m)*theta2-(g/l)*math.sin(theta1) 
	dtheta_dt = [dtheta1_dt,dtheta2_dt]
	return dtheta_dt

#input parameters:
b=0.05    #damping coefficient
g=9.81  #acceleration due to gravity
l=1      #length of string
m=1       # mass of bob.

theta_0=[0,3]   
t=np.linspace(0,20,150)

theta= odeint(function,theta_0,t,args=(b,g,l,m))

plt.plot(t,theta[:,0])
plt.plot(t,theta[:,1])
plt.show()



#creating animation:

ct=1
x0=0
y0=0

for i in theta[:,0]:
	x1=x0+l*math.sin(i)
	y1=y0+(-l*math.cos(i))

	filename=str(ct)+'.png'
	ct=ct+1

	plt.figure()
	plt.plot([-1,1],[0,0],'black')
	plt.plot([x0,x1],[y0,y1],'red')
	plt.plot(x1,y1,'o',markersize='20')
	plt.xlim(-1.5,1.5)
	plt.ylim(-1.5,1.0)
	plt.savefig(filename)

result:

the figure of time vs displacement and time vs angular velocity is shown below:

animation video :

https://drive.google.com/file/d/18rX5oZw8XW0k0b7oFG1DZ3frlAoMqdhJ/view?usp=sharing

conclusion:

here the second order ode for a simple pendulum is done.

The way the pendulum moves depends on the Newtons 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 is done by using odient inbuilt command in python.

-----------------------thank you----------------------------------

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.