A9 · Lesson 39 · Lab: flux, divergence and curl
Checking Gauss, Stokes and Biot–Savart numerically
The divergence and Stokes theorems connect what happens inside a region to what crosses or circles its boundary. In this lab you'll test them numerically: derivatives of a field on a grid, the flux of a point charge through spheres that do and don't contain it, the flux out of a cube computed both ways, the magnetic field of a current loop summed segment by segment, and the circulation of a wire's field around loops that do and don't enclose it.
- The field is that of a line source at the origin, , a two-dimensional point charge.
- Its flux through any closed curve is if the curve encloses the source and zero if it doesn't. The readout applies that to a circle of radius centered at distance from the source.
- Slide past : the flux drops from to zero at once, however close the curve still is.
Predict
A point charge sits just outside a closed sphere. What is the net electric flux through the sphere?
Zero. Every field line that enters the sphere on the near side leaves on the far side. The field is strong on the near side and weak on the far side, but the near-side patch it crosses is seen at a steep angle and the far-side patch is larger, and the two cancel exactly. Only enclosed charge produces net flux.
Name it
Derivatives on a grid
np.gradient(F, h, axis=k) estimates by central differences in the interior. Divergence and curl come from combining them:
div = np.gradient(Fx, h, axis=0) + np.gradient(Fy, h, axis=1)
curl_z = np.gradient(Fy, h, axis=0) - np.gradient(Fx, h, axis=1)
(with arrays indexed [i, j] for , ).
Flux through a sphere
On a grid of angles, the outward normal is and . Sum over the cells.
Biot–Savart
A current loop's field is the sum over short segments :
with from each segment to the field point. On the axis of a circular loop of radius , the exact result is .
Worked example
Check the divergence theorem for on the unit cube.
Show the solution
- Divergence.
- Integrate it over the cube.
- Flux through the far faces. At , , giving ; at , , giving ; at , , giving .
- Flux through the near faces. The three faces through the origin have zero normal component.
- Compare. The total flux is 1.5, matching.
Answer both sides give 1.5.
Worked example
Find the field on the axis of a 5 cm loop carrying 1 A, 3 cm from its center.
Show the solution
- On-axis field of a loop.
- Check numerically. Summing Biot–Savart over 2000 segments of the loop gives the same to thirteen digits: on the axis the sum is a symmetric average, and it converges very fast.
Answer the field is 7.92 µT along the axis.
Check
A loop of radius 2 cm carries 1 A. What is the field on its axis, 3 cm from the center?
Write units the way you normally would: 1.5 µT, 1500 nT and 0.0015 mT are the same answer.
Check
An 8 nC charge sits at the center of a cube. What is the flux through one face?
Write units the way you normally would: 1.5 V·m, 1500 mV·m and 0.0015 kV·m are the same answer.
Check
On a 101 × 101 grid over , compute the divergence and the curl's component of with np.gradient. Set err_div and err_curl to the largest differences from the exact and at interior points.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write flux(c, a, n=400) returning the flux of (a point charge at the origin) through a sphere of radius a centered at c, with a midpoint grid of n × 2n angles. Set f1, f2, f3 for spheres of radius 1 centered at , and .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Check the divergence theorem for on the unit cube: compute the volume integral of with a 100³ midpoint grid and the outward flux through the six faces with 100² midpoint grids. Set vol and surf.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Sum Biot–Savart over 2000 segments of a loop of radius 5 cm carrying 1 A to get at 3 cm along the axis. Set Bz and formula.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A long wire along carries 2 A, with . Compute the circulation of around the square with corners (enclosing the wire) and around the square from to (not enclosing it), 4000 midpoint segments per side. Set c1 and c2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
A field's flux through a closed surface is zero. Does that mean the field is zero inside?
No. Zero net flux only means that as much flux leaves as enters, so the total divergence inside, or the enclosed source, is zero. A uniform field passes straight through any closed surface with zero net flux, and the field of a charge outside the surface does too, though both are nonzero everywhere inside. Even with charges inside, positive and negative ones can cancel in the total. Gauss's law constrains the total source, not the field's value at any point; to get the field itself you also need symmetry, as in the textbook Gauss's-law problems, or a full solution of the field equations.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.