Skip to main content

5.9 Integration reference sketches

Source: Concepts from Computational Physics (Landau, Páez, Bordeianu), §§5.3–5.8. Original modern Python stubs only — not textbook listings (TrapMethods.py, IntegGauss.py, vonNeuman.py). Graded work lives in the labs linked below.

Use these as memory aids while coding the exercises. They deliberately omit full tables, production edge cases, and copyrighted program text.

Trapezoid (reminder)​

Composite trapezoid on [a,b][a,b] with NN panels (h=(b−a)/Nh=(b-a)/N):

# original stub — not TrapMethods.py
def trap(f, a, b, N):
h = (b - a) / N
s = 0.5 * (f(a) + f(b))
for i in range(1, N):
s += f(a + i * h)
return s * h

Practice: 5.3b Integration labs.

Gauss map (reminder)​

Legendre nodes/weights live on [−1,1][-1,1]. Remap onto [a,b][a,b] (or [0,∞)[0,\infty), etc.) — pick the affine map among lookalikes (full walk also in 5.4):

# original stub — not IntegGauss.py
def gauss_map(nodes, weights, a, b):
mid = 0.5 * (a + b)
half = 0.5 * (b - a)
xs = [mid + half * y for y in nodes]
ws = [half * w for w in weights]
return xs, ws

Practice: 5.4b Gaussian quadrature labs.

Rejection sampler (reminder)​

Box height w0≥w(x)w_0\ge w(x); throw (x,W)=(U,w0V)(x,W)=(U,w_0 V); accept if W≤w(x)W\le w(x).

# original stub — not vonNeuman.py
def reject_sample(w, w0, a, b, n_want, rnd):
"""rnd() → U[0,1). Return n_want accepted abscissae ~ w on [a,b]."""
out = []
while len(out) < n_want:
x = a + (b - a) * rnd()
W = w0 * rnd()
if W <= w(x):
out.append(x)
return out

Practice: 5.8b Importance-sampling labs. Related: 5.7b Variance-reduction labs, 5.5b Monte Carlo integration labs.

Where to work next​

TopicLesson
Trap / Simpson / Romberg5.3 · 5.3b
Gauss–Legendre5.4 · 5.4b
Stone throw / mean value5.5 · 5.6
Control variates5.7 · 5.7b
Importance / rejection5.8 · 5.8b

Next: 6.1 Quantum bound states — Chapter 6 searching (then bisection labs, then §6.5 Lagrange).