Challenge

Week 3 - Solving second order ODEs

Week 3 - Solving second order ODEs

Solving second order ODEs

AIM - To simulate the transient behaviour of a simple pendulum

OBJECTIVE -

  • To Write a program in python to solve the ODE that will simulate the pendulum motion.
  • Upload an animation on You tube and provide the You tube link for an animation in your report.

GOVERNING EQUATIONS

  • The way the pendulum moves depends on the Newtons second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the "ball" w.r.t time.
  • In Engineering, ODE is used to describe the transient behaviour of a system. A simple example is a pendulum.

In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

PYTHON CODE

import numpy as np

from scipy.integrate import odeint

import math

import matplotlib.pyplot as plt

 

 

                                                                    # ODE function

def system(theta,t,b,g,l,m):

         theta1 = theta[0]

         theta2 = theta[1]

         dtheta1_dt = theta2

         dtheta2_dt = -(b/m)*theta2-g*math.sin(theta1)

         dtheta_dt = [dtheta1_dt,dtheta2_dt]

 

 

         return dtheta_dt

 

 

                                                                  # Defining constants

b = 0.05

g = 9.81

l = 5

m = 1

 

# initial conditions

theta_0 = [0,3]

 
 
                                                       #time plot

t = np.linspace(0,20,240)

 

                                                       # solving ode

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

 

                                                       # for animation and plots

f = 1

for i in range(0,240):

         filename = str(f) + '.png'

         f = f + 1

         plt.figure()

         plt.plot([10,l*math.sin(theta[i,0])+10],[10,10-l*math.cos(theta[i,0])],marker="o")

         plt.xlim([0,20])

         plt.ylim([0,20])

 

         plt.savefig(filename)

 

 

plt.plot(t,theta[:,0],'b-')

plt.plot(t,theta[:,1],'r--')

plt.show()

EXPLANATION –

  1. Given the initial values ​​of g, l, m, b and angular displacement and angular velocity.

Thereafter timespan values ​​were entered from 0-20 seconds with 240 steps of smooth pendulum movement.

  1. The odeint code is still used to resolve ODE.
  2. With a loop it is used to predict the movement of the pendulum at different time intervals.
  3. All pendulum positions are automatically maintained at various sites.
  4. The video is made with an image and stored and uploaded to youtube.

Output plots

Conclusion

The created file was an gif file, so cannot be uploaded to the youtube.

Reference: https://en.wikipedia.org/wiki/Ordinary_differential_equation

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.