Solving second order ODEs to simulate the transient behaviour of a simple pendulum
Simple pendulum
A pendulum is a weight suspended from a pivot so that it can swing freely. When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position. When released, the restoring force acting on the pendulum\'s mass causes it to oscillate about the equilibrium position, swinging back and forth. The time for one complete cycle, a left swing and a right swing, is called the period. The period depends on the length of the pendulum and also to a slight degree on the amplitude, the width of the pendulum\'s swing.
Programme:-
from math import*
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
#initial condition
st=0
et=20.4
ts=0.1
g=9.81
L=1
b=0.05
m=1
#1st order equation to solve in a function
def sim_pen_eq(t,theta):
dtheta2_dt=(-b/m)*theta[1]+(-g/L)*np.sin(theta[0])
dtheta1_dt=theta[1]
return [dtheta1_dt,dtheta2_dt]
theta1_ini=0
theta2_ini=3
theta_ini=[theta1_ini,theta2_ini]
t_span=[st,et+ts]
t=np.arange(st,et+ts,ts)
sim_points=len(t)
l=np.arange(0,sim_points,1)
theta12=solve_ivp(sim_pen_eq, t_span, theta_ini, t_eval=t)
theta1=theta12.y[0,:]
theta2=theta12.y[1,:]
plt.plot(t,theta1,label=\'angular displacement (rad)\')
plt.plot(t,theta2,label=\'angular velocity(rad/s)\')
plt.xlabel(\'time(s)\')
plt.ylabel(\'angular disp.(rad) and angular velocity(rad/s)\')
plt.legend()
plt.show()
x=L*np.sin(theta1)
y=-L*np.cos(theta1)
for point in l:
plt.figure()
plt.plot(x[point],y[point],\'bo\',markersize=20)
plt.plot([0,x[point]],[0,y[point]])
plt.xlim(-L-0.5,L+0.5)
plt.ylim(-L-0.5,L+0.5)
plt.xlabel(\'X direction\')
plt.ylabel(\'Y direction\')
filenumber=point
filenumber=format(filenumber,\'05\')
filename=\'image{}.png\'.format(filenumber)
plt.savefig(filenumber)
plt.close()
Graph:-
Simulation of simplependulum:-
Youtube link
Error\'s occured:-
1) In line no 40 ,I entered marker =20.
by entering correct index as markersize=20 i corrected that error.
2)In line number 16 I entered theta(1) & theta(0) in equation.
By changing the brackets i overcome with that error i.e theta[1] & theta[0]
Note:-
1) For the following program Pycharm software is used.