Challenge

Week 3 - Solving second order ODEs

Solving second order ODEs

AIM - To simulate the transient behaviour of a simple pendulum

OBJECTIVE -

  • To Write a program in python to solve the ODE that will simulate the pendulum motion.
  • Upload an animation on You tube and provide the You tube link for an animation in your report.

GOVERNING EQUATIONS

  • 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.
  • In Engineering, ODE is used to describe the transient behaviour of a system. A simple example is a pendulum.

In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

PYTHON CODE

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


# ODE function
def system(theta,t,b,g,l,m):
	theta1 = theta[0]
	theta2 = theta[1]
	dtheta1_dt = theta2
	dtheta2_dt = -(b/m)*theta2-g*math.sin(theta1)
	dtheta_dt = [dtheta1_dt,dtheta2_dt]


	return dtheta_dt


# Defining constants 
b = 0.05
g = 9.81
l = 5
m = 1

# initial conditions
theta_0 = [0,3]

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

# solving ode
theta = odeint(system,theta_0,t,args = (b,g,l,m))

# for animation and plots
f = 1
for i in range(0,240):
	filename = str(f) + '.png'
	f = f + 1
	plt.figure()
	plt.plot([10,l*math.sin(theta[i,0])+10],[10,10-l*math.cos(theta[i,0])],marker="o")
	plt.xlim([0,20])
	plt.ylim([0,20])

	plt.savefig(filename)


plt.plot(t,theta[:,0],'b-')
plt.plot(t,theta[:,1],'r--')
plt.show()

EXPLANATION –

  1. The initial values of g , l , m ,b and angular displacement and angular velocity are given.
  2. Then the timespan values are added from 0-20 seconds in 240 steps for smooth motion of the pendulum.
  3. The odeint code is then used to solve the ODE.
  4. For loop is used to predict the motion of the pendulum for different values of time.
  5. All the positions of the pendulum are then saved in different plots.
  6. The video is created by imagemagick and stored and uploaded to the youtube.

Output plots

Conclusion

The created file was an gif file, so cannot be uploaded to the youtube.

Therefore the gif file was attached here.

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.