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()
In this program I am trying to create the Electric Field Lines of 2 Electric Charges(then hopefully N # of charges) My approach was as follows:
Step1: Define a window of LxL
Step2: Pick random locations for the charges and determining the magnitude.(In this case, I just took it be magnitude of -1,0,1) - Does my random positions need to be 2-D?
Step3: Pick an array for E Ex(L,L) and Ey(L,L)
Step4: In a nested loop over ix and iy
Ex = x/r**3 , x = (dx - ix)a, where a is the spacing.
Currently, it seems that my code is currently plotting only 1 charge.
