@rfhongyi
2016-11-09T03:41:39.000000Z
字数 7609
阅读 658
Exercise 3.12, we could just easily have chosen to make the plot at times corresponding to a maximum of drive force, or at times out-of-phase with this force, etc.
code
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.001,\total_time=5000,initial_theta=0.2,initial_omega=0):self.g=gself.l=lself.q=qself.F=F_Dself.O=O_Dself.t=[0]self.initial_theta=initial_thetaself.initial_omega=initial_omegaself.dt=time_stepself.time= total_timeself.omega= [initial_omega]self.theta= [initial_theta]self.nsteps=int(total_time//time_step+1)self.tmpo=[0]self.tmpt=[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)self.t.append(self.t[i]+self.dt)for i in range(self.nsteps):if self.t[i]%(2*math.pi/self.O)<0.01:self.tmpo.append(self.omega[i])self.tmpt.append(self.theta[i])def show_results(self):font = {'family': 'serif','color': 'darkred','weight': 'normal','size': 16,}pl.plot(self.tmpt ,self.tmpo,'.',label='$F_{D}$=%.2f'%self.F)pl.title(r'$\omega$ versus $\theta$', fontdict = font)pl.xlabel(r'$\theta$(radians)')pl.ylabel(r'$\omega$(rad/s)')pl.legend((['$F_D$=1.2']))pl.show()a = oscillatory()a.run()a.show_results()




Exercise 3.13, imagine two identical pendulums only with slightly different initial angles and the difference is 0.001 rad.
- code
import pylab as plimport mathclass oscillatory:def __init__(self,g=9.8,l=9.8,q=0.5,F_D=0.5,O_D=2/3,time_step=0.04,total_time=80,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()
Exercise 3.14, imagine two identical pendulums only with slightly different initial q and the difference is 0.001.
- code
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()