Challenge

Week 3 - Solving second order ODEs

Aim: To simulate the motion of simple pendulum and to create the animation file of the motion of the pendulum.

Objective:

1. write a program to solve the second order differential equations which represent the simple pendulum motion.

2.To create an animation file, the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.

Theory:

A pendulum is a bob/ball of mass suspended form a wire/string.

A simple pendulum consists of a mass m hanging from a string of length L and fixed at a pivot point P. When displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion. By applying Newton's secont law for rotational systems, the equation of motion for the pendulum may be obtained

,

and rearranged as

.


If the amplitude of angular displacement is small enough that the small angle approximation () holds true, then the equation of motion reduces to the equation of simple harmonic motion

.


The simple harmonic solution is


with being the natural frequency of the motion. objective is to write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping

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.

we will solve the decond order ODE by splitting it into two first order ODEs.

Damped Simple Harmonic Motion

When the motion of an oscillator reduces due to an external force, the oscillator and its motion are damped. These periodic motions of gradually decreasing amplitude are damped simple harmonic motion. An example of a damped simple harmonic motion is a simple pendulum.

In the damped simple harmonic motion, the energy of the oscillator dissipates continuously. But for a small damping, the oscillations remain approximately periodic. The forces which dissipate the energy are generally frictional forces.

Stepwise explanation of code:

1. Import math,matplotlib,numpy and scipy.

2. defining the function to give the inputs.

3.defining the input values.

4.definig the angular velocity.

5.defining equation.

6.plotting the graphs.

7.plotting for animation images.

Pythin code for the given challange:

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

#function representing the ODE system
def pendulum (theta,t,b,m,l,g):
	theta_1 = theta[0]
	theta_2 = theta[1]
	dtheta1_dt = theta_2
	dtheta2_dt = -(b/m)*theta_2-(g/1)*math.sin(theta_1)
	dtheta_dt = [dtheta1_dt,dtheta2_dt]
	return dtheta_dt

# simple pendulum inputs
#damping co-efficient
b = 0.05
#mass of the pendulum in kg
m = 1
#length of the pendulum in m
l = 1
# gravity in m/s^2
g = 9.81

#initial conditions
theta_0 = [0,3]
#time points
time_span = np.linspace(0,20,200)
# solving the ODE using the scipy module
theta = odeint(pendulum,theta_0,time_span,args=(b,m,l,g))

#plotting the results
plt.plot(time_span,theta[:,0],'b-',label = r'$\frac{d\theta_1}{dt}=\theta_2$')
plt.plot(time_span,theta[:,1],'r--',label = r'$\frac{d\theta_2}{dt}=-\frac{b}{m}\theta_2-\frac{g}{l}sin\theta_1$')
plt.xlabel('Time(sec)')
plt.ylabel('plot')
plt.title('damped motion of simple pendulum')
plt.grid()
plt.show()

# "for" loop iteration to obtain the image of pendulum at different positions,create the animation
Anime = 1 
temp = theta [:,0]
for z in temp:
	x_begin = 0
	y_begin = 0
	x_end = 1*math.sin(z)
	y_end = -1*math.cos(z)
	#animation plotting
	plt.figure()
	# string plotting
	plt.plot([x_begin,x_end],[y_begin,y_end],linewidth=3)
	#plotting bob
	plt.plot(x_end,y_end,'o')
	#plotting the rigid support
	plt.plot([-0.5,0.5],[0,0],linewidth=8)
	#plotting vertical line for reference
	plt.plot([0,0],[0,-1.5])
	plt.xlim([-2,2])
	plt.ylim([-3,2])
	plt.title('damped motion of simple pendulum animation')
	filename = 'test%05d.png' % Anime
	plt.savefig(filename)
	Anime = Anime + 1
	print(Anime)

Errors:

1.The below error is the module has no atribute as linespace,this kind of error is because of gramatical error.which is recognized by the python intrepreter.

to rectify this error we need to provide the command without gramatical error as "linspace"

2.The below error unindent does not match any outer indendation level.the error came becaues if we use bot spaces and tabs while defining a loop .to recrify this use either space or tab.

Results:

The images below shows the damping effect of pendulum

The below file shows the animation file of simple pendulum

hence the criteria met.

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.