Challenge

Solution of second order ODEs

Solution of second order ODEs

AIM: To solve a second order ordinary differential equation of a pendulum and find the position of the pendulum at different timestep using python programming. Also animate the movement of the pendulum and show the result.

PROGRAM

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

#FUNCTION THAT RETURN dz/dt
def mode1(theta,t,b,g,L,m):
	b=0.2
	g=9.81
	l=1
	m=0.1

	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.05
g=9.81
l=1
m=0.1
#INITIAL CONDITION
theta_0=[math.pi/3,math.pi/2]

#TIME POINTS
t=np.linspace(0,20,300)

#SOLVE ODE
theta=odeint(mode1,theta_0,t,args=(b,g,l,m))
disp1=theta(:,1)
vel1=theta(:,2)

#PLOT RESULTS
plt.plot(t,theta[:,0],'b-',label=r'$frac{dtheta_1}{dt}{dt}=theta2$')
plt.plot(t,theta[:,1],'r--',label=r'$frac{dtheta2}{dt}=-frac{b}{m}theta_2-frac{g}{L}sintheta_1$')
plt.ylabel('Displacement and Velocity')
plt.xlabel('time')
plt.legend(loc='best')
plt.show()

x0=0
y0=0

ct=1
y=theta[:,0]

for angle in y:
	x1=0
	y1=2
	x2=-1*math.sin(angle)
	y2=-1*math.cos(angle)

	filename= '%05.png' %ct
	ct=ct+1

	plt.figure()
	plt.plot([x1,x2],[y1,y2],linewidth=2)
	plt.plt([-1.5,1.5],[2,2],linewidth=5)
	plt.xlim([-2.5,2.5])
	plt.ylim([-2.5,2.5])
	plt.title('Pendulum animation')
	plt.savefig(filename)


PRINCIPLE:

In the given problem we are asked to find the oscillation in a simple pendulum. Osceillation in a simple pendulum indicates the transient system. The motion is dependent on space and time. These motions are represented using second order ordinary diffential equations. In the given problem we make use of a pendulum with a mass of 1 Kg, length 1m. Gravitational constant =9.81 m/s^2.

We make use of the ode45 function to convert second order ordinary differential equation into two first order euations.

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

odeint is the function implements Runga-Kutta method with variable timestep of effecient computation. The function initIally reduces the given equation of motion into 2 first order equation.

The function odeint redirects to another function named ode_func which is an ordinary function. The output of ode45 function is the time and the angle of each timestep. The function ode_func takes the value of time, theta, damping coefficient, gravitational constant, length of rod and mass of the ball as inputs

The division of second order equation into 2 first order equation is dine in the named function.

Thus we have find out two dtheta values which is used to find the angle.

function[dtheta_dt]=ode_func(t,theta,b,g,l,m)
theta1=theta(1);
theta2=theta(2);

dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);

[dtheta_dt]=[dtheta1_dt;dtheta2_dt];
end

RESULTS

The animated file of the pendulum can be viewed by clicking on the below link

https://drive.google.com/file/d/1SA6bby-iDkfymoKkfei0ObukMxKIJFxE/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.