I've recently been studying Gauss's Law and have come across some results that I want to verify. For one, I am trying to verify that the electric field inside a conductor (in this case it could be hollow or solid as the charges will be uniformly distributed along the surface no matter what) is indeed 0. I found that this must be true in order for the charges to remain stationary and due to the symmetry created with Gauss's Law, especially with those problems that involve finding the electric field in a cavity with a point charge in it. Because the charge in the cavity is dependent on only the point charge (according to Gauss's Law), this must imply that all the other charges along the inside wall of the cavity and the outside wall of the sphere must create a net electric field of 0. I tried to simulate this to verify it.
So I used the algorithm describe here in order to generate a set of uniformly ditributed points on a sphere. Here is a visual plot of the resulting points (the red dot is the test point):
Then, I made the following method to calculate the electric field from each of these points at some test point:
import math
def calc_efield(point, testpoint):
k = 1
diffx = testpoint[0] - point[0]
diffy = testpoint[1] - point[1]
diffz = testpoint[2] - point[2]
dist = math.sqrt((diffx ** 2) + (diffy ** 2) + (diffz ** 2))
alpha = math.acos(abs(diffx/dist))
beta = math.acos(abs(diffy/dist))
gamma = math.acos(abs(diffz/dist))
eField = k/(dist**2)
eFieldx = math.cos(alpha) * eField
eFieldy = math.cos(beta) * eField
eFieldz = math.cos(gamma) * eField
if abs(diffx) < .001:
eFieldx = 0
elif diffx < 0:
eFieldx = eFieldx * -1
if abs(diffy) < .001:
eFieldy = 0
elif diffy < 0:
eFieldy = eFieldy * -1
if abs(diffz) < .001:
eFieldz = 0
elif diffz < 0:
eFieldz = eFieldz * -1
return eFieldx, eFieldy, eFieldz
However, after iterating through each of these points and summing the electric field in all directions, I only arrive at a relatively net zero result when the test point is at the origin (0,0,0) (and even then it is not exactly zero).
Is there something I'm missing either in physical predictions made in the first paragraph or in my code? I made N=2e3 -- is that sufficiently large?