[关闭]
@Xc-liu 2016-04-26T16:40:52.000000Z 字数 7122 阅读 940

第九次作业_计算程序

程序1

  1. # basic packages needed
  2. # matplotlib - for plot 2D lines
  3. # numpy - for math
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import math
  7. # class CROMER
  8. class CROMER_1(object):
  9. def __init__(self, _theta0=0.2, _omg0=0.0, _t0=0.0, _l=9.8,_g=9.8, _dt=0.04, _time=60.0,_q=0.5,_OMG_D=2.0/3):
  10. self.theta, self.omg, self.t = [_theta0], [_omg0], [_t0]
  11. self.l, self.g, self.dt, self.time, self.n= _l, _g, _dt, _time, int(_time/_dt)
  12. self.OMG_D=_OMG_D
  13. self.F_D=[0.0,0.5,1.2]
  14. self.I=range(self.n)
  15. self.q=[0,0.5,1.2]
  16. def calculate(self):
  17. for i in self.I:
  18. self.omg.append(self.omg[i]+(-self.g/self.l*math.sin(self.theta[i])-self.q[0]*self.omg[i]+self.F_D[1]*math.sin(self.OMG_D*self.t[i]))*self.dt)
  19. self.theta.append(self.theta[i]+self.omg[i+1]*self.dt)
  20. if self.theta[i+1]<-math.pi:
  21. self.theta[i+1]=self.theta[i+1]+2*math.pi
  22. if self.theta[i+1]>math.pi:
  23. self.theta[i+1]=self.theta[i+1]-2*math.pi
  24. self.t.append(self.t[i]+self.dt)
  25. def plot_theta(self,_ax):
  26. _ax.plot(self.t, self.theta, '--',label='q=0')
  27. def plot_w(self,_ax):
  28. _ax.plot(self.t,self.omg,'--',label='q=0')
  29. class CROMER_2(CROMER_1):
  30. def calculate(self):
  31. for i in self.I:
  32. self.omg.append(self.omg[i]+(-self.g/self.l*math.sin(self.theta[i])-self.q[1]*self.omg[i]+self.F_D[1]*math.sin(self.OMG_D*self.t[i]))*self.dt)
  33. self.theta.append(self.theta[i]+self.omg[i+1]*self.dt)
  34. if self.theta[i+1]<-math.pi:
  35. self.theta[i+1]=self.theta[i+1]+2*math.pi
  36. if self.theta[i+1]>math.pi:
  37. self.theta[i+1]=self.theta[i+1]-2*math.pi
  38. self.t.append(self.t[i]+self.dt)
  39. def plot_theta(self,_ax):
  40. _ax.plot(self.t, self.theta, '--',label='q=0.5')
  41. def plot_w(self,_ax):
  42. _ax.plot(self.t,self.omg,'--',label='q=0.5')
  43. class CROMER_3(CROMER_1):
  44. def calculate(self):
  45. for i in self.I:
  46. self.omg.append(self.omg[i]+(-self.g/self.l*math.sin(self.theta[i])-self.q[2]*self.omg[i]+self.F_D[1]*math.sin(self.OMG_D*self.t[i]))*self.dt)
  47. self.theta.append(self.theta[i]+self.omg[i+1]*self.dt)
  48. if self.theta[i+1]<-math.pi:
  49. self.theta[i+1]=self.theta[i+1]+2*math.pi
  50. if self.theta[i+1]>math.pi:
  51. self.theta[i+1]=self.theta[i+1]-2*math.pi
  52. self.t.append(self.t[i]+self.dt)
  53. def plot_theta(self,_ax):
  54. _ax.plot(self.t, self.theta, '--',label='q=1.2')
  55. def plot_w(self,_ax):
  56. _ax.plot(self.t,self.omg,'--',label='q=1.2')
  57. # plot :
  58. # EULER CROMER METHOD are used
  59. fig= plt.figure(figsize=(14,7))
  60. ax1 = plt.subplot(121)
  61. ax2 = plt.subplot(122)
  62. comp= CROMER_1()
  63. comp.calculate()
  64. comp.plot_theta(ax1)
  65. comp.plot_w(ax2)
  66. comp=CROMER_2()
  67. comp.calculate()
  68. comp.plot_theta(ax1)
  69. comp.plot_w(ax2)
  70. comp=CROMER_3()
  71. comp.calculate()
  72. comp.plot_theta(ax1)
  73. comp.plot_w(ax2)
  74. ax1.set_title('Nonlinear Pendulum - Angle',fontsize=14)
  75. ax2.set_title('Nonlinear Pendulum - W',fontsize=14)
  76. ax1.set_xlabel('time(s)',fontsize=14)
  77. ax1.set_ylabel('Angel (rad)',fontsize=14)
  78. ax2.set_xlabel('time(s)',fontsize=14)
  79. ax2.set_ylabel('w(rad/s)',fontsize=14)
  80. ax1.legend(fontsize=12,loc='best')
  81. ax2.legend(fontsize=12,loc='best')
  82. plt.show(fig)

程序2

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. import math
  4. theta0=0.2
  5. theta1=0.2001
  6. omg0=0.0
  7. t0=0.0
  8. l=9.8
  9. g=9.8
  10. dt=0.04
  11. time=400.0
  12. OMG_D=2.0/3
  13. theta=[theta0]
  14. theta1=[theta1]
  15. omg1=[omg0]
  16. omg=[omg0]
  17. t1=[t0]
  18. t=[t0]
  19. t2=[t0]
  20. n=int(time/dt)
  21. OMG_D=OMG_D
  22. F_D=[0.0,1.5,1.2]
  23. I=range(n)
  24. q=[0,1.5,0.5]
  25. delta=[]
  26. for i in I:
  27. omg.append(omg[i]+(-g/l*math.sin(theta[i])-q[2]*omg[i]+F_D[2]*math.sin(OMG_D*t[i]))*dt)
  28. theta.append(theta[i]+omg[i+1]*dt)
  29. if theta[i+1]<-math.pi:
  30. theta[i+1]=theta[i+1]+2*math.pi
  31. if theta[i+1]>math.pi:
  32. theta[i+1]=theta[i+1]-2*math.pi
  33. t.append(t[i]+dt)
  34. for i in I:
  35. omg1.append(omg1[i]+(-g/l*math.sin(theta1[i])-q[2]*omg1[i]+F_D[2]*math.sin(OMG_D*t1[i]))*dt)
  36. theta1.append(theta1[i]+omg1[i+1]*dt)
  37. if theta1[i+1]<-math.pi:
  38. theta1[i+1]=theta1[i+1]+2*math.pi
  39. if theta1[i+1]>math.pi:
  40. theta1[i+1]=theta1[i+1]-2*math.pi
  41. t1.append(t1[i]+dt)
  42. for i in I:
  43. l=theta1[i]-theta[i]
  44. #delta.append(l)
  45. if l>=0:
  46. l=l
  47. delta.append(l)
  48. if l<0:
  49. l=-l
  50. delta.append(l)
  51. t2.append(t2[i]+dt)
  52. delta=delta[1:int(10000.00000)]
  53. t1=t1[1:int(10000.00000)]
  54. # EULER CROMER METHOD are used
  55. plt.plot(t1, delta, '--',label='F_D=0.5')
  56. plt.title(u'Nonlinear Pendulum - delta Angle',fontsize=14)
  57. plt.xlabel(u'time(s)',fontsize=14)
  58. plt.ylabel(u'delta angel (rad)',fontsize=14)
  59. plt.legend(fontsize=12,loc='best')
  60. plt.show()

程序3

  1. # basic packages needed
  2. # matplotlib - for plot 2D lines
  3. # numpy - for math
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import math
  7. import pylab as pl
  8. # class CROMER
  9. class CROMER_1(object):
  10. def __init__(self, _theta0=0.2, _omg0=0.0, _t0=0.0, _l=9.8,_g=9.8, _dt=6.0*math.pi/1000, _time=6000.0,_q=0.5,_OMG_D=2.0/3):
  11. self.theta, self.omg, self.t = [_theta0], [_omg0], [_t0]
  12. self.l, self.g, self.dt, self.time, self.n= _l, _g, _dt, _time, int(_time/_dt)
  13. self.OMG_D=_OMG_D
  14. self.F_D=[0.0,0.5,1.2]
  15. self.I=range(self.n)
  16. self.q=[0.5,0.5,0.5]
  17. self.theta1=[]
  18. self.omg1=[]
  19. self.p=0
  20. def calculate(self):
  21. for i in self.I:
  22. self.omg.append(self.omg[i]+(-self.g/self.l*math.sin(self.theta[i])-self.q[0]*self.omg[i]+self.F_D[1]*math.sin(self.OMG_D*self.t[i]))*self.dt)
  23. self.theta.append(self.theta[i]+self.omg[i+1]*self.dt)
  24. if self.theta[i+1]<-math.pi:
  25. self.theta[i+1]=self.theta[i+1]+2*math.pi
  26. if self.theta[i+1]>math.pi:
  27. self.theta[i+1]=self.theta[i+1]-2*math.pi
  28. self.t.append(self.t[i]+self.dt)
  29. self.p=i%1000
  30. if self.p==0:
  31. self.theta1.append(self.theta[-1])
  32. self.omg1.append(self.omg[-1])
  33. self.theta1=self.theta1[100:]
  34. self.omg1=self.omg1[100:]
  35. def plot_theta(self,_ax):
  36. _ax.axes.scatter(self.theta1, self.omg1, marker='x',color='m',label='F_D=0.5')
  37. print len(self.theta1)
  38. # def plot_w(self,_ax):
  39. # _ax.plot(self.t,self.omg,'--',label='q=0')
  40. class CROMER_2(CROMER_1):
  41. def calculate(self):
  42. for i in self.I:
  43. self.omg.append(self.omg[i]+(-self.g/self.l*math.sin(self.theta[i])-self.q[1]*self.omg[i]+self.F_D[2]*math.sin(self.OMG_D*self.t[i]))*self.dt)
  44. self.theta.append(self.theta[i]+self.omg[i+1]*self.dt)
  45. if self.theta[i+1]<-math.pi:
  46. self.theta[i+1]=self.theta[i+1]+2*math.pi
  47. if self.theta[i+1]>math.pi:
  48. self.theta[i+1]=self.theta[i+1]-2*math.pi
  49. self.t.append(self.t[i]+self.dt)
  50. if i%1000==0:
  51. self.theta1.append(self.theta[-1])
  52. self.omg1.append(self.omg[-1])
  53. self.theta1=self.theta1[100:]
  54. self.omg1=self.omg1[100:]
  55. # def plot_theta(self,_ax):
  56. # _ax.plot(self.t, self.theta, '---',label='q=0.1')
  57. def plot_w(self,_ax):
  58. _ax.axes.scatter(self.theta1,self.omg1,marker='o',color='b',label='F_D=1.2')
  59. class CROMER_3(CROMER_1):
  60. def calculate(self):
  61. for i in self.I:
  62. self.omg.append(self.omg[i]+(-self.g/self.l*math.sin(self.theta[i])-self.q[2]*self.omg[i]+self.F_D[1]*math.sin(self.OMG_D*self.t[i]))*self.dt)
  63. self.theta.append(self.theta[i]+self.omg[i+1]*self.dt)
  64. if self.theta[i+1]<-math.pi:
  65. self.theta[i+1]=self.theta[i+1]+2*math.pi
  66. if self.theta[i+1]>math.pi:
  67. self.theta[i+1]=self.theta[i+1]-2*math.pi
  68. self.t.append(self.t[i]+self.dt)
  69. def plot_theta(self,_ax):
  70. _ax.plot(self.t, self.theta, '--',label='q=0.5')
  71. def plot_w(self,_ax):
  72. _ax.plot(self.t,self.omg,'--',label='q=0.5')
  73. # plot :
  74. # EULER CROMER METHOD are used
  75. fig= plt.figure(figsize=(14,7))
  76. ax1 = plt.subplot(121)
  77. ax2 = plt.subplot(122)
  78. comp= CROMER_1()
  79. comp.calculate()
  80. comp.plot_theta(ax1)
  81. #comp.plot_w(ax2)
  82. comp=CROMER_2()
  83. comp.calculate()
  84. #comp.plot_theta(ax1)
  85. comp.plot_w(ax2)
  86. #comp=CROMER_3()
  87. #comp.calculate()
  88. #comp.plot_theta(ax1)
  89. #comp.plot_w(ax2)
  90. ax1.set_title('Poincare section - $2n\pi$',fontsize=14)
  91. ax2.set_title('Poincare section - $2n\pi$',fontsize=14)
  92. ax1.set_xlabel('theta(rad)',fontsize=14)
  93. ax1.set_ylabel('Angel volocity (rad/s)',fontsize=14)
  94. ax2.set_xlabel('theta(rad)',fontsize=14)
  95. ax2.set_ylabel('Angel volocity(rad/s)',fontsize=14)
  96. ax1.legend(fontsize=12,loc='best')
  97. ax2.legend(fontsize=12,loc='best')
  98. plt.show(fig)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注