Challenge

Week 3 - Solving second order ODEs

ANSWER:

GIVEN DETA IN ABOVE EQUATION :-

gravity (g) =9.81m/s^2

length (l) = 1 metre

mass (m) = 1 kg

damping coefficient (b) = 0.05

stimulating the motion between 0-20 sec, for angular dispalcement = 0,

angular velocity = 3rad/sec at time = 0

THEORY:

1. A simple pendulam is defined to have an objects that has a small mass, also known as pendulam bob, which is suspended from a light wire or string.

2. in engineering ODE is used to describe the transient behavior of a system. a simple example is pendulam. the way the pendulam moves depends on the newtons second law. when this law is written down, we get second order ordinary diffrential equation that describes the position of the ball with respect to time.

SIMPLIFYING EQUATION:

d^2theta/dt +b/m*dtheta/dt + g/l *sin theta = 0

above equation are second order ode form we will solve using python

theta = theta

dtheta/dt = dtheta1/dt = theta 2............eq 1

d^2theta/dt^2 = d^2theta 1/dt^2 = d/dt*(dtheta1/dt) = d/dt*theta2..........eq 2

simplifying above equations

d^2theta/dt^2 + b/m* dtheta/dt + g/l sintheta = D

d^2theta1/dt^2 + b/m*dtheta 1/dt + g/l sin theta 1

dtheta 2/dt + b/m*theta2 + g/l sintheta 1 = D

dtheta1/dt = theta 2 AND dtheta2/dt = -b/m*theata2-g/m*sintheta1

d/dt vertical matrix [theta 1 theta 2] = [-b/m theta 2-g/l sin theta]

considering above first bracket as theta 1 and second as theta 2

CODING STEPS :

1. initial condition

theta_ =[math.pi/2, theta]

from above equation we found out,

math.pi = displacement , theta = velocity

2. in this case 2 ordinary diffrential equation of first order we providing intial condition for both ODE's

3. Then we will create time array.

4. to solve the equation we used odient command in scipy module.

where, odient is the function which integrates ordinary diffrential equation.

5. model is function which represents ODE's system.

in the program codes,

line - 11 is 1st order ODE's

line- 12 is 2nd order ODE's

ERROR FACED WHILE PROGRAMMING:

1. SYNTAX ERROR

2. IDENTATION ERROR

3. ODIENT ERROR

STEPS TAKEN TO OVERCOME THE ERROR:

1. WE CHECKED ERROR ACCORDING TO OUTPUT WINDOW RESULTS THEN IDENTIFIED ERROR AND THEN RE-WRITEN CODES OR SYMBOLS.

2. PROVIDED NEEDED SPACE AND TABS TO THE CODES.

3. IN CODES WE IMPORTED SCIPY MODULE.

CODES:

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




# function that returns dz/dt
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	
b=0.02
g=9.81
l=1
m=0.1

# intial condition
theta_0 = [0,3]

# time points
t = np.linspace(0,10,150)
	

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

# animation
ct = 1
theta_1 = theta[:,0]
for t in theta_1:
	x1 = l*math.sin(t)
	y1 = -l*math.cos(t)

	x0 = 0
	y0 = 0
	filename = 'pendulum%05d.png'%ct
	ct=ct+1

	plt.figure()
	plt.plot([-0.5,0.5],[0,0],'g',linewidth = 3)
	plt.plot([x0,x1],[y0,y1],'r',linewidth = 3)
	plt.plot(x1,y1,'o',markersize = 30)
	plt.xlim([-1.5,1.5])
	plt.ylim([-1.5,1.5])
	plt.title('Animation of a Simple Pendulum using Python')
	plt.savefig(filename)

RESULTS:

YOUTUBE VIDEO LINK

https://youtu.be/_upr-Wrzzic

NOTE - screeshots of errors are uploded.

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.