B2 · Lesson 13 · Computing fields
Superposition by computer, checked against the formulas
The formulas of lesson 5 cover a handful of symmetric shapes. A real electrode, a PCB trace or an irregular charged part has no formula. But superposition still holds, so a computer can do what the integral does: chop the charge into many small pieces, add up their fields as point charges, and refine until the answer stops changing. Field solvers in electronics design start from exactly this idea. In this lab you'll write one in NumPy and test it against cases where the answer is known.
- The solid curve is the exact field of a charged rod (half-length , 2 nC/m) on its perpendicular bisector. The dashed curves are an infinitely long line and a point charge with the rod's total charge.
- Close to the rod it looks like an infinite line; far away, like a point. In between, neither formula works, and that's where numerical summing earns its keep.
- Make the rod longer. Where does the changeover move?
Predict
You approximate a charged rod by equal point charges at the middles of equal segments. Roughly how does the error behave as grows?
It shrinks as about . Placing each point charge at its segment's middle is the midpoint rule for the integral (A6), whose error falls as the square of the segment length. For a 10 cm rod seen from 2 cm away, 10 pieces are within 0.06% and 50 pieces within 0.002%.
Name it
Superposition in code
For point charges at positions , the field at a point is
In NumPy, with sources as an array and field points as , broadcasting builds all separation vectors at once: d = pts[:, None, :] - src[None, :, :]. Divide by the cube of each distance, weight by the charges, and sum over the sources.
Discretizing continuous charge
Chop a line of charge into segments of length , each carrying at its midpoint. As grows the sum approaches the integral, with error about . Surfaces work the same way with small patches carrying .
Testing a field code
- One point charge must give exactly , pointing away.
- A symmetric shape must reproduce its formula: a rod on its bisector, a ring on its axis.
- Refine and compare: doubling should change the answer by about a quarter of the remaining error.
- Check far-field behavior: any neutral pair of charges must fall off as far away, the dipole law; measuring the exponent on a log-log scale checks the whole calculation at once.
Worked example
A 10 cm rod carries 2 nC/m. What is its field 2 cm out on its bisector, exactly and with 10 point charges?
Show the solution
- Exact field.
- Ten point charges. With 10 midpoint charges of 0.2 nC each, the sum is 1669.9 V/m, high by 0.055%.
- Two hundred point charges. With 200, it's 1669.03 V/m.
Answer the exact field is 1669.0 V/m; 10 point charges give 1669.9 V/m (0.055% high), and 200 give 1669.03 V/m.
Worked example
How can you check numerically that a dipole's field falls as ?
Show the solution
- Two distances. Compute the field at two distances and along the axis.
- Slope on a log-log plot.
- Far out. For ±1 nC charges 1 mm apart, distances of 2 and 5 cm give −3.001.
- Close in. At 2 and 4 mm, it's −3.14: the dipole law holds only far compared with the separation.
Answer find the log-log slope between two distances on the axis; for this dipole it's −3.001 at 2 and 5 cm, very close to −3.
Check
Step 1. Write field(q, src, pts) returning an array of field vectors at the points pts, from charges q (length ) at positions src (), using = 8.988 × 10⁹.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write rod_field(lam, a, y, n) returning at from a rod along from to carrying lam C/m, approximated by n charges at segment midpoints. field is included.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write axis_exponent(q, d, r1, r2): for charges at and at , compute the field strength on the axis at and and return . field is included.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 4. A midpoint-rule field sum with 10 pieces is off by 0.06%. The error falls as . What error do you expect with 4 times as many pieces, in percent?
Explain why
Why is placing each piece's charge at the middle of its segment so much better than placing it at one end?
Treating a segment as a point charge at its middle is the midpoint rule for the integral. Its errors on the two halves of each segment nearly cancel, because the field's variation is roughly linear across a small segment: the near half pulls the answer one way and the far half the other. Putting the charge at an end has no such cancellation, so it leaves an error proportional to the segment length, . The midpoint version leaves only the curvature's effect, proportional to the length squared, .
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.