首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从电势中求出电场?

如何从电势中求出电场?
EN

Stack Overflow用户
提问于 2020-04-18 08:57:10
回答 1查看 200关注 0票数 1

这是我在电离室中找到电势的脚本:

代码语言:javascript
复制
# Programme de résolution de l'équation de Laplace
# par la méthode de Gauss-seidel

# importation des librairies
import numpy as np
import time
import matplotlib.pyplot as plt

# définition des paramétres physiques de l'expérience
Vi = 300.0           # le couvercle est à  300 V
V0 = 0.0             # les cotés sont au potentiel nul
x = 0
# définition de la grille de calcul
M = 50
N = 300                     # nombre de pas sur la grile (identique en Ox et Oy)
V = np.zeros([M,N])     # grille de calcul courante
Vnew = np.zeros([M,N])  # grille de stockage des calculs nouveaux

# critère de précision du calcul
EPS = 1e-3
# initialisation des compteurs        
ecart = 1.0
iteration = 0

# définition des conditions aux limites
V[0:8, 0:20] = x
V[8:15, 0:10] = x
V[35:50, 0:10] = x
V[42:50, 0:20] = x
V[0,20:300]  = Vi  # bord supérieur
V[0:8, 20]  = Vi  # vue que 1.6/0,2 = 8
V[41:50, 20] = Vi # bas de la bord gauche
V[8, 10:21] = Vi
V[15, 0:11] = Vi
V[34, 0:11] = Vi
V[41, 10:21] = Vi
V[8:15, 10] = Vi
V[34:41, 10] = Vi
V[15:35, 0] = Vi
V[:,-1] = Vi  # bord droit
V[-1,20:300] = Vi  # bord inférieur
V[23:27,15:299] = V0

# début du calcul - enregistrement de la durée
tdebut = time.time()

# boucle de calcul - méthode de Gauss-Seidel
while ecart > EPS:
  iteration += 1
  # sauvegarde de la grille courante pour calculer l'écart
  Vavant = V.copy()
  # calcul de la nouvelle valeur du potentiel sur la grille
  V[1:-1,1:-1]= 0.25*(Vavant[0:-2,1:-1] +V[2:,1:-1] + Vavant[1:-1,0:-2] + V[1:-1,2:])
  # on repose les même conditions
  V[0:8, 0:20] = x
  V[8:15, 0:10] = x
  V[35:50, 0:10] = x
  V[42:50, 0:20] = x
  V[0,20:300]  = Vi 
  V[0:8, 20]  = Vi  
  V[41:50, 20] = Vi 
  V[8, 10:21] = Vi
  V[15, 0:11] = Vi
  V[34, 0:11] = Vi
  V[41, 10:21] = Vi
  V[8:15, 10] = Vi
  V[34:41, 10] = Vi
  V[15:35, 0] = Vi
  V[:,-1] = Vi  
  V[-1,20:300] = Vi  
  V[23:27,15:299] = V0
  # critère de convergence
  ecart = np.max(np.abs(V-Vavant))

# fin du calcul - affichage de la durée
print ('Nombre iterations : ',iteration  )  
print ('Temps de calcul (s) : ',time.time() - tdebut)

v, u = np.gradient(V)

x = np.linspace(0, 1, N) # x-axis
y = np.linspace(0, 1, M) # y-axis

X, Y= np.meshgrid(x,y)

# Create figure
fig, axarr = plt.subplots(1, 1, figsize=(14, 8), dpi=300)

# Axes 1: Electric potential and field
im1 = axarr[0].contourf(x, y, V, 20)
axarr[0].streamplot(x, y, -u, -v, color="k")
axarr[0].set_title("Electric potential and field")
fig.colorbar(im1, orientation='horizontal', ax=axarr[0],
                  label=r"Electric potential, $V/V_0$")
axarr[0].set_xlabel("$x/L$")
axarr[0].set_ylabel("$y/L$")

但是我在affichage方法中有一个问题,错误在第90行:

代码语言:javascript
复制
line 90, in <module>
    im1 = axarr[0].contour(x, y, V, 20)
TypeError: 'AxesSubplot' object is not subscriptable

我几乎什么都试过了,但还是找不到解决方案。如何修复此错误?

EN

回答 1

Stack Overflow用户

发布于 2020-04-19 02:43:41

matplotlib.pyplot.subplots返回matplotlib.axes.Axes实例的数组或单个matplotlib.axes.Axes实例。后者仅在行数(第一个位置参数)和列数(第二个位置参数)都为1时使用。所以语法

代码语言:javascript
复制
fig, ax = plt.subplots(1, 1)

创建单个Axes实例(也称为AxesSubplot),因此ax[0]是非法的-只有当plt.subplots创建了多个子图时,它才有效。代码中的正确语法是

代码语言:javascript
复制
fig, axarr = plt.subplots(1, 1, figsize=(14, 8), dpi=300)

im1 = axarr.contourf(x, y, V, 20)
axarr.streamplot(x, y, -u, -v, color="k")
axarr.set_title("Electric potential and field")
fig.colorbar(im1, orientation='horizontal', ax=axarr,
                  label=r"Electric potential, $V/V_0$")
axarr.set_xlabel("$x/L$")
axarr.set_ylabel("$y/L$")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61282997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档