3. For any function Æ’ and n + 1 distinct nodes $x_0, \dots, x_n$, Lagrange interpolation factors $f = P + R$ where
$P: x \mapsto \sum_{i=0}^n f(x_i) \prod_{\substack{j=0 \ j \neq i}}^n \frac{x - x_j}{x_i - x_j}$
and R is the approximation error. The following Matlab function,
function a = linterp_poly(X, Y)
a = zeros(1, numel(X));
for i = 1:numel(X)
aa = poly(X([1:i-1 i+1:end]));
a = a + Y(i) * aa / polyval(aa, X(i));
end
end
represents $P(x) = \sum_{k=0}^n a_k x^k$ as its coefficients a = [$a_n, a_{n-1}, \dots, a_1, a_0$] (this solves Worksheet 7 Q2.)
(a) Write a Matlab function that uses P to approximate $f^{(d)}(x)$ for any positive integer d and real number x.
The function signature should be
function y = num_diff(X, Y, x, d)
and should invoke both linterp_poly (see above) and Matlab's polyder. Test your code with by con-
structing a problem where you expect the answer to be exact (up to rounding errors). What happens when
d equals or exceeds the number of given interpolation nodes (n + 1)?