Challenge

Solving second order ODEs

OBJECTIVE:

1.To solve the second order differential equation for pendulum.

2.To create the animation of pendulum movement.

EQUATION FOR PENDULUM:

Where,

b is damping cofficient

m is mass in kg

g is gravity m/s^2

l is the length of string in meter

To solve second order differential equation firstly equation is converted into first order and below is the solution:

θ=θ1

(d2 θ1/dt2 )+(dθ1/dt)*b/m+(g/l)*sin⁡θ1=0

dθ1/dt= θ2

d2 θ1/dt2=dθ2/dt

dθ2/dt+(b/m)*θ2+(g/l)*sin⁡θ1=0

dθ2/dt=-(b/m)*θ2-(g/l)*sin⁡θ1

dθ1/dt=θ2

and also to craeat the animation of pendulum following equation will be used:

x = l*sinθ

y = -l*cosθ

PROGRAMING LANGUAGE USED:

Python programing language is used.

CODE FOR SOLVING ODE EQUATION AND FOR CREATING PENDULUM ANIMATION:

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

#creating a function to convert second order differential equation into ist order
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




#inputs
g = 9.81    #gravity(m/s^2)
l = 1     #length(m)
m = 1       #mass(kg)
b = 0.05    #damping cofficient

#initial conditions
theta_0 = [0,3]  #displacement = 0  angular velocity = 3

#time
t = np.linspace(0,20,500)

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

#plotting time and displacement graph
plt.figure()
plt.plot(t,theta[:,0],'r-',label=r'$\frac{d\theta_1}{dt} = \theta2$')
plt.plot(t,theta[:,1],'b-',label=r'$\frac{d\theta_2}{dt} = -\frac{b}{m}\theta2-\frac{g}{L}sin\theta1$')
plt.xlabel('time')
plt.ylabel('plot')
plt.legend()
plt.show()



#pendulum animation

ct = 1       # frame counting variable
x0 =  0      #fixed point of string
y0 = -0.2

for i in range(len(t)):
		# lower ponts of pendulum string
	 x1 =  l*math.sin(theta[i,0])
	 y1 = -l*math.cos(theta[i,0])
	 
	 anim =  'test%05d.png' % ct
	 ct =ct+1

	 plt.figure()
	#for ploting string
	 plt.plot([x0,x1],[y0,y1],[-0.6,0.6],[-0.2,-0.2])
	# creating pendulum bob
	 plt.plot(x1,y1,marker='o',markersize='30')#
	 plt.axis([-1,1,-1.35,0])
	 plt.savefig(anim)
	

CODE EXPLANATION:

1.first of all import the important module such as math for trignometry , matplotlib for plot, numpy for array and scipy for ode solver.

2..A function is created in which the second order differential equation is broke into first order equation as shown in the begining of the c

3.After that, the value of constants is defiend such as damping coffiecient , length , mass and gravity.

4.define the initial value of displacement and angulat veocity, and time span is set.

5.After that, solving the equation using odeint and by calling the already created function.

6.Plot the diaplacement and angular veloity against time.

7.To creating the animation of pendulum 'for loop' is used and ct variable is used to creat frames.

8.Further, fixed end and changing end of the string is made by using the trignometric equation as mentioned above.

9.After that, plotting the string and bob using plot command.

8. creating animation by saving the plots using savefig command and all the saved plot can be stiched and coverted into gif format using magick command in command window as shown below.

ERROR FACED:

The above error was faced while executing the program as return command was not used to return the value of function,But as soon as i read the error in command window i rectified the mistake.

OUTPUT:

Plot of time and displacement:

link of animation of pendulum is below:

https://drive.google.com/file/d/1mR7WLwt1pyDCypmYyWlJDoGkoiY9mW69/view?usp=sharing

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.