Challenge

Week 3 - Solving second order ODEs

Gdrive: https://drive.google.com/drive/folders/1mwlGUaGJyMrs0zaj7CcsVDp8T3jkt2CQ

AIM

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

Program

  • import numpy as np
    from scipy.integrate import odeint # for intagration purpose
    import matplotlib.pyplot as plt
    import math
    
    def model (theta,t,b,g,l,m):
    	theta1= theta[0] # coverting the second order ODE into simpleform
    	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
    
    b=0.02 #damping coefficient.
    g=9.81 #gravity m/sec2
    l=1	   # length of the pendulum in m,	
    m=0.1  #mass of the ball in kg,
    
    #initial conditions
    theta_0=[0,5] #the angle between string and plane is 5
    
    #time points
    t=np.linspace(0,20,150)
    
    #solve ode
    theta= odeint(model,theta_0,t,args=(b,g,l,m))
    #plot results
    plt.plot(t,theta[:,0])
    plt.plot(t,theta[:,1])
    plt.ylabel('plot')
    plt.xlabel('time')
    plt.show()
    
    
    ct=1
    for theta in theta[:,0]:
    	x0=0
    	y0=0
    	x1=l*math.sin(theta)
    	y1=-l*math.cos(theta)
    	plt.figure()
    	plt.plot([-1,1],[0,0])
    	plt.plot([0,x1],[0,y1])
    	plt.plot(x1,y1,marker='o', markersize =30)
    	plt.xlim([-1.5,1.5])
    	plt.ylim([-1.5,1.5])
    	#plt.show()
    	
    	filename='test%05d.png' % ct
    	plt.savefig(filename)
    	ct=ct+1
    
    
    
    

Procedure

  • Import the required module. as in case we need the ODE scipy module
  • convert the second order ODE into simple single order ODE
  • Applying or define the inputs in python such mass,gravity,daming factor
  • Intial displacement is 0 and velcoity is 5m/s
  • the total time is 20ms and the input 20 is spillited into 150 to plot curve
  • the Orange line indicate the velcoity and the bule is displacement

OUTPUT

Result

from this assignment we learn the how to slove the second order ode by using the python

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.