6.5b Lagrange interpolation labs
Source: Lab ideas from Computational Physics (Landau, Páez, Bordeianu), §6.5.1. Rewritten as original drills. Continues 6.5 Lagrange interpolation.
Write your own Lagrange evaluator — no calling a library interp routine. Stubs fail until filled in.
Formula refresh
Warm up: pick the matching equation among lookalikes (same walk as the theory lesson).
Labs
Implement Lagrange interpolation for points (a straight line) and evaluate it at a given .
Input: x1 g1 x2 g2 x. Print the interpolated value with 6 decimals.
Write-ups
Using “degree”, “n-1”, and “points”, explain:
With n tabulated points, what degree polynomial does Lagrange interpolation produce, and why?
Minimum 3 words. Your answer must include: “degree”, “n-1”, “points”.
Meet the writing requirements, then you can open the model answer.
Explorers
# lagrange_ref.py — n-point Lagrange interpolation (original sketch)
def lagrange(nodes, x):
n = len(nodes)
total = 0.0
for i in range(n):
xi, gi = nodes[i]
term = gi
for j in range(n):
if j == i:
continue
xj, _ = nodes[j]
term *= (x - xj) / (xi - xj)
total += term
return total