Skip to main content

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​

Activity: Two-point line

Implement Lagrange interpolation for n=2n=2 points (a straight line) and evaluate it at a given xx.

Input: x1 g1 x2 g2 x. Print the interpolated value with 6 decimals.

Write-ups​

0/4 solved
6.5b.W1

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”.

0 words/ 3 min

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

Next: 6.6 Cubic spline interpolation.