Project

Solving Second Order ODE (Simple Pendulum)

AIM :

To animate a simple pendulum using Python.

Problem Statement :-

Write a Python Program to solve following equation.

`frac {d^2 theta}{dt^2} + frac {b}{m} frac {d theta}{dt} + frac{g}{L} sin theta = 0`

This ODE represents the equation of motion of simple pendulum with damping.

Create animation of simple pendulum using above ODE.

Data Given :-

Length of pendulum L = 1 metre

Mass of the ball m = 1 kg.

Damping Coefficient b = 0.05

Gravitational Acceleration g = 9.81 m/`s^2`

time span t = 0 to 20 seconds

at t = 0 angular displacement = 0 and angular velocity = 3 rad/sec. [Initial Conditions]

Theory :-

ODEs :-

  • An ODE is an equation that contains one independent variable(e.g. time) and one or more derivatives with repect to that independent variable.
  • In the time domain, ODEs are initial-value problems, so all the conditions are specified at the initial time t=0.
  • These solvers can be used with the following syntax:

Python Code :

Google Colab used for Python Coding.

'''Python Project 4 : Solving Second Order ODE (Simple Pendulum)'''
import numpy as np
from scipy.integrate import odeint
import math
import matplotlib.pyplot as plt

# Define function
def odefunction(theta,t,b,g,l,m):
  # Define solution array theta
  theta1 = theta[0]
  theta2 = theta[1]
  # define both solution equations
  dtheta1_dt = theta2
  dtheta2_dt = -(b/m)*theta2-(g/1)*math.sin(theta1)
  # define final solution vector
  dtheta_dt = [dtheta1_dt,dtheta2_dt]
  # Output of Function
  return dtheta_dt

# Input Data
b = 0.05   # Damping Coefficient
g = 9.81   # Acceleration Due to gravity in m/s^2
l = 1      # Length of Pendulum String in meter
m = 1      # Mass in Kg

# Initial condition zero for displacement and 3 for velocity
theta_0 = [0,3]

# Time points
t_span = np.linspace(0,20,200)

# Solve ODE
result = odeint(odefunction,theta_0,t_span,args=(b,g,l,m))

# Plot Result (Dispalcement and Velocity Change with Time)
plt.plot(t_span,result[:,0],'b-',label=r'$Displacement$')  # Time and displacement plot
plt.plot(t_span,result[:,1],'r--',label=r'$Velocity$')  # Time and velocity plot
plt.ylabel('Displacement and velocity')
plt.xlabel('Time')
plt.legend()
plt.title('Change in dispacement and velocity with repect to time')
plt.show()

# Image generation for animation
k=1;  # Constant define to get figure for animation
z=result[:,0]
for i in z:
    x0=0
    y0=0
    x1=1*math.sin(i) # Position of Ball in x-direction
    y1=-1*math.cos(i) # Position of Ball in y-direction

    # Define plot file name for generate animation
    filename = str(k) + '.png'
    k=k+1
    print(filename)

    # Plot Animation
    plt.figure()
    plt.plot([-1.5,1.5],[0,0],linewidth=4); # Upper line where pendulum is fix
    plt.plot([x0,x1],[y0,y1],linewidth=2); # Pendulum length string
    plt.plot(x1,y1,'o',markersize=20); # Ball of Pendulum
    plt.title('Motion of Pendulum')
    plt.xlim([-1.5,1.5])
    plt.ylim([-1.5,1.5])
    plt.savefig(filename)
  

Result :

1. This codes give change in displacement and velocity with repect to time as follow.

2. This code generate 200 images of motion of pendulum from which some images are as follow.

Animation Link :-

By combining all this 200 images we create animation of motion of pendulum.

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.