Brute-Forcing a Sphere as a Gaussian Surface

I'm not going to bury the lede. The blog post below describes my experience programming; a later post will explain how I'm going to use the program next year. The program shows the flux as brighter or dimmer shades of red (flux out of the surface) and blue (flux into the surface). Play with it. Zoom in and out. Rotate the sphere. Edit and break the program. (I tried to write a lot in the comments, so hopefully you can change a number or two and see what happens.) Give me suggestions. (My students like running it in fullscreen. Find that in the menu at the upper left of the screen.) Here you go:

I'm teaching AP Physics C: Electricity and Magnetism as a second-semester class to students who already took AP Physics 1 and 2. They've done most of my great labs, and now, especially when talking about fields, we're stuck in abstract problem land. I've always wanted to do more programming and computational thinking for these students, but only now am I slowly entering into it.

What I'm finding, though, is I need to spend some time programming and feeling the frustration and knowing how to help before I feel comfortable having my students program. So, this year, I've been using GlowScript to have students visualize the difficult three-dimensional ideas of E&M.

I read Rhett Allain's great post on Gauss's Law and vPython and I stole his program. (Thanks! I'm pretty sure that's OK, right?) And then I started using it as a guide to write my own program in GlowScript on Trinket I started with the cube, but that isn't the only Gaussian Surface we use. What about the cylinder? The sphere? So I wrote programs for both of those. I'm going to focus on my challenges on making a Gaussian surface in GlowScript.

First, how do you tile a sphere? How do you find the little dA's that add up to surface of a sphere? I asked the multivariable calculus teacher at school, and, together, we couldn't figure out a good solution. I drew some diagrams and started calling it the "disco ball problem" in my head. But it turns out the solution I found (after googling sector of a sphere) was nothing like that. I found my solution in a Wolfram post about the zones of a sphere when I realized that parallel planes equally spaced split the surface area into equal parts. Then, for each of those zones I could split them into equally-sized parts by going around the circle from 0 to 2π. So, as I went around each part of the sphere at the same z-coordinate, I'd go around the circle, sampling the electric field. Every sample would be attached to a dA and all the dA's for the surface area would have the same area. Nice!

Then I had to figure out how to draw those little dA's. Could i draw little squares (or rectangles) like the disco ball idea? I tried it--it was terrible. What I hadn't realized until I programmed it and saw it was that each dA might have the same area but they don't have the same shape. The dA's near the minimum and maximum of the z-coordinate were very triangular while those near the equator were basically rectangles. So it was time to draw quadrilaterals, and a quick trip to GlowScript help taught me the syntax. 

But, when the program would draw the sphere, I'd often end up with an open sphere. One end would be closed, but the other end wouldn't show, even though the calculations came out as expected. It wasn't happening with all values of zones, the variable I used to split the surface of the sphere into n-by-n dA's. It took me way too long to realize what I should do. I should print the values of the points being plotted. Such a basic debugging technique! I had lots of NaN's when I finally did that. I didn't know that NaN meant "not a number." (I learned a lot by programming this.) I realized it came from a rounding error. When finding the circle of the sphere's surface at the z-coordinate, it would sometimes find an imaginary radius. I was taking the square root of a negative number that was close to zero! I'll just force it to be zero. So I learned how to check if a variable is NaN and used that to force the radius to be zero if this rounding error occurred.

Now it works! When the charge is close to the surface of the sphere, the little dA's must be pretty small to get the correct flux. Other than a weird dark line shadow on the sphere, I'm pretty satisfied with this program. I have no idea why that's happening? Do you know why?