@2014301020054
2016-12-12T01:32:25.000000Z
字数 9051
阅读 649
python physics 2014301020054 陈佩卓
Numerically, we get that:
Specially, for the 2-dimensional case:
Worth mentioning,whilw the performance if the Gauss-Seidel akgorithm is better than that of the Jacobi method,the imporvement is only modest.It turns out that the number of iterations required for convergence with the Gauss-Siedel method is smaller,but only by a factor of 2.
But the benefits in Gauss-Seidel method is obvious,cause there is no need for us to save the old values seperately.In other words, the updating can be performed in place with using an algorithm of the form.
where is a factor that measures how much we "over-relax".Choosing tuekds the Gauss-Seidel method.It turns out that if , the method does bit converge.The best choice of is:
Problem 5.7
Write two problems to slove the capacitor problem of Figures 5.6 and 5.7, one using the Jacobi method and one using the SOR algorithm. For a fixed accuracy (as set by the convergence test) compare the number of iterations, , that each algorithm requires as a function of the number of the grid elelments, . Show that for the Jacobi method ~, while with SOR ~.
Codes
For Jacobi method:
import matplotlib.pyplot as plimport numpy as npfrom mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cmfrom matplotlib.ticker import LinearLocator, FormatStrFormatterclass Jacobi:def __init__(self):passdef initialization(self,initial_file):itxt= open(initial_file)self.lattice_in = []self.lattice_out = []for lines in itxt.readlines():lines=lines.replace("\n","").split(",")#print (lines[0].split(" ")self.lattice_in.append(lines[0].split(" "))self.lattice_out.append(lines[0].split(" "))itxt.close()#print (self.lattice_in)#print (self.initial_lattice)for i in range(len(self.lattice_in)):for j in range(len(self.lattice_in[i])):self.lattice_in[i][j] = float(self.lattice_in[i][j])self.lattice_out[i][j] = float(self.lattice_out[i][j])return 0def evolution(self):delta = 10self.N = 0delta_record = []while (delta > 0.00001*len(self.lattice_in)*len(self.lattice_in[0])):delta = 0for i in range(1,len(self.lattice_in) - 1):for j in range(1,len(self.lattice_in[i]) - 1):if (self.lattice_in[i][j] != 1 and self.lattice_in[i][j] != -1):self.lattice_out[i][j] = 0.25*(self.lattice_in[i - 1][j] + self.lattice_in[i + 1][j] + self.lattice_in[i][j - 1] + self.lattice_in[i][j + 1])delta+= abs(self.lattice_out[i][j] - self.lattice_in[i][j])delta_record.append(delta)for i in range(len(self.lattice_in)):for j in range(len(self.lattice_in[i])):self.lattice_in[i][j] = self.lattice_out[i][j]self.N+= 1pl.plot(delta_record,label = 'jacobi method')print (self.N)print (len(self.lattice_in))return 0def electric_field(self):self.ex = np.zeros([len(self.lattice_in),len(self.lattice_in)])self.ey = np.zeros([len(self.lattice_in),len(self.lattice_in)])deltax = 0.3333deltay = 0.3333for i in range(1,len(self.lattice_in) - 1):for j in range(1,len(self.lattice_in[0]) - 1):self.ey[i][j] = (-(self.lattice_in[i + 1][j] - self.lattice_in[i - 1][j])/(2*deltax))self.ex[i][j] = (-(self.lattice_in[i][j + 1] - self.lattice_in[i][j - 1])/(2*deltay))return 0def plot_field(self):self.electric_field()X, Y = np.meshgrid(np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)), np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)))#pl.figure(figsize = (8,8))pl.quiver(X, Y, self.ex, self.ey, pivot = 'mid', units='width')pl.title('Electric field between two metal plates')#pl.show()return 0def plot_contour(self):X, Y = np.meshgrid(np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)), np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)))#pl.figure(figsize = (8,8))CS = pl.contour(X, Y, self.lattice_in, 13)pl.clabel(CS, inline=1, fontsize=10)pl.title('Equipotential lines')#pl.show()return 0def plot_surface(self):fig = pl.figure()ax = fig.gca(projection='3d')X, Y = np.meshgrid(np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)), np.arange(-1.00, 1.01, 2./(len(self.lattice_in) - 1)))surf = ax.plot_surface(X, Y, self.lattice_in, rstride=1, cstride=1,cmap = cm.coolwarm_r,linewidth=0, antialiased=False)ax.set_zlim(-1.01, 1.01)ax.zaxis.set_major_locator(LinearLocator(10))ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))fig.colorbar(surf, shrink=0.5, aspect=5)#A = Jacobi()A = SOR()A.initialization('initial_capacitor_26.txt')A.evolution()#pl.figure(figsize = (8,8))pl.figure(figsize = (16,8))pl.subplot(121)A.plot_contour()pl.subplot(122)A.plot_field()#A.plot_surface()pl.savefig('chapter5_5.7_26.png', dpi = 256)pl.show()
For SOR Method:
class SOR(jacobi):def evolution(self):delta = 10self.N = 0delta_record = []alpha = 2/(1+np.pi/len(self.lattice_in))while (delta > 0.00001*len(self.lattice_in)*len(self.lattice_in[0])):delta = 0for i in range(1,len(self.lattice_in) - 1):for j in range(1,len(self.lattice_in[i]) - 1):if (self.lattice_in[i][j] != 1 and self.lattice_in[i][j] != -1):self.lattice_out[i][j] = 0.25*(self.lattice_in[i - 1][j] + self.lattice_in[i + 1][j] + self.lattice_in[i][j - 1] + self.lattice_in[i][j + 1])delta+= abs(self.lattice_out[i][j] - self.lattice_in[i][j])self.lattice_in[i][j] = alpha * (self.lattice_out[i][j] - self.lattice_in[i][j]) + self.lattice_in[i][j]#print (self.lattice_out)delta_record.append(delta)self.N+= 1pl.plot(delta_record, label ='SOR algorithm')print (self.N)print (len(self.lattice_in))#print (self.lattice_in)return 0class SOR_2(jacobi):def evolution(self,alpha):delta = 10self.N = 0delta_record = []#alpha = 2/(1+np.pi/len(self.lattice_in))while (delta > 0.00001*len(self.lattice_in)*len(self.lattice_in[0])):delta = 0for i in range(1,len(self.lattice_in) - 1):for j in range(1,len(self.lattice_in[i]) - 1):if (self.lattice_in[i][j] != 1 and self.lattice_in[i][j] != -1):self.lattice_out[i][j] = 0.25*(self.lattice_in[i - 1][j] + self.lattice_in[i + 1][j] + self.lattice_in[i][j - 1] + self.lattice_in[i][j + 1])delta+= abs(self.lattice_out[i][j] - self.lattice_in[i][j])self.lattice_in[i][j] = alpha * (self.lattice_out[i][j] - self.lattice_in[i][j]) + self.lattice_in[i][j]#print (self.lattice_out)delta_record.append(delta)self.N+= 1#pl.plot(delta_record, label ='SOR algorithm')print (self.N)#print (len(self.lattice_in))#print (self.lattice_in)return 0

Solving the partial differential equation of two capacitor plates

Then, enlarging the grids to 26*26 leads to smoother equipotential curve and a finer structure of electric field.

The relationship between the number of iterations and that of grid elements.

The Convergence speed

The of SOR algorithm
