Quiz: polynomial interpolation
Short questions to test your understanding of polynomial interpolation. Try each on paper first, then open the answer.
Q1. You have OD readings at four distinct times. How many polynomials of degree at most 3 pass through all four? How many of degree at most 4?
Count the coefficients and the conditions. What if two different cubics both passed through the four points?
Exactly one of degree \(\le 3\): four coefficients, four conditions, and the Vandermonde matrix is invertible when the times are distinct. (If two cubics agreed at four points, their difference would be a cubic with four roots, so it is zero.) Of degree \(\le 4\) there are infinitely many: add \(c\,(t - t_0)(t - t_1)(t - t_2)(t - t_3)\) with any \(c\). Interpolation needs the degree tied to the number of points.
Q2. What is \(L_2(t_2)\), and what is \(L_2(t_0)\)? Why must \(\sum_i L_i(t) = 1\) for every \(t\)?
Interpolate the constant readings \(y_i = 1\).
\(L_2(t_2) = 1\) and \(L_2(t_0) = 0\): each basis polynomial is 1 at its own node and 0 at all the others. If all readings are 1, the interpolant is \(\sum_i 1\cdot L_i(t)\); but the constant polynomial 1 also passes through the readings, and the interpolant is unique, so \(\sum_i L_i(t) = 1\). It is a useful check on hand calculations.
Q3. A plate reader adds one reading every hour, and you keep an interpolating polynomial up to date. Why is the Newton form convenient here, and what does adding a reading cost?
What does the new term look like at the old readings?
The new reading adds one term \(a_{n+1}(t - t_0)\cdots(t - t_n)\), which is zero at all old readings, so the old coefficients stay. Computing the new coefficient means one new diagonal of the divided-difference table: \(O(n)\) operations. In Lagrange form every basis polynomial changes and you start again. (The barycentric weights can also be updated in \(O(n)\).)
Q4. Readings \((t, y) = (1, 2), (3, 8), (4, 14)\). Find the Newton form of the interpolating parabola and evaluate it at \(t = 2\).
\(f[t_0, t_1] = (y_1 - y_0)/(t_1 - t_0)\), then the same one level up, dividing by \(t_2 - t_0\).
\(f[1, 3] = (8 - 2)/2 = 3\), \(f[3, 4] = (14 - 8)/1 = 6\), \(f[1, 3, 4] = (6 - 3)/(4 - 1) = 1\). So \(p(t) = 2 + 3(t - 1) + 1\cdot(t - 1)(t - 3)\), and \(p(2) = 2 + 3 - 1 = 4\). (Check: \(p(4) = 2 + 9 + 3 = 14\).)
Q5. You interpolate an OD curve linearly between readings. The largest second derivative of the curve is \(0.074\) OD/h². Bound the error for readings every 2 h, and for readings every 30 min.
\(|f - p| \le \tfrac{h^2}{8}\max|f''|\).
\(h = 2\): \(\tfrac{4}{8}\cdot 0.074 = 0.037\) OD. \(h = 0.5\): \(\tfrac{0.25}{8}\cdot 0.074 = 0.0023\) OD. Four times as many readings, sixteen times smaller bound: the error is \(O(h^2)\).
Q6. In the error formula \(f(t) - p(t) = \dfrac{f^{(n+1)}(\xi)}{(n+1)!}\prod_i (t - t_i)\), which part depends on the biology and which on the experimental design? Where in the interval is the second part largest for equally spaced readings?
Which factor changes if you move the sampling times but not the culture?
The derivative \(f^{(n+1)}\) is a property of the curve (the biology); the node polynomial \(w(t) = \prod (t - t_i)\) depends only on the sampling times (the design). For equally spaced readings \(|w|\) is largest in the first and last intervals, by orders of magnitude for many readings (about 300 times the middle value for 13 readings), so the error concentrates near the ends.
Q7. A 48-h growth run is read every 4 h, and the degree-12 interpolant dips to OD \(= -0.15\) near the start. A colleague suggests reading every 2 h instead and using degree 24. Will it help? What would help?
Look at the red curve on the “More readings make it worse” slide.
Not reliably. With equally spaced readings over this interval the error does not converge as the degree grows (Runge phenomenon): it jumps around, 0.34 at degree 20, 0.04 at degree 24 (by luck), 0.43 at degree 28 and 0.58 at degree 40. You cannot know in advance whether your degree is a lucky one. What helps: Chebyshev sampling times (crowded at the start and the end), which make the error fall steadily; or, with the readings you have, low-degree local interpolation: piecewise linear (approx) or splines.
Q8. The five readings at \(t = 0, 2, 4, 6, 8\) h from the OD curve are interpolated with a degree-4 polynomial. What do you expect at 10, 12 and 24 h? Run the code.
K <- 1.2; N0 <- 0.05; r <- 0.8
od <- function(t) K / (1 + (K / N0 - 1) * exp(-r * t))
t <- c(0, 2, 4, 6, 8)
fit <- lm(od(t) ~ poly(t, 4))
predict(fit, data.frame(t = c(10, 12, 24)))
od(c(10, 12, 24))
approx(t, od(t), xout = 10)$yimport numpy as np
from numpy.polynomial import Polynomial
np.set_printoptions(precision=7)
K, N0, r = 1.2, 0.05, 0.8
def od(t):
return K / (1 + (K / N0 - 1) * np.exp(-r * t))
t = np.array([0.0, 2, 4, 6, 8])
p = Polynomial.fit(t, od(t), deg=4)
print(p(np.array([10.0, 12, 24])))
print(od(np.array([10.0, 12, 24])))
print(np.interp(10.0, t, od(t)))// rust/examples/polynomial_interpolation_q8.rs — cargo run --example polynomial_interpolation_q8
// Quiz: extrapolating the degree-4 interpolant beyond the last reading.
const K: f64 = 1.2;
const N0: f64 = 0.05;
const R: f64 = 0.8;
fn od(t: f64) -> f64 {
K / (1.0 + (K / N0 - 1.0) * (-R * t).exp())
}
fn lagrange(x: &[f64], y: &[f64], z: f64) -> f64 {
(0..x.len())
.map(|i| {
let li: f64 = (0..x.len()).filter(|&j| j != i)
.map(|j| (z - x[j]) / (x[i] - x[j])).product();
li * y[i]
})
.sum()
}
fn main() {
let t = [0.0, 2.0, 4.0, 6.0, 8.0];
let y: Vec<f64> = t.iter().map(|&ti| od(ti)).collect();
let p: Vec<f64> = [10.0, 12.0, 24.0].iter().map(|&z| lagrange(&t, &y, z)).collect();
let truth: Vec<f64> = [10.0, 12.0, 24.0].iter().map(|&z| od(z)).collect();
println!("{:?}", p);
println!("{:?}", truth);
}
// Output:
// [0.8693450015617135, -0.0055580390951419645, -21.845438200201897]
// [1.1908121218342402, 1.1981335942867695, 1.199999873393797]Outside the readings every factor of \(w(t) = t(t-2)(t-4)(t-6)(t-8)\) grows.
R prints
1 2 3
0.869345002 -0.005558039 -21.845438200
[1] 1.190812 1.198134 1.200000
[1] NA
(Python and Rust print the same numbers in their own format.) The truth levels off at 1.2, but the polynomial turns down: 0.87 at 10 h, slightly negative at 12 h and \(-21.8\) at 24 h. Never extrapolate an interpolating polynomial. Note also the last line: approx returns NA outside the readings (unless you set rule = 2), whereas np.interp silently returns the end value, 1.156. Know what your tool does at the edges.
Q9. You interpolate 13 noisy hourly OD readings (noise sd 0.02) with a degree-12 polynomial. All residuals are exactly zero. Is that good news?
What should the residuals of a good model look like, if the readings are noisy?
No. A model whose residuals are smaller than the known measurement noise is fitting the noise. Each reading’s error is multiplied by its basis polynomial \(L_i(t)\), which for 13 equispaced points reaches 18 near the ends (the Lebesgue constant is 89), and in the exercise the interpolant ends up 0.39 OD off. Fit a lower degree by least squares instead, and choose the lowest degree whose residual spread matches the noise (degree 4–5 here, error about 0.03).
Q10. Why do the slides evaluate the interpolant with the barycentric formula rather than by solving \(V\mathbf c = \mathbf y\) for the monomial coefficients? Give two reasons.
Think of the size of \(48^{12}\), and of the cost per evaluation point.
- Conditioning: the Vandermonde matrix is badly conditioned (\(\kappa \approx 8\times10^{20}\) for 13 readings in hours; even rescaled to \([-1, 1]\) it reaches \(10^{15}\) at degree 40), so the coefficients lose their digits; R’s
solverefuses, numpy silently returns them. The barycentric formula never forms coefficients and stays accurate to near machine precision at Chebyshev nodes. (2) Cost and convenience: after \(O(n^2)\) work for the weights, each evaluation is \(O(n)\), a new reading updates the weights in \(O(n)\), and a common scale factor of the weights cancels, so no overflow.
Q11. A colleague has a gene’s expression at 0, 0.5, 1, 2, 4, 8, 12 and 24 h and draws a polynomial “trendline” of order 7 through it to read off the value at 18 h. What do you tell them, and which quick check would have shown the problem?
Where are the sampling times dense, and where is the biggest gap?
The sampling is dense early and very sparse late, so the node polynomial is enormous in the 12–24 h gap; in the exercise (C7) the degree-7 interpolant of a smooth induction curve swings to about \(-2000\) there and predicts \(-1120\) at 18 h. Use piecewise linear interpolation or a spline instead, or better, measure at 18 h. The check: leave one time point out (say 12 h), interpolate from the rest and compare with the measured value; the polynomial misses by about 300, the linear interpolant by 0.3. Also simply plot the curve on a fine grid.