@2014301020054
2016-11-06T16:33:30.000000Z
字数 5393
阅读 465
physics python 3.13&3.14
Using Euler-Cromer method to solve the oscillatory motion.
Also using class function,and take more factors into consideratioon.
We can use Euler method to solve some easy questions, but to solve the problem of the oscillatory motion and chaos with the conservation of energy, we cannot solve the problems easily by Euler method. we should use Euler-Cromer method to find the answer.
we know the Euler-Cromer method. Now I will take the Euler-Cromer method into the oscillatory motion and chaos calculation:
Newton’s second law for ideal pendulum with small angle:
Write the second-order equations as two firest-order differential equations:
Finite difference form with Euler-Cromer method:
To make the question more realistic,we can take some factors into consideration.
We do not assume the small-angle approximation, and thus do not expand term in
We include friction of the form
We add to our model a sinusoidal driving force
Putting all of these ingredients together, we have the equation of motion:
We can rewrite the two-order differential equation now as two first-order differential equations and obtian:
Finite difference form with Euler-Cromer method:
If is out of the range ,add or subtract to keep it in this range.
import pylab as plimport mathclass oscillatory:def __init__(self,g=10,l=10,q=0.5,F_D=0.5,O_D=2/3,time_step=0.04,\total_time=50,initial_theta=0.2,initial_omega=0,initial_omega1=0,\initial_theta1=0.201,q1=0.5):self.g=gself.l=lself.q=qself.q1=q1self.F=F_Dself.O=O_Dself.t=[0]self.initial_theta=initial_thetaself.initial_theta1=initial_theta1self.initial_omega=initial_omegaself.dt=time_stepself.time= total_timeself.omega= [initial_omega]self.omega1=[initial_omega1]self.theta= [initial_theta]self.theta1= [initial_theta1]self.nsteps=int(total_time//time_step+1)self.D=[0]self.e=[0]def run(self):for i in range(self.nsteps):tmpo=self.omega[i]+(-1*(self.g/self.l)*math.sin(self.theta[i])-\self.q*self.omega[i]+self.F*math.sin(self.O*self.t[i]))*self.dttmpt=self.theta[i]+tmpo*self.dtwhile(tmpt<(-1*math.pi) or tmpt>math.pi):if tmpt<(-1*math.pi):tmpt+=2*math.piif tmpt>math.pi:tmpt-=2*math.piself.omega.append(tmpo)self.theta.append(tmpt)tmpo1=self.omega1[i]+(-1*(self.g/self.l)*math.sin(self.theta1[i])-\self.q1*self.omega1[i]+self.F*math.sin(self.O*self.t[i]))*self.dttmpt1=self.theta1[i]+tmpo1*self.dtwhile(tmpt1<(-1*math.pi) or tmpt1>math.pi):if tmpt1<(-1*math.pi):tmpt1+=2*math.piif tmpt1>math.pi:tmpt1-=2*math.piself.omega1.append(tmpo1)self.theta1.append(tmpt1)self.t.append(self.t[i]+self.dt)tmpD=abs(tmpt-tmpt1)self.D.append(tmpD)self.e.append(0.001*math.exp(-0.247*self.t[i]))def show_results(self):font = {'family': 'serif','color': 'darkred','weight': 'normal','size': 16,}pl.semilogy(self.t,self.D)pl.semilogy(self.t,self.e,'--')pl.title(r'$\Delta\theta$ versus time', fontdict = font)pl.xlabel('time(s)')pl.ylabel(r'$\Delta\theta$(radians)')pl.legend((['$F_D$=0.5']))pl.show()a = oscillatory()a.run()a.show_results()
Reslut1:
1. F=0.5
2. F=1.2

import pylab as plimport mathclass oscillatory:def __init__(self,g=9.8,l=9.8,q=0.5,F_D=1.2,O_D=2/3,time_step=0.04,\total_time=150,initial_theta=0.2,initial_omega=0,initial_omega1=0,\initial_theta1=0.2,q1=0.501):self.g=gself.l=lself.q=qself.q1=q1self.F=F_Dself.O=O_Dself.t=[0]self.initial_theta=initial_thetaself.initial_theta1=initial_theta1self.initial_omega=initial_omegaself.dt=time_stepself.time= total_timeself.omega= [initial_omega]self.omega1=[initial_omega1]self.theta= [initial_theta]self.theta1= [initial_theta1]self.nsteps=int(total_time//time_step+1)self.D=[0]self.e=[0]def run(self):for i in range(self.nsteps):tmpo=self.omega[i]+(-1*(self.g/self.l)*math.sin(self.theta[i])-\self.q*self.omega[i]+self.F*math.sin(self.O*self.t[i]))*self.dttmpt=self.theta[i]+tmpo*self.dtwhile(tmpt<(-1*math.pi) or tmpt>math.pi):if tmpt<(-1*math.pi):tmpt+=2*math.piif tmpt>math.pi:tmpt-=2*math.piself.omega.append(tmpo)self.theta.append(tmpt)tmpo1=self.omega1[i]+(-1*(self.g/self.l)*math.sin(self.theta1[i])-\self.q1*self.omega1[i]+self.F*math.sin(self.O*self.t[i]))*self.dttmpt1=self.theta1[i]+tmpo1*self.dtwhile(tmpt1<(-1*math.pi) or tmpt1>math.pi):if tmpt1<(-1*math.pi):tmpt1+=2*math.piif tmpt1>math.pi:tmpt1-=2*math.piself.omega1.append(tmpo1)self.theta1.append(tmpt1)self.t.append(self.t[i]+self.dt)tmpD=abs(tmpt-tmpt1)self.D.append(tmpD)self.e.append(0.001*math.exp(0.25*self.t[i]))def show_results(self):font = {'family': 'serif','color': 'darkred','weight': 'normal','size': 16,}pl.semilogy(self.t,self.D)pl.semilogy(self.t,self.e,'--')pl.title(r'$\Delta\theta$ versus time', fontdict = font)pl.xlabel('time(s)')pl.ylabel(r'$\Delta\theta$(radians)')pl.legend((['$F_D$=1.2']))pl.show()a = oscillatory()a.run()a.show_results()
Reslut2:
F=0.5
F=1.2
