@OrionPaxxx
2016-12-12T04:51:18.000000Z
字数 4728
阅读 1293
computationalphysics
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
import mathimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dimport numpy as npfrom matplotlib import cmfrom matplotlib.ticker import LinearLocator, FormatStrFormatterclass ef:#初始化创建二维n*n全为0的矩阵,在后面的函数中通过修改矩阵中元素的值来构建不同的电场def __init__(self,alpha=1,n=20):self.n=nself.field=[[0]*(self.n) for i in range(self.n)]#教材figure5.2所示电场def field1(self):self.field[0]=np.linspace(-1,1,self.n)self.field[-1]=np.linspace(-1,1,self.n)for k in range(1,self.n-1):self.field[k][0]=-1self.field[k][-1]=1self.kk=1while(1):self.kk+=1for i in range(1,self.n-1):for j in range(1,self.n-1):self.field[i][j]=(self.field[i+1][j]+self.field[i-1][j]+self.field[i][j+1]+self.field[i][j-1])/4if self.kk>1000:break#教材figure5.4所示电场def field2(self):for i in range(int(self.n/4),int(self.n*3/4)):for j in range(int(self.n/4),int(self.n*3/4)):self.field[i][j]=1self.kk=1while(1):self.kk+=1for i in range(1,self.n-1):for j in range(1,self.n-1):if int(self.n/4)<=i<=int(self.n*3/4) and int(self.n/4)<=j<=int(self.n*3/4):self.field[i][j]=1else:self.field[i][j]=(self.field[i+1][j]+self.field[i-1][j]+self.field[i][j+1]+self.field[i][j-1])/4if self.kk>1000:break#教材figure5.6所示电场def field3(self):for i in range(1,self.n-1):for j in range(1,self.n-1):if j==int(self.n/4) and i>=self.n/4 and i<=self.n*3/4:self.field[i][j]=1elif j==int(self.n*3/4) and i>=self.n/4 and i<=self.n*3/4:self.field[i][j]=-1self.kk=1while(1):self.kk+=1for i in range(1,self.n-1):for j in range(1,self.n-1):if j==int(self.n/4) and i>=self.n/4 and i<=self.n*3/4:self.field[i][j]=1elif j==int(self.n*3/4) and i>=self.n/4 and i<=self.n*3/4:self.field[i][j]=-1else:self.field[i][j]=(self.field[i+1][j]+self.field[i-1][j]+self.field[i][j+1]+self.field[i][j-1])/4if self.kk>5000:breakdef show_field(self):fig = plt.figure()ax = Axes3D(fig)X=np.linspace(-1,1,self.n)Y=np.linspace(-1,1,self.n)X, Y = np.meshgrid(X, Y)Z=self.fieldax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.cm.hot)ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.cm.hot)ax.set_zlim(-2,2)a=ef()a.field2()a.show_field()
a simple example of relaxation algorithm

2.Electric potemtial and field inside the prism

3.electric potential of capacitor plates
