繁体   English   中英

电场线

[英]Electric Field Lines

L = 10
Ex = np.zeros([L,L])         # 2D array to store the Ex and
Ey = np.zeros([L,L])  

nq = 2
for i in range(nq): 
    q = random.randrange(-1,2,1) #indicates charge is poistive or negative
    qx = random.randrange(1,N) #indicates the positions of the charge
    qy = random.randrange(1,N)
for i in range(N): 
    for j in range(N): 
        denom = (((i-qx)**2.0+(j-qy)**2.0)**(1.5))
        if denom != 0: 
            Ex[i,j] += (q*(i-qx))/ denom
            Ey[i,j] += (q*(j-qy))/denom
        else: 
            continue

plot(Ex, Ey, color='b') #Could this code also be optimized in streamplot?
show() 

在此程序中,我尝试创建2个电荷的电场线(然后希望是N个电荷)。我的方法如下:

步骤1:定义一个LxL的窗口

步骤2:为电荷选择随机位置并确定幅度(在这种情况下,我只是将其幅度设为-1,0,1)-我的随机位置是否需要为2D?

步骤3:为E Ex(L,L)和Ey(L,L)选择一个数组

步骤4:在ix和iy的嵌套循环中

Ex = x / r ** 3,x =(dx-ix)a,其中a是间距。

目前,看来我的代码目前只绘制了1个费用。

要获得所需的内容,可以使用quiver图,并应纠正代码中的错误。 这是修改代码以可视化电场强度的方式:

import numpy as np
import matplotlib.pyplot as plt
import random

np.seterr(divide='ignore', invalid='ignore')

# grid size
N = 15
M = 25
# coordinates
X = np.arange(0, M, 1)
Y = np.arange(0, N, 1)
X, Y = np.meshgrid(X, Y)
# strength
Ex = np.zeros((N, M))
Ey = np.zeros((N, M))
# amount of charges
nq = 3

# computing
qq = [[], []]  # to store charges coordinates
for dummy in range(nq): 
    q = random.choice([-1, 1])
    qx, qy = random.randrange(1, N), random.randrange(1, M)
    # print(q, qx, qy)
    qq[0].append(qy)
    qq[1].append(qx)
    for i in range(N):
        for j in range(M):
            denom = ((i - qx) ** 2 + (j - qy) ** 2) ** 1.5
            if denom != 0: 
                Ex[i, j] += q * (j - qy) / denom
                Ey[i, j] += q * (i - qx) / denom

# arrows color
C = np.hypot(Ex, Ey)
# normalized values for arrows to be of equal length
E = (Ex ** 2 + Ey ** 2) ** .5
Ex = Ex / E
Ey = Ey / E

# drawing
plt.figure(figsize=(12, 8))
# charges
plt.plot(*qq, 'bo')
# field
plt.quiver(X, Y, Ex, Ey, C, pivot='mid')
# colorbar for magnitude
cbar = plt.colorbar()
cbar.ax.set_ylabel('Magnitude')
# misc
plt.title('Electric Field Strength')
plt.axis('equal')
plt.axis('off')
plt.show()

结果:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM