Challenge

Week 3 - Solving second order ODEs

Aim- To solve an 2nd order ordinary diffrential equation by using python and to make an animation showing the pendulum.

Theory- A diffrential equation is involved with one or more derivatives which includes the study of changes, A derivative is a ratio of chnage in one to other, like dy/dx means that change of y , with respect to x . so here x is an independent variable and y y is an dependent variable.

  • There are two types of differential equations - ordinary differential equation and partial differential equation.
  • An differential equation helps in finding or solving various engineering problems like- in fluid dymanics, thermal studies.
  • There are various numericaal method made by using differental equation the mopst famous was taylor series method, it has some dis advantage so a new more effective method which is Runge Kutta method (RK method) is used and the solver i python works on this method.
  • In python a solver is used called as odeint solver its is a solver found in scipy module.

Approch of solving in python-

  1. Discretisation of ODE- It involves to write a differential equation into linear equations.
  2. Initialising the input variables.
  3. Using odeint solver for solving.
  4. Making a pendulum animation.

Input variables are shown in code with names.

Python code-

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

def model(theta,t,b,g,l,m) :
	theta1 = theta[0]
	theta2 = theta[1]
	dtheta1_dt = theta2
	dtheta2_dt = -(b/m)*theta2-(g/l)*cmath.sin(theta1)
	dtheta_dt = [dtheta1_dt,dtheta2_dt]
	return dtheta_dt

#Input patrameters
b = 0.02  #Damping coefficient
g = 9.81  # acceleration due to gravity
l = 1     # length if string
m = 0.1   # mass of bob

theta_0 = [0,5] #angle

t = np.linspace(0,10,150) #time span from 0-10






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


#for animation 
ct = 1
x0 = 0
y0 = 0

for i in theta[:,0] :
	x1 = x0+(1*cmath.sin(i))
	y1 = y0+(-1*cmath.cos(i))

	filename = str(ct)+'.png'
	ct = ct + 1

	plt.figure()

	plt.plot([-1,1],[0,0],"black")
	plt.plot([x0,x1],[y0,y1],"red")
	plt.plot(x1,y1,'o', markersize = '30')
	plt.xlim([-1.5,1.5])
	plt.ylim([-1.5,1.0])
	plt.savefig(filename)





Animation vdo is attached.

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.