[关闭]
@rfhongyi 2016-12-20T02:18:32.000000Z 字数 6289 阅读 946

Exercise_13 : Waves

1111.gif

Problem 6.12 & 6.16

冉峰 弘毅班 2014301020064


Abstract


Background


The Main Body

Ideal case

Gaussian pluck (semirealistic)

Realistic case


Code

  1. import matplotlib.pyplot as plt
  2. from matplotlib import animation
  3. import numpy as np
  4. from scipy.fftpack import rfft, irfft, fftfreq
  5. class Guass_seidel:
  6. def __init__(self,L,dx,T,e):
  7. self.L = L
  8. self.dx = dx
  9. self.r = 0.25
  10. self.c = 300
  11. self.T = T
  12. self.dt = self.dx/(4*self.c)
  13. self.e = e
  14. pass
  15. def initialization(self):
  16. self.string = []
  17. for i in range(int(self.T/self.dt)):
  18. self.string.append([])
  19. for j in range(int(self.L/self.dx)):
  20. self.string[i].append(0)
  21. for l in range(1,int(int(self.L/self.dx) - 1)):
  22. self.string[0][l] = np.e**(-0.1*(l - 30)**2)
  23. self.string[0][1] = 0
  24. self.string[0][-2] = 0
  25. self.string[0][0] = - self.string[0][2]
  26. self.string[0][-1] = - self.string[0][-3]
  27. self.string[1] = self.string[0][:]
  28. #plt.plot(self.string[1])
  29. def calculation(self):
  30. M = int(self.L/self.dx)
  31. for i in range(2,int(self.T/self.dt)):
  32. for j in range(2,M-2):
  33. self.string[i][j] = (2 - 2*self.r**2 - 6*self.e*self.r**2*M**2)*self.string[i - 1][j] - self.string[i - 2][j] + self.r**2*(1 + 4*self.e*M**2)*(self.string[i-1][j+1]+self.string[i-1][j-1]) - self.e*self.r**2*M**2*(self.string[i-1][j+2]+self.string[i-1][j-2])
  34. #self.string[i][j] = 2*(1-self.r**2)*self.string[i - 1][j] - self.string[i - 2][j] + self.r**2*(self.string[i - 1][j + 1] +self.string[i - 1][j - 1])
  35. self.string[i][0] = -self.string[i][2]
  36. self.string[i][-1] = -self.string[i][-3]
  37. #plt.plot(self.string[-1])
  38. return self.string
  39. def fft(self):
  40. t_plot = np.arange(0,self.T,self.dt)
  41. amplitude_record = []
  42. for i in range(int(self.T/self.dt)):
  43. amplitude_record.append(self.string[i][20])
  44. freq = fftfreq(len(amplitude_record), d=self.dt)
  45. freq = np.array(abs(freq))
  46. f_signal = rfft(amplitude_record)
  47. f_signal = np.array(f_signal**2)
  48. #plt.subplot(122)
  49. plt.title('Power spectra')
  50. plt.ylabel('Power (arbitrary units)')
  51. plt.xlabel('Frequency (Hz)')
  52. plt.xlim(2000,8000)
  53. plt.plot(freq,f_signal,label = 'epsilon = '+str(self.e))
  54. return 0
  55. A = Guass_seidel(1,0.01,0.05,0)
  56. A.initialization()
  57. A.calculation()
  58. A.fft()
  59. # ---- animation ---------------------------------
  60. fig = plt.figure(figsize = (8,6))
  61. ax = plt.axes(xlim=(0, 1),ylim = (-1,1))
  62. line, = ax.plot([], [], 'k')
  63. def init():
  64. line.set_data([], [])
  65. return line,
  66. def animate(i):
  67. x_plot = np.arange(0.01,0.99,0.01)
  68. y_plot = []
  69. for j in range(1, int(A.L/A.dx) - 1):
  70. y_plot.append(data_record[i][j])
  71. line.set_data(x_plot,y_plot)
  72. return line,
  73. anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=50, blit=True)
  74. anim.save('chapter6_string_guass_e.gif', fps=20, writer='Feng_Chen')
  75. plt.show()

Result




Conclusion


Thanks For

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注