# Simulating the Electric Field inside a Hollow Sphere of Uniform Charge

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 - point
diffy = testpoint - point
diffz = testpoint - point
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?

## 1 Answer

You will never get exactly zero for two reasons.

1. You are making a discrete approximation to a continuous distribution, and
2. computers have limited precision. They get the absolutely correct answer to floating point operations only very rarely (such as the multiplication of two integers.)

You need to compare the value that you calculated to something else in order to determine if the answer is small. I'd suggest comparing to the the magnitude of the electric field at the origin due to one of your charges.