Challenge

Week 3 - Solving second order ODEs

AIM:  To animate the swing of a simple pendulum and replicate its fleeting character.
OBJECTIVE : 
1.	To write a program in Python that solves the second-order ODE corresponding to the motion of a simple pendulum and to plot its angular displacement and angular velocity wrt time.
GOVERNING EQUATIONS :
To solve the above equation in Python, we need to transform the given second order ODE into first order ODE as, 
                               ⇒  θ=θ1 ,
                               ⇒d(θ)dt=d(θ1)dt=θ2 ,          
                              ⇒d2θdt=d2θ1dt=d(θ2)dt ,      
                               ⇒d2θdt+(bm)⋅d(θ)dt+(gL)⋅sinθ=0 ,
                               ⇒d2θ1dt+(bm)⋅d(θ1)dt+(gL)⋅sinθ1=0 ,
                               ⇒d(θ2)dt+(bm)⋅θ2+(gL)⋅sinθ1 ,
                               ∴,d(θ1)dt=θ2  and  d(θ2)dt=−(bm)⋅θ1−(gL)⋅sinθ1
Represent them in matrix form,
                               ⇒ddx[θ1θ2]=[θ2−(bm)⋅θ2−(gL)⋅sinθ1] ,
 AREA OF CONTENTS: 
# Steps followed#
1. The first thing done was to load the math, matplotlib, numpy, and scipy modules for their respective functions.
2. Initial inputs like b, g, l, and m are given in accordance with the information in the query.
3. To specify beginning angular displacement and initial angular velocity, an array of initial theta was generated.
4. To hold 100 evenly spaced intervals between 0 and 10 seconds, a time span array was made.
5. A function called ode function, which contains the aforementioned matrix data, is used to solve the second-order ODE.
6.The specified second-order ODE was solved by using the 'odeint' command and the calling function's correct syntax, and the solution was then utilised to produce a graph showing the change in angular displacement and angular velocity with respect to time.
7.A loop counter variable was started and a for loop was used to create an animation of the pendulum's acquired harmonic motion. The values of the angular position are saved in a different variable named "tmp" and are iterated in the for loop. In this execution, one end of the pendulum string was fixed, and the other end moved in accordance with the ODE solution that was found and plotted for each instant of the time period.
conclusion :- 
In conclusion, Python can be used to solve any second-order or higher-order ODE, and a simulation of the transient behaviour of a basic pendulum was created using it to show how damping affects its motion.

MAIN CODE : 

# Simulation of motion of pendulum 

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

# function that return 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

# Input
b=0.05  # damping coefficient.
g=10  # gravity in m/s2
l=0.75    # length of the pendulum in m
m=1   # mass of the ball in kg

# Initial condition
theta_0 = [0,5]

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

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

# Plotting
plt.figure(1)
plt.plot(t,theta[:,0],'b-',label=r'$\frac{d\theta1}{dt}=\theta2$')
plt.plot(t,theta[:,1],'r--',label=r'$\frac{d\theta2}{dt}=-\frac{b}{m}\theta2-\frac{g}{l}sin\theta1$')
plt.xlabel('Time')
plt.ylabel('Dispacement & Velocity')
plt.legend(loc = 'best')
plt.show()

# Iteration using for loop to create animation of motion of pendulum

loop_counter =1
tmp = theta[:,0]
for i in tmp:
	x_start = 0                                                           
	y_start = 0
	x_end =  l*math.sin(i)                                          
	y_end = -l*math.cos(i)
	# Plotting for animation
	plt.figure()
	# Plotting String of pendulum
	plt.plot([x_start,x_end],[y_start,y_end],linewidth=3)
	# Plotting Bob of pendulum
	plt.plot(x_end,y_end,'o',markersize=20,color='r')
	# Plotting the rigid support through which string of pendulum is attached
	plt.plot([-0.5,0.5],[0,0],linewidth=8,color='k')
	# Plotting hatched vertical line for refernce
	plt.plot([0,0],[0,-1.5],'--',color='y')
	plt.xlim([-1.25,1.25])
	plt.ylim([-2,1])
	plt.title('Animation of transient behaviour of a Simple Pendulum')
	filename = 'test%05d.png' % loop_counter
	plt.savefig(filename)
	loop_counter = loop_counter + 1
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.