[关闭]
@Xc-liu 2016-04-19T23:40:27.000000Z 字数 9740 阅读 704

homework_8 附录

程序1

  1. #basic packges needed
  2. #matplotlib - for plot 2D lines
  3. #numpy - for math
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. # class EULER
  7. # use EULER METHOD to solve the pendulum
  8. # para. & var. :
  9. # intial value
  10. class EULER(object):
  11. #def __init__(self, _theta0=10., _omg0=0, _t0=0., _l=9.8/(4*(np.pi)**2),_g=9.8, _dt=0.01, _time=4.):
  12. # self.theta, self.omg, self.t = [_theta0], [_omg0], [_t0]
  13. # self.l, self.g, self.dt, self.time, self.n= _l, _g, _dt, _time, int(_time/_dt)
  14. # self.E = [0.5*((self.l*_omg0)**2+self.g*se
  15. def __init__(self,_t0=0.,_dt=0.01,_time=15.,_x0=1.,_v0=0):
  16. self.x,self.t,self.v=[_x0],[_t0],[_v0]
  17. self.dt=_dt
  18. self.n=int(_time/_dt)
  19. def calculate(self):
  20. for i in range(self.n):
  21. self.v.append(self.v[-1]-self.x[-1]*self.dt)
  22. self.x.append(self.x[-1]+self.v[-2]*self.dt)
  23. self.t.append(self.t[-1]+self.dt)
  24. #self.E.append(0.5*((self.l*self.omg[-1])**2+self.g*self.l*(self.theta[-1])**2))
  25. def plot_x(self,_ax):
  26. _ax.plot(self.t, self.x, '--',label='Euler Method')
  27. #def plot_E(self,_ax):
  28. #_ax.plot(self.t,self.E,'--',label='Euler Method')
  29. # class CROMER
  30. # use EULER-CROMER METHOD to solve the pendulum
  31. # para. & var. :
  32. #initial value
  33. class CROMER(EULER):
  34. def calculate(self):
  35. for i in range(self.n):
  36. self.v.append(self.v[-1]-self.x[-1]*self.dt)
  37. self.x.append(self.x[-1]+self.v[-1]*self.dt)
  38. self.t.append(self.t[-1]+self.dt)
  39. #self.E.append(0.5*((self.l*self.omg[-1])**2+self.g*self.l*(self.theta[-1])**2))
  40. def plot_x(self,_ax):
  41. _ax.plot(self.t, self.x, '--',label='Euler-Cromer Method')
  42. #def plot_E(self,_ax):
  43. #_ax.plot(self.t,self.E,'--',label='Euler-Cromer Method')
  44. #class RUNGE_RUNGE_22
  45. # use 2nd Runge-Kutta Method to solve the pendulum
  46. # para. & var. :
  47. #initial value
  48. class RUNGE_RUNGE_22(EULER):
  49. def calculate(self):
  50. for i in range(self.n):
  51. self.t2=self.t[-1]+self.dt/2
  52. self.v2=self.v[-1]-self.x[-1]*self.dt/2
  53. self.x2=self.x[-1]+self.v2*self.dt
  54. self.t.append(self.t[-1]+self.dt)
  55. self.v.append(self.v[-1]-self.x2*self.dt)
  56. self.x.append(self.x[-1]+self.v[-1]*self.dt)
  57. def plot_x(self,_ax):
  58. _ax.plot(self.t, self.x, '--',label='2-Runge-Kutta Method')
  59. #def plot_E(self,_ax):
  60. #_ax.plot(self.t,self.E,'--',label='2nd-order Runge-Kutta')
  61. # class RUNGE_RUNGE_44
  62. # use 4th Runge-Kutta Method to solve the pendulum
  63. # para. & var. :
  64. #initial value
  65. class RUNGE_RUNGE_44(EULER):
  66. def calculate(self):
  67. for i in range(self.n):
  68. self.t1,self.t2,self.t3,self.t4=self.t[-1],self.t[-1]+self.dt/2.,self.t[-1]+self.dt/2.,self.t[-1]+self.dt
  69. self.v1=self.v[-1]
  70. self.x1=self.x[-1]+self.v1*self.dt
  71. self.v2=self.v1-self.x1*self.dt/2
  72. self.x2=self.x[-1]+self.v2*self.dt
  73. self.v3=self.v1-self.x2*self.dt/2
  74. self.x3=self.x[-1]+self.v3*self.dt
  75. self.v4=self.v1-self.x3*self.dt/2
  76. self.x4=self.x[-1]+self.v4*self.dt
  77. self.t.append(self.t[-1]+self.dt)
  78. self.v.append(self.v[-1]+1./6.*(-self.x1-2.*self.x2-2.*self.x3-self.x4)*self.dt)
  79. self.x.append(self.x[-1]+self.v[-1]*self.dt)
  80. def plot_x(self,_ax):
  81. _ax.plot(self.t, self.x, '--',label='4-Runge-Kutta Method')
  82. # self.E.append(0.5*((self.l*self.omg[-1])**2+self.g*self.l*(self.theta[-1])**2))
  83. # def plot_E(self,_ax):
  84. # _ax.plot(self.t,self.E,'--',label='4th-order Runge-Kutta')
  85. # plot :
  86. # ax1 - time dependence of angel
  87. # ax2 - time dependence of energy
  88. # both EULER & EULER CROMER METHOD &RUNGE_KUTTA are used
  89. fig= plt.figure(figsize=(14,7))
  90. ax1 = plt.subplot(121)
  91. ax2 = plt.subplot(122)
  92. comp= EULER()
  93. comp.calculate()
  94. comp.plot_x(ax1)
  95. comp= CROMER()
  96. comp.calculate()
  97. comp.plot_x(ax1)
  98. comp=RUNGE_RUNGE_22()
  99. comp.calculate()
  100. comp.plot_x(ax2)
  101. comp=RUNGE_RUNGE_44()
  102. comp.calculate()
  103. comp.plot_x(ax2)
  104. t=np.linspace(0,15,30)
  105. x=np.cos(t)
  106. ax1.plot(t,x,'--',label='Analysis')
  107. ax2.plot(t,x,'--',label='Analysis')
  108. #ax2.text(0.5,350,'Period: '+r'$ \tau $'+' = 1s',fontsize=14)
  109. ax1.set_title('the different result between different method ',fontsize=14)
  110. ax2.set_title('the different result between different method ',fontsize=14)
  111. ax1.set_xlabel('time/t',fontsize=14)
  112. ax1.set_ylabel('displacement',fontsize=14)
  113. ax2.set_xlabel('time/t',fontsize=14)
  114. ax2.set_ylabel('displacement',fontsize=14)
  115. ax1.legend(fontsize=12,loc='best')
  116. ax2.legend(fontsize=12,loc='best')
  117. plt.show(fig)

程序2

  1. #basic packges needed
  2. #matplotlib - for plot 2D lines
  3. #numpy - for math
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. # class EULER
  7. # use EULER METHOD to solve the pendulum
  8. # para. & var. :
  9. # intial value
  10. class EULER(object):
  11. #def __init__(self, _theta0=10., _omg0=0, _t0=0., _l=9.8/(4*(np.pi)**2),_g=9.8, _dt=0.01, _time=4.):
  12. # self.theta, self.omg, self.t = [_theta0], [_omg0], [_t0]
  13. # self.l, self.g, self.dt, self.time, self.n= _l, _g, _dt, _time, int(_time/_dt)
  14. # self.E = [0.5*((self.l*_omg0)**2+self.g*se
  15. def __init__(self,_t0=0.,_dt=0.01,_time=15.,_x0=1.,_v0=0):
  16. self.x,self.t,self.v=[_x0],[_t0],[_v0]
  17. self.dt=_dt
  18. self.n=int(_time/_dt)
  19. def calculate(self):
  20. for in range(self.n): self.t1,self.t2,self.t3,self.t4=self.t[-1],self.t[-1]+self.dt/2.,self.t[-1]+self.dt/2.,self.t[-1]+self.dt
  21. self.v1=self.v[-1]
  22. self.x1=self.x[-1]+self.v1*self.dt
  23. self.v2=self.v1-self.x1**1*self.dt/2
  24. self.x2=self.x[-1]+self.v2*self.dt
  25. self.v3=self.v1-self.x2**1*self.dt/2
  26. self.x3=self.x[-1]+self.v3*self.dt
  27. self.v4=self.v1-self.x3**1*self.dt/2
  28. self.x4=self.x[-1]+self.v4*self.dt
  29. self.t.append(self.t[-1]+self.dt)
  30. self.v.append(self.v[-1]+1./6.*(-self.x1**1-2.*self.x2**1-2.*self.x3**1-self.x4**1)*self.dt)
  31. self.x.append(self.x[-1]+self.v[-1]*self.dt)
  32. def plot_x(self,_ax):
  33. _ax.plot(self.t, self.x, '--',label='a=1')
  34. #def plot_E(self,_ax):
  35. #_ax.plot(self.t,self.E,'--',label='Euler Method')
  36. # class CROMER
  37. # use EULER-CROMER METHOD to solve the pendulum
  38. # para. & var. :
  39. #initial value
  40. class CROMER(EULER):
  41. def calculate(self):
  42. for i in range(self.n):
  43. self.t1,self.t2,self.t3,self.t4=self.t[-1],self.t[-1]+self.dt/2.,self.t[-1]+self.dt/2.,self.t[-1]+self.dt
  44. self.v1=self.v[-1]
  45. self.x1=self.x[-1]+self.v1*self.dt
  46. self.v2=self.v1-self.x1**3*self.dt/2
  47. self.x2=self.x[-1]+self.v2*self.dt
  48. self.v3=self.v1-self.x2**3*self.dt/2
  49. self.x3=self.x[-1]+self.v3*self.dt
  50. self.v4=self.v1-self.x3**3*self.dt/2
  51. self.x4=self.x[-1]+self.v4*self.dt
  52. self.t.append(self.t[-1]+self.dt)
  53. self.v.append(self.v[-1]+1./6.*(-self.x1**3-2.*self.x2**3-2.*self.x3**3-self.x4**3)*self.dt)
  54. self.x.append(self.x[-1]+self.v[-1]*self.dt)
  55. def plot_x(self,_ax):
  56. _ax.plot(self.t, self.x, '--',label='a=3')
  57. #def plot_E(self,_ax):
  58. #_ax.plot(self.t,self.E,'--',label='Euler-Cromer Method')
  59. #class RUNGE_RUNGE_22
  60. # use 2nd Runge-Kutta Method to solve the pendulum
  61. # para. & var. :
  62. #initial value
  63. class RUNGE_RUNGE_22(EULER):
  64. def calculate(self):
  65. for i in range(self.n):
  66. self.t1,self.t2,self.t3,self.t4=self.t[-1],self.t[-1]+self.dt/2.,self.t[-1]+self.dt/2.,self.t[-1]+self.dt
  67. self.v1=self.v[-1]
  68. self.x1=self.x[-1]+self.v1*self.dt
  69. self.v2=self.v1-self.x1**2*self.dt/2
  70. self.x2=self.x[-1]+self.v2*self.dt
  71. self.v3=self.v1-self.x2**2*self.dt/2
  72. self.x3=self.x[-1]+self.v3*self.dt
  73. self.v4=self.v1-self.x3**2*self.dt/2
  74. self.x4=self.x[-1]+self.v4*self.dt
  75. self.t.append(self.t[-1]+self.dt)
  76. self.v.append(self.v[-1]+1./6.*(-self.x1**2-2.*self.x2**2-2.*self.x3**2-self.x4**2)*self.dt)
  77. self.x.append(self.x[-1]+self.v[-1]*self.dt)
  78. def plot_x(self,_ax):
  79. _ax.plot(self.t, self.x, '--',label='a=2')
  80. #def plot_E(self,_ax):
  81. #_ax.plot(self.t,self.E,'--',label='2nd-order Runge-Kutta')
  82. # class RUNGE_RUNGE_44
  83. # use 4th Runge-Kutta Method to solve the pendulum
  84. # para. & var. :
  85. #initial value
  86. class RUNGE_RUNGE_44(EULER):
  87. def calculate(self):
  88. for i in range(self.n):
  89. self.t1,self.t2,self.t3,self.t4=self.t[-1],self.t[-1]+self.dt/2.,self.t[-1]+self.dt/2.,self.t[-1]+self.dt
  90. self.v1=self.v[-1]
  91. self.x1=self.x[-1]+self.v1*self.dt
  92. self.v2=self.v1-self.x1**4*self.dt/2
  93. self.x2=self.x[-1]+self.v2*self.dt
  94. self.v3=self.v1-self.x2**4*self.dt/2
  95. self.x3=self.x[-1]+self.v3*self.dt
  96. self.v4=self.v1-self.x3**4*self.dt/2
  97. self.x4=self.x[-1]+self.v4*self.dt
  98. self.t.append(self.t[-1]+self.dt)
  99. self.v.append(self.v[-1]+1./6.*(-self.x1**4-2.*self.x2**4-2.*self.x3**4-self.x4**4)*self.dt)
  100. self.x.append(self.x[-1]+self.v[-1]*self.dt)
  101. def plot_x(self,_ax):
  102. _ax.plot(self.t, self.x, '--',label='a=4')
  103. # self.E.append(0.5*((self.l*self.omg[-1])**2+self.g*self.l*(self.theta[-1])**2))
  104. # def plot_E(self,_ax):
  105. # _ax.plot(self.t,self.E,'--',label='4th-order Runge-Kutta')
  106. # plot :
  107. # ax1 - time dependence of angel
  108. # ax2 - time dependence of energy
  109. # both EULER & EULER CROMER METHOD &RUNGE_KUTTA are used
  110. fig= plt.figure(figsize=(14,7))
  111. ax1 = plt.subplot(121)
  112. ax2 = plt.subplot(122)
  113. comp= EULER()
  114. comp.calculate()
  115. comp.plot_x(ax1)
  116. comp= CROMER()
  117. comp.calculate()
  118. comp.plot_x(ax1)
  119. comp=RUNGE_RUNGE_22()
  120. comp.calculate()
  121. comp.plot_x(ax1)
  122. comp=RUNGE_RUNGE_44()
  123. comp.calculate()
  124. comp.plot_x(ax1)
  125. ax1.set_title('the different result for different a ',fontsize=14)
  126. ax1.set_xlabel('time/t',fontsize=14)
  127. ax1.set_ylabel('displacement',fontsize=14)
  128. ax1.legend(fontsize=12,loc='best')
  129. plt.show(fig)

程序3

  1. import matplotlib.pyplot as plt
  2. import math
  3. k=15
  4. dx=0.001
  5. x0=0.
  6. x=[]
  7. s=[]
  8. x.append(x0)
  9. s.append(0)
  10. x_1=1
  11. a1=range(k)
  12. f1=[]
  13. for j in range(k):
  14. for i in range(int((x_1-x0)/dx)):
  15. x.append((4*(a1[j]+1)**0.5*(x_1**(a1[j]+1)-(x0+i*dx)**(a1[j]+1))**-0.5*dx))
  16. s.append(s[-1]+x[-1])
  17. f1.append(s[-1])
  18. # print s[-1]
  19. del x[:]
  20. del s[:]
  21. x.append(x0)
  22. s.append(0)
  23. x_1=2
  24. a2=range(k)
  25. f2=[]
  26. for j in range(k):
  27. for i in range(int((x_1-x0)/dx)):
  28. x.append((4*(a2[j]+1)**0.5*(x_1**(a2[j]+1)-(x0+i*dx)**(a2[j]+1))**-0.5*dx))
  29. s.append(s[-1]+x[-1])
  30. f2.append(s[-1])
  31. # print s[-1]
  32. del x[:]
  33. del s[:]
  34. x.append(x0)
  35. s.append(0)
  36. x_1=3
  37. a3=range(k)
  38. f3=[]
  39. for j in range(k):
  40. for i in range(int((x_1-x0)/dx)):
  41. x.append((4*(a3[j]+1)**0.5*(x_1**(a3[j]+1)-(x0+i*dx)**(a3[j]+1))**-0.5*dx))
  42. s.append(s[-1]+x[-1])
  43. f3.append(s[-1])
  44. # print s[-1]
  45. del x[:]
  46. del s[:]
  47. x.append(x0)
  48. s.append(0)
  49. x_1=4
  50. a4=range(k)
  51. f4=[]
  52. for j in range(k):
  53. for i in range(int((x_1-x0)/dx)):
  54. x.append((4*(a4[j]+1)**0.5*(x_1**(a4[j]+1)-(x0+i*dx)**(a4[j]+1))**-0.5*dx))
  55. s.append(s[-1]+x[-1])
  56. f4.append(s[-1])
  57. # print s[-1]
  58. del x[:]
  59. del s[:]
  60. x.append(x0)
  61. s.append(0)
  62. x_1=5
  63. a5=range(k)
  64. f5=[]
  65. for j in range(k):
  66. for i in range(int((x_1-x0)/dx)):
  67. x.append((4*(a5[j]+1)**0.5*(x_1**(a5[j]+1)-(x0+i*dx)**(a5[j]+1))**-0.5*dx))
  68. s.append(s[-1]+x[-1])
  69. f5.append(s[-1])
  70. # print s[-1]
  71. #The following codes plot the data.
  72. plt.figure(1)
  73. line_1,=plt.plot(a1,f1)
  74. line_2,=plt.plot(a2,f2)
  75. line_3,=plt.plot(a3,f3)
  76. line_4,=plt.plot(a4,f4)
  77. line_5,=plt.plot(a5,f5)
  78. plt.ylabel('period')
  79. plt.xlabel('index.a')
  80. plt.title('the different period for different a')
  81. plt.legend([line_1,line_2,line_3,line_4,line_5],['amplitude=1','amplitude=2','amplitude=3','amplitude=4','amplitude=5',],fontsize='x-small')
  82. plt.show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注