[关闭]
@zy-0815 2016-12-12T01:25:59.000000Z 字数 4314 阅读 1379

计算物理第十二次作业Problem5.1


摘要

在前面的课程中我们已经学习了微分方程的求解方式,而这一节我们将通过Gauss-Sideal Method来求解拉普拉斯方程。

背景介绍

已知电场的Laplace's equation为:


微分可得:

由5.1可知,Gauss-Sideal Method有:

正文

关于电势的程序为:

  1. from __future__ import division
  2. import matplotlib
  3. import numpy as np
  4. import matplotlib.cm as cm
  5. import matplotlib.mlab as mlab
  6. import matplotlib.pyplot as plt
  7. from mpl_toolkits.mplot3d import Axes3D
  8. # initialize the grid
  9. grid = []
  10. for i in range(201):
  11. row_i = []
  12. for j in range(201):
  13. if i == 0 or i == 200 or j == 0 or j == 200:
  14. voltage = 0
  15. elif 70<=i<=130 and j == 70:
  16. voltage = 1
  17. elif 70<=i<=130 and j == 130:
  18. voltage = 1
  19. elif 70<=j<=130 and i == 70:
  20. voltage = 1
  21. elif 70<=j<=130 and i == 130:
  22. voltage = 1
  23. else:
  24. voltage = 0
  25. row_i.append(voltage)
  26. grid.append(row_i)
  27. # define the update_V function (Gauss-Seidel method)
  28. def update_V(grid):
  29. delta_V = 0
  30. for i in range(201):
  31. for j in range(201):
  32. if i == 0 or i == 200 or j == 0 or j == 200:
  33. pass
  34. elif 70<=i<=130 and j == 70:
  35. pass
  36. elif 70<=i<=130 and j == 130:
  37. pass
  38. elif 70<=j<=130 and i == 70:
  39. pass
  40. elif 70<=j<=130 and i == 130:
  41. pass
  42. else:
  43. voltage_new = (grid[i+1][j]+grid[i-1][j]+grid[i][j+1]+grid[i][j-1])/4
  44. voltage_old = grid[i][j]
  45. delta_V += abs(voltage_new - voltage_old)
  46. grid[i][j] = voltage_new
  47. return grid, delta_V
  48. # define the Laplace_calculate function
  49. def Laplace_calculate(grid):
  50. epsilon = 10**(-5)*200**2
  51. grid_init = grid
  52. delta_V = 0
  53. N_iter = 0
  54. while delta_V >= epsilon or N_iter <= 10:
  55. grid_impr, delta_V = update_V(grid_init)
  56. grid_new, delta_V = update_V(grid_impr)
  57. grid_init = grid_new
  58. N_iter += 1
  59. return grid_new
  60. matplotlib.rcParams['xtick.direction'] = 'out'
  61. matplotlib.rcParams['ytick.direction'] = 'out'
  62. x = np.linspace(0,200,201)
  63. y = np.linspace(0,200,201)
  64. X, Y = np.meshgrid(x, y)
  65. Z = Laplace_calculate(grid)
  66. plt.figure()
  67. CS = plt.contour(X,Y,Z,10)
  68. plt.clabel(CS, inline=1, fontsize=12)
  69. plt.title('voltage near capacitor')
  70. plt.xlabel('x(m)')
  71. plt.ylabel('y(m)')
  72. fig = plt.figure()
  73. ax = fig.add_subplot(111, projection='3d')
  74. ax.plot_surface(X, Y, Z)
  75. ax.set_xlabel('x(m)')
  76. ax.set_ylabel('y(m)')
  77. ax.set_zlabel('voltage(V)')
  78. ax.set_title('voltage distribution')
  79. plt.show()

运行结果为:
image_1b3nd30301hfpics1h5n2ah1tvi9.png-51.8kB
image_1b3nd4099147gonm1ci1vbv8dum.png-47.6kB
关于电场的程序为:

  1. from __future__ import division
  2. import matplotlib
  3. import numpy as np
  4. import matplotlib.cm as cm
  5. import matplotlib.mlab as mlab
  6. import matplotlib.pyplot as plt
  7. from mpl_toolkits.mplot3d import Axes3D
  8. from copy import deepcopy
  9. # initialize the grid
  10. grid = []
  11. for i in range(201):
  12. row_i = []
  13. for j in range(201):
  14. if i == 0 or i == 200 or j == 0 or j == 200:
  15. voltage = 0
  16. elif 70<=i<=130 and j == 70:
  17. voltage = 1
  18. elif 70<=i<=130 and j == 130:
  19. voltage = 1
  20. elif 70<=j<=130 and i == 70:
  21. voltage = 1
  22. elif 70<=j<=130 and i == 130:
  23. voltage = 1
  24. else:
  25. voltage = 0
  26. row_i.append(voltage)
  27. grid.append(row_i)
  28. # define the update_V function (Gauss-Seidel method)
  29. def update_V(grid):
  30. delta_V = 0
  31. for i in range(201):
  32. for j in range(201):
  33. if i == 0 or i == 200 or j == 0 or j == 200:
  34. pass
  35. elif 70<=i<=130 and j == 70:
  36. pass
  37. elif 70<=i<=130 and j == 130:
  38. pass
  39. elif 70<=j<=130 and i == 70:
  40. pass
  41. elif 70<=j<=130 and i == 130:
  42. pass
  43. else:
  44. voltage_new = (grid[i+1][j]+grid[i-1][j]+grid[i][j+1]+grid[i][j-1])/4
  45. voltage_old = grid[i][j]
  46. delta_V += abs(voltage_new - voltage_old)
  47. grid[i][j] = voltage_new
  48. return grid, delta_V
  49. # define the Laplace_calculate function
  50. def Laplace_calculate(grid):
  51. epsilon = 10**(-5)*200**2
  52. grid_init = grid
  53. delta_V = 0
  54. N_iter = 0
  55. while delta_V >= epsilon or N_iter <= 10:
  56. grid_impr, delta_V = update_V(grid_init)
  57. grid_new, delta_V = update_V(grid_impr)
  58. grid_init = grid_new
  59. N_iter += 1
  60. return grid_new
  61. x = np.linspace(0,200,201)
  62. y = np.linspace(0,200,201)
  63. X, Y = np.meshgrid(x, y)
  64. Z = Laplace_calculate(grid)
  65. Ex = deepcopy(Z)
  66. Ey = deepcopy(Z)
  67. E = deepcopy(Z)
  68. for i in range(201):
  69. for j in range(201):
  70. if i == 0 or i == 200 or j == 0 or j == 200:
  71. Ex[i][j] = 0
  72. Ey[i][j] = 0
  73. else:
  74. Ex_value = -(Z[i+1][j] - Z[i][j])/2
  75. Ey_value = -(Z[i][j+1] - Z[i][j])/2
  76. Ex[i][j] = Ex_value
  77. Ey[i][j] = Ey_value
  78. for i in range(201):
  79. for j in range(201):
  80. E_value = np.sqrt(Ex[i][j]**2 + Ey[i][j]**2)
  81. E[i][j] = E_value
  82. fig0, ax0 = plt.subplots()
  83. strm = ax0.streamplot(X, Y, np.array(Ey), np.array(Ex), color=np.array(E), linewidth=2, cmap=plt.cm.autumn)
  84. fig0.colorbar(strm.lines)
  85. ax0.set_xlabel('x(m)')
  86. ax0.set_ylabel('y(m)')
  87. ax0.set_title('Electric field')
  88. plt.show()

运行结果为:
image_1b3nds08i1b6mid711pkk8s19it13.png-60.9kB

结论

对于prism geometry,势能和电场便如程序运行结果所示。

致谢

感谢贺一珺同学的程序。

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