Project

SOLVING SECOND ORDER DIFFERENTIAL EQUATION USING PYTHON.

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

SECOND ORDER EQUATION FOR MOTION:

d²θ/dt²+(b/m)(dθ/dt) +(g/L)sin⁡θ=0

Consider dθ/dt=⁡θ1

Thus dθ²/dt²=dθ1/dt

Eq : d²θ/dt²+(b/m)(dθ/dt) +(g/L)sin⁡θ=0

Become dθ1/dt+(b/m)θ1+(g/L)sin⁡θ=0

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.

Now we write a program in Matlab that will simulate the pendulum motion, just like the one shown in the start of this challenge.

we use input:

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

we Simulate the motion between 0-20 sec, angular displacement=0,

angular velocity=3 rad/sec at time t=0.

PROGRAM FOR SIMULATION OF SIMPLE PENDULUM IN MOVIE FILE:

import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import math
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
b=0.02
g=9.81
l=1
m=0.1
#initial condition
theta_0=[math.pi/2,0]
#time points
t=np.linspace(0,10,32)
#solve ode
theta =odeint(model,theta_0,t,args=(b,g,l,m))
plt.plot(t,theta[:,0],'b')
plt.plot(t,theta[:,1],'r')
plt.ylabel('plot')
plt.xlabel('time')
plt.legend('best')
plt.grid()
plt.show()

x0=1;

y0=1;

ct=1;

cord = theta[:,0]

for i in np.length(theta[:,0]):

    x1 = 1-l*math.sin(cord(i))

    y1 = 1-l*math.cos(cord(i))

    filename = str(ct)+'.png';          ct=ct+1
    plt.figure()
    plt.show() 
    plt.plot([1,x1],[1,y1],[x1,x1],[y1,y1])
    plt.xlim([0,2])
    plt.ylim([0,2])
    plt.savefig(filename)

EXPLANATION:

1. we create array that store differential equation

2.function definition gives some attributes that Definevas function parameters.

3.the output of function is array storing solution attributes of equation.

OUTPUT MOVIE FILE:

CONCLUSION:

thus we obtain simulation of simple pendulum.

Here i use each png image and convert it into gif file.

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.