Project

Solving Second Order ODE of a simple pendulum using PYTHON

Simple Pendulum:-

This is a program to solve a second order differential equation of a simple pendulum.

The youtube link for the animation is =https://youtu.be/lgdDNwfSoSg

The equation of oscillation of a simple pendulum is a second order differential eqution, which has been mentioned in the question. To achieve a real life animation of the pendulum, we need to solve this equation using PYTHON.

Solution:-

The given inputs for the question are:-

damping coefficient(b) = 0.05;

length = 1 m

mass = 1 kg

initial disp = 0

initial velocity = 3 rad/s

For the required time I created a variable called \'t\' which stores 150 values between 0-2- seconds. Then, I used the odeint solver provided in the scipy.integrate module which solves a second order equation. The required inputs for this solver were the angle, time and another custom function which defines the relation between all the input values.

For the custom function, the math solution is given below:-

The second order ode function is given as:-

d^i/dt^2 +(b/m)di/dt + g/l sin i where i=\'theta\'

So I considered \'theta\' as \'t1\' and then di/dt as \'t2\'

so, therfore we get dt1 = t2 and dt2 = (-b*t2/m)-(g*sin(t1)/l)

Now an array named \'dt\' was defined which contains the values dt1 and dt2 which are the required values to solve the ode.

Now, the resultant of th ode function was stored in an array named \'sol\' which has output as time and results.

Now, to create an animation a region on the graph needed to be defined , that is the boundary conditions and all. And an iterative process was carried out which calculates the postion of the pendulum at each interval of time. For, this purpose a for loop was used.

Inside, the for loop, I first created a counter variable which getsincremented after each interval, this is required to store each image generated , with a different filename. Then, I defined the boundary conditions, then the fixture, string and the ball of the pendulum. To make the ball, the marker command was used. Each image was stored by the counter name along with \'.png\' to make it an image file. These images were then stitched together to convert them into a gif file, which gives a nice animation of the pendulum. Then, later the gif file was converted into a videofile.

We can alternatively change the b or l or m values to get different animations and study the nature of the pendulum in real time.

Hence, this is te advantage of making a realtime animation curve .

The code for the program is given below:-

\'\'\'
Program to solve a second order ode 
\'\'\'
import math
import numpy as np 
import matplotlib.pyplot as plt 
from scipy.integrate import odeint 

#function for solving the ode parameters
def func(beg,t,b,l,g,m):
	t1=beg[0]
	t2=beg[1]
	dt1=t2
	dt2=(-b*t2/m)-(g*math.sin(t1)/l)
	dt=[dt1,dt2]
	return dt

#main program
b=0.05
l=1
g=9.81
m=1

value = [0,3]
t=np.linspace(0,20,150)
sol = odeint(func,value,t,args=(b,l,g,m))

#plotting
a = sol[:,0]
c=1             #counter
for i in a:
	x1=0
	y1=0
	x2=l*math.sin(i)
	y2=-l*math.cos(i)
	file=str(c)+\'.png\'
	plt.figure()
	plt.plot([-1.5,1.5],[0,0],linewidth=6)
	plt.plot([x1,x2],[y1,y2],linewidth=3)
	plt.plot(x2,y2,\'o\',markersize=20)
	plt.xlim([-1.5,1.5])
	plt.ylim([-1.5,1])
	plt.savefig(file)
	c=c+1

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.