[关闭]
@OrionPaxxx 2016-12-12T04:51:18.000000Z 字数 4728 阅读 1293

computationalphysics

electric potentials and fields:Laplace's equation

abstract

The partial differential equation is of physicists' great interest for its widely applicaiton in decribing physical systems. Jacobi method and simultaneous over relaxation method are two algrithm that keep a balance of the simplicity for comprehension and accuracy of computation. Here in this passage,we are going to discuss the relation and difference with differnet methods in tackling same problem.
key words:relaxation algorithm,jacobi method,Gauss-seidel method,SOR,Laplace'squation

background

main body

  1. import math
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4. import numpy as np
  5. from matplotlib import cm
  6. from matplotlib.ticker import LinearLocator, FormatStrFormatter
  7. class ef:
  8. #初始化创建二维n*n全为0的矩阵,在后面的函数中通过修改矩阵中元素的值来构建不同的电场
  9. def __init__(self,alpha=1,n=20):
  10. self.n=n
  11. self.field=[[0]*(self.n) for i in range(self.n)]
  12. #教材figure5.2所示电场
  13. def field1(self):
  14. self.field[0]=np.linspace(-1,1,self.n)
  15. self.field[-1]=np.linspace(-1,1,self.n)
  16. for k in range(1,self.n-1):
  17. self.field[k][0]=-1
  18. self.field[k][-1]=1
  19. self.kk=1
  20. while(1):
  21. self.kk+=1
  22. for i in range(1,self.n-1):
  23. for j in range(1,self.n-1):
  24. self.field[i][j]=(self.field[i+1][j]+self.field[i-1][j]+self.field[i][j+1]+self.field[i][j-1])/4
  25. if self.kk>1000:
  26. break
  27. #教材figure5.4所示电场
  28. def field2(self):
  29. for i in range(int(self.n/4),int(self.n*3/4)):
  30. for j in range(int(self.n/4),int(self.n*3/4)):
  31. self.field[i][j]=1
  32. self.kk=1
  33. while(1):
  34. self.kk+=1
  35. for i in range(1,self.n-1):
  36. for j in range(1,self.n-1):
  37. if int(self.n/4)<=i<=int(self.n*3/4) and int(self.n/4)<=j<=int(self.n*3/4):
  38. self.field[i][j]=1
  39. else:
  40. self.field[i][j]=(self.field[i+1][j]+self.field[i-1][j]+self.field[i][j+1]+self.field[i][j-1])/4
  41. if self.kk>1000:
  42. break
  43. #教材figure5.6所示电场
  44. def field3(self):
  45. for i in range(1,self.n-1):
  46. for j in range(1,self.n-1):
  47. if j==int(self.n/4) and i>=self.n/4 and i<=self.n*3/4:
  48. self.field[i][j]=1
  49. elif j==int(self.n*3/4) and i>=self.n/4 and i<=self.n*3/4:
  50. self.field[i][j]=-1
  51. self.kk=1
  52. while(1):
  53. self.kk+=1
  54. for i in range(1,self.n-1):
  55. for j in range(1,self.n-1):
  56. if j==int(self.n/4) and i>=self.n/4 and i<=self.n*3/4:
  57. self.field[i][j]=1
  58. elif j==int(self.n*3/4) and i>=self.n/4 and i<=self.n*3/4:
  59. self.field[i][j]=-1
  60. else:
  61. self.field[i][j]=(self.field[i+1][j]+self.field[i-1][j]+self.field[i][j+1]+self.field[i][j-1])/4
  62. if self.kk>5000:
  63. break
  64. def show_field(self):
  65. fig = plt.figure()
  66. ax = Axes3D(fig)
  67. X=np.linspace(-1,1,self.n)
  68. Y=np.linspace(-1,1,self.n)
  69. X, Y = np.meshgrid(X, Y)
  70. Z=self.field
  71. ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot)
  72. ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.cm.hot)
  73. ax.set_zlim(-2,2)
  74. a=ef()
  75. a.field2()
  76. a.show_field()

acknowledgement

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