Challenge

Week 3 - Solving second order ODEs

The animation is uploaded to Google drive, below is the link to this location.

GDrive_Location

The above link has an animated GIF, screenshots of images denoting each location of pendulum motion, and the t-theta graph.

Code Explanation:

#import required modules
import math
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from PIL import Image
from matplotlib.animation import FuncAnimation

#function for ODE solution
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

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

#Initical Condition
theta_0 = [0,math.pi/2]

#time Points
t = np.linspace(0,20,150)

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

#plot Result
plt.figure(1)
plt.plot(t,theta[:,0],'b-',label=r'$\frac{d\theta_1}{dt}=\theta2$')
plt.plot(t,theta[:,1],'r--',label=r'$\frac{d\theta2}{dt}=-\frac{b}{m}\theta_2-\frac{g}{l}sin\theta_1$')
plt.ylabel('Plot')
plt.xlabel('Time')
plt.legend(loc ='best')
plt.savefig('t-theta graph.png')

All_plot = []                                       # list to store all images
count = 1

#plot multiple psition of pendulam
for i in range(0,len(theta)):
    Theta = theta[i][0]
    x0,y0 = 0,0                                     # Initialise our plot
    x1,y1 = l*math.sin(Theta),-l*math.cos(Theta)    # Define Bob cordination
    plt.figure(2)
    plt.plot([x0,x1],[y0,y1])
    plt.plot(x0,y0,'gs',markersize = 10)            # plot rectangular hanging point in green color
    plt.plot(x1,y1,'ro',markersize=25)              # plot circuluar bob in red color 
    plt.xlim(-1.5,1.5)
    plt.ylim(-1.75,0.5)
    plt.axis('off')

    file_name1 = str(count) + '.png'
    count = count + 1
    plt.savefig(file_name1)                         # save plot as png image file
    Abd = Image.open(file_name1)                    # open image file to create GIF
    All_plot.append(Abd)
    plt.close()


All_plot[0].save('Pendulam_animation.gif', format='GIF',      #create GIF file
            append_images=All_plot[1:],
            save_all=True,
            duration=250, loop=0)


#done

Errors faced while programming:

I faced a problem in drawing a bob and square base to hold the pendulum.

Got the solution to this from the format string argument of matplotlib.pyplot.plot from https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html

Thanks,

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.