Project

Solving 2nd Order ODEs

OBJECTIVE:-

  • Write a program in python to simulate the motion of the pendulum.
  • To show the animated pendulum.

THEORY:-

In the Equation,

`(d^2theta)/(dt^2)+b/m*(d theta)/dt+g/l*sin theta=0`, where

`b` = damping co-efficient= `0.05`

`m` = mass = `1 kg`

`g` = gravity = `9.81 m/s^2`

`l` = Length = `1 m`

PROCEDURE:-

Following is the code which I wrote for the simulation of the pendulum also for the animation of the same.

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

# function that returns dz/dt
def model(theta, t, b, g, L, m):
	theta_1 = theta[0]
	theta_2 = theta[1]
	dtheta1_dt = theta_2
	dtheta2_dt = -(b/m) * theta_2 - (g/L) * math.sin(theta_1)
	dtheta_dt = [dtheta1_dt, dtheta2_dt]
	return dtheta_dt

# Given parameters
b = 0.05            # damping
g = 9.81            # gravity
L = 1               # length
m = 1               # mass

# initial condition
theta_0 = [0,3]

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

# solving ODE
theta = odeint(model, theta_0, t, args=(b, g, L, m))

# plot results
plt.plot(t, theta[:, 0], \'b-\', label=r\'$\\frac{d\\theta_1}{dt}=\\theta_2$\')
plt.plot(t, theta[:, 1], \'r--\', label=r\'$\\frac{d\\theta_2}{dt}= -\\frac{b}{m}\\theta_2-\\frac{g}{L}sin\\theta_1$\')
plt.ylabel(\'Amplitude\')
plt.xlabel(\'Time\')
plt.legend(loc=\'best\')
plt.show()

# counter number
ct = 1

# for showing pendulum motion
for theta in theta[:, 0]:
	
	a0 = 0
	b0 = 0
	a1 = -L*math.sin(theta)
	b1 = -L*math.cos(theta)

	plt.figure()
	plt.plot([-1,1],[0,0], \'black\', linewidth = 10)
	plt.plot([a0, a1], [b0, b1], \'black\', linewidth = 2)
	plt.xlim(-2, 2)
	plt.ylim(-2, 2)
	plt.plot(a1, b1, \'o\', markersize=30)
	plt.xlabel(\'X-axis\')
	plt.ylabel(\'Y-axis\')
	plt.title(\'Pendulum Motion\')
	filename = str(ct) + \'.png\'
	plt.savefig(filename)
	ct = ct+1

OUTPUT:-

\"\"

The above figure shows the Amplitude vs. Time graph of the pendulum in motion.

\"\"

The above .gif shows the animation of Pendulum in motion.

EXPLAINATION:-

  • We used the function in the program to store the values for each value of `theta`. For this we used the command def under which every values were stored.
  • To solve the differential equation, we used a different library package named scipy.integrate. In this we used a command odeint.
  • We used the linspace command to store the time points of the motion of pendulum. We applied for loop to use each and every of the stored value to form an animated pendulum.

These were the 3 main steps of this program to run successfully.

LEARNING OUTCOME:-

  • We learnt the way to solve the Differential functions.
  • We learnt the the use of the commands odeint and linspace.

CONCLUSIONS:-

  • The graph was successfully generated of the pendulum motion.
  • The animated pendulum was successfully created after stitching all the .png files.
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.