Project

Simple Pendulum using ODE

Introduction:

A pendulum is a body suspended from a fixed support so that it swings freely back and forth under the influence of gravity. When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position. When released, the restoring force acting on the pendulum's mass causes it to oscillate about the equilibrium position, swinging back and forth. The mathematics of pendulum are in general quite complicated.

Simplifying assumptions can be made, which in the case of a simple pendulum allow the equations of motion to be solved analytically for small-angle oscillations.

A so-called "simple pendulum" is an idealization of a "real pendulum" but in an isolated system using the following assumptions:

  • The rod or cord on which the bob swings is massless, inextensible and always remains taut;
  • The bob is a point mass;
  • Motion occurs only in two dimension, i.e. the bob does not trace an ellipse but an arc.
  • The motion does not lose energy to friction or air resistance.
  • The gravitational field is uniform.
  • The support does not move.

In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum

The way the pendulum moves depends on the Newton’s 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.

(dθ)^2/(dt^2 )+b/m*dθ/dt+g/L*sinθ=0

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.


                                         θ= θ1

                                    dθ/dt=dθ1/dt= θ2

                                 (dθ)^2/(dt^2 )=dθ2/dt

Now, using these equations in the Damping Equation,

                               dθ2/dt+b/m*θ2+g/L*sinθ1=0
 
                               dθ2/dt= -b/m*θ2-g/L*sinθ1    This is the angular velocity.    

                                      θ2=dθ1/dt         This is the angular displacement.

                     (d/dt)(θ1¦θ2)= ((-b/m*θ2-g/L*sinθ1)¦(dθ1/dt  ))

We assume the matrix (θ1¦θ2) as a single value θ.

Therefore,  
               
                       dθ/dt= ((-b/m*θ2-g/L*sinθ1)¦(dθ1/dt))
   

 

The mathematical code written in python for the simple pendulum is appended below:

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

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
	
	print(theta1)

# Inputs 

b = 0.05
g = 9.8
l = 1
m = 1

# Initial Conditions for Displacement and Velocity

tmp = [0,3]

# Time Range

t = np.linspace(0,20,150)

theta = odeint(model,tmp,t,args = (b,g,l,m))
disp = theta[:,0]
velocity = theta[:,1]

plt.figure()
plt.plot(t,theta[:,0])
plt.plot(t,theta[:,1])
plt.xlabel('Time')
plt.ylabel('Oscillations')
plt.show()

x0 = 0
y0 = 0

ct = 1
for Theta in disp:

	x1 = l*math.sin(Theta)
	y1 = -l*math.cos(Theta) 
	filename = str(ct) + '.png'
	ct = ct + 1

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

Outputs:

The youtube link for the Simple Pendulum: https://www.youtube.com/watch?v=2l5ELCtBmEg

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.