7.8 Code-listing sketches
Source: Structural ideas from Computational Physics (Landau, Páez, Bordeianu), §7.8 listings. Original sketches only — not a paste of
NewtonNDanimate.pyorHyperfine.py(those depend on VPython / SymPy GUIs outside this sandbox).
Multidimensional Newton for two masses on a string
Section 7.5 reduced the statics lesson to a 2D Newton toy. The full two-mass / three-string problem is a 9-unknown residual with components that encode:
- horizontal / vertical length closures with , ,
- force balance with tensions,
- three circle constraints .
A practical solver:
- Evaluate .
- Build the Jacobian by central differences in each coordinate (step ).
- Solve (Gaussian elimination /
linalg.solvewhen NumPy is available). - Update ; stop when and are tiny.
- Optionally redraw the geometry each iteration (any plotting toolkit — VPython is one historical choice).
Prefer when the sketch needs (see 7.5).
Jacobian by central differences
For residual , build by central differences (and check the zero-field hyperfine spectrum). Pick the column formula and the spectrum among lookalikes.
Use (or scaled ). Analytic is nicer when you have it; finite differences keep the physics code short and are fine for the string toy.
When is singular (or nearly so)
Newton needs a solvable . Failure modes (same spirit as 1D Newton traps in Fig. 6.3):
| Symptom | Likely cause | Mitigation |
|---|---|---|
linalg.solve / GE blows up | (redundant constraints, bad branch of ) | Restart from a better ; enforce |
| huge while barely drops | Ill-conditioned | Smaller or damping: with |
| Oscillating iterates | Bracket / geometry inconsistent | Check length closures before trusting the step |
If is singular, do not trust the update — fix the residual definition or the initial guess first.
# newton_nd_string_sketch.py — structure only (original)
def F(x, out):
# fill out[0]..out[8] with length, force, and sin²+cos² residuals
...
def jacobian(x, h=1e-4):
# central difference columns of J
...
def newton_nd(x, eps=1e-3, maxit=100):
for it in range(maxit):
f = F(x)
J = jacobian(x)
dx = solve_linear(J, [-fi for fi in f]) # your GE or library solve
x = [xi + d for xi, d in zip(x, dx)]
if max(abs(d) for d in dx) <= eps and max(abs(fi) for fi in f) <= eps:
return x, it
return x, maxit
Hyperfine matrix with a computer-algebra assist
Section 7.6 already builds
by hand. A symbolic package can print eigenvals() of and of when an external field couples . Numerically, the zero-field spectrum is (×3) and (×1); the gap is . With the levels split further — plot from the closed forms or from a dense eig sweep.
# hyperfine_sketch.py — structure only (original)
# V = [[W,0,0,0],[0,-W,2*W,0],[0,2*W,-W,0],[0,0,0,W]]
# print sorted eigenvalues for W=1 → -3, 1, 1, 1
# optionally add diagonal magnetic terms and re-diagonalize vs B
Takeaway
| Piece | Keep | Swap freely |
|---|---|---|
| String ND Newton | , central-diff , solve | VPython / any plotter |
| Hyperfine | , eigenvalues, | SymPy GUI vs NumPy eig |
| Singular | Detect / restart / damp | Fancy line-search libraries |
Listings in older books often glue residuals to a specific GUI. Graded path: 7.5b + 7.6b + 7.7b.
Matrix code sketches
1 / 3A practical ND Newton step for the string residuals is…
Pick an answer to see if you are right.
Next: 8.1 Nonlinear oscillators.