[关闭]
@SuperMan 2016-05-31T08:16:13.000000Z 字数 1744 阅读 883

计算物理第十三次作业

作者:夏海峰 学号:2013301020094

作业5.3题

  • Use the symmetry of the capacitor problem to write a program that obtains the result by calculating the potential in only one quadrant Of the x-y plane.
    Photo

  • 解决方法:

  • Jacobi方法
  • 具体解决方法:
    (1)确定边界条件
    (2)利用Jacobi方法:

Photo
photo
由图可知在计算了7714次后达到精确度要求!
程序
当计算了8351次后,前后两次的差值已经小于

程序如下:

  1. from mpl_toolkits.mplot3d import Axes3D
  2. from matplotlib import cm
  3. from matplotlib.ticker import LinearLocator, FormatStrFormatter
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import random
  7. A_a=range(61)
  8. A_a.remove(15)
  9. A_a.remove(45)
  10. class Fields:
  11. def __init__(self):
  12. self.v=[]
  13. self.old_v=[]
  14. for i in range(61):
  15. self.v.append([])
  16. self.old_v.append([])
  17. for i in range(61):
  18. for j in range(61):
  19. self.v[i].append(random.uniform(-1,1))
  20. #print self.v
  21. def update(self):
  22. for i in range(61):
  23. self.v[i][0]=0
  24. self.v[i][60]=0
  25. for i in range(15,45):
  26. self.v[i][15]=-1
  27. self.v[i][45]=1
  28. for j in range(61):
  29. self.v[0][j]=0
  30. self.v[60][j]=0
  31. for i in range(61):
  32. for j in range(61):
  33. self.old_v[i].append(self.v[i][j])
  34. self.Delta_v=0.
  35. for i in range(1,60):
  36. for j in range(1,60):
  37. if ((j==15 and (i in range(15,45))) or (j==45 and (i in range(15,45)))):
  38. self.v[i][j]=self.v[i][j]
  39. else:
  40. self.v[i][j]=(self.v[i-1][j]+self.v[i+1][j]+self.v[i][j-1]+self.v[i][j+1])/4.
  41. self.Delta_v+=abs(self.old_v[i][j]-self.v[i][j])
  42. print self.Delta_v
  43. def fire(self):
  44. for i in range(1000):
  45. self.update()
  46. i+=1
  47. print i
  48. return self.v
  49. AA=Fields()
  50. Super=AA.fire()
  51. fig = plt.figure()
  52. ax = fig.gca(projection='3d')
  53. x=range(61)
  54. y=range(61)
  55. x, y = np.meshgrid(x, y)
  56. surf = ax.plot_surface(x, y, Super, rstride=1, cstride=1, cmap=cm.coolwarm,
  57. linewidth=0, antialiased=False)
  58. ax.set_zlim(-1.01, 1.01)
  59. ax.zaxis.set_major_locator(LinearLocator(10))
  60. ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
  61. plt.xlabel("X")
  62. plt.ylabel("Y")
  63. fig.colorbar(surf, shrink=0.5, aspect=5)
  64. plt.show()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注