[关闭]
@wudawufanfan 2016-12-17T17:06:58.000000Z 字数 1337 阅读 536

在此处输入标题

未分类


在此输入正文

  1. from numpy import *
  2. import mpl_toolkits.mplot3d
  3. import matplotlib.pyplot as plt
  4. from matplotlib import cm
  5. from matplotlib.ticker import LinearLocator, FormatStrFormatter
  6. import time
  7. class WAVE(object):
  8. def __init__(self, _n=3000, _steps=1000, _c=300, _x0=0.5):
  9. self.M=int(_steps)
  10. self.c=_c
  11. self.dx=1/float(_steps)
  12. self.dt=self.dx/float(_c)
  13. self.r=self.c*self.dt/self.dx
  14. self.x0=_x0
  15. self.n=int(_n)
  16. def cal(self):
  17. self.y1=[0]*(self.M+1)
  18. for i in range(1,self.M,1):
  19. self.y1[i]=0.03*sin(30.*pi*i*self.dx)
  20. #for i in range(1,int(0.7*self.M),1):
  21. # self.y1[i]=i*self.dx*0.05/0.7
  22. #for i in range(int(0.7*self.M),self.M):
  23. # self.y1[i]=0.05/0.3*(1-i*self.dx)
  24. self.y2=self.y1[:]
  25. self.y3=[0.]*(self.M+1)
  26. for j in range(self.n):
  27. for i in range(1,self.M,1):
  28. self.y3[i]=2.*(1-self.r**2)*self.y2[i]-self.y1[i]+self.r**2*(self.y2[i+1]+self.y2[i-1])
  29. self.y3[0]=0.
  30. self.y3[self.M]=0.
  31. self.y1=self.y2[:]
  32. self.y2=self.y3[:]
  33. if (mod(j,5)==0):
  34. self.plot(j*self.dt*1000)
  35. def plot(self,_t):
  36. fig=plt.figure(figsize=(10,4))
  37. self.x=linspace(0.,1.,self.M+1)
  38. self.ax=plt.axes([0.1,0.2,0.8,0.7])
  39. self.ax.plot(self.x,self.y2,'-',label='time = %.2f (ms)'%_t)
  40. self.ax.set_xlim(-0.0,1.0)
  41. self.ax.set_ylim(-0.06,0.08)
  42. self.ax.set_xlabel("x (m)",fontsize=18)
  43. self.ax.set_ylabel("y (m)",fontsize=18)
  44. self.ax.set_title('Wave on a string: Sine',fontsize=18)
  45. self.ax.legend(loc='best',fontsize=14)
  46. plt.savefig(r'd:\save'+'%.2f'%_t+r'.jpg')
  47. cmp=WAVE()
  48. cmp.cal()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注