The set of the following five data points is given:
\begin{array}{llllll}x & 1 & 2 & 4 & 5 & 7 \\ y &52 &5 &-5 &-40& 10 \end{array}(a) Determine the fourth-order polynomial in the Lagrange form that passes through the points.
(b) Use the polynomial obtained in part (a) to determine the interpolated value for x=3.
(c) Develop a MATLAB user-defined function that interpolates using a Lagrange polynomial. The input to the function are the coordinates of the given data points and the x coordinate at the point at which the interpolated value of y is to be calculated. The output from the function is the interpolated value of y at x=3.
(a) Following the form of Eq. (6.44),
\begin{gathered} f(x)=\frac{\left(x-x_2\right)\left(x-x_3\right) \ldots\left(x-x_n\right)}{\left(x_1-x_2\right)\left(x_1-x_3\right) \ldots\left(x_1-x_n\right)} y_1+\frac{\left(x-x_1\right)\left(x-x_3\right) \ldots\left(x-x_n\right)}{\left(x_2-x_1\right)\left(x_2-x_3\right) \ldots\left(x_2-x_n\right)} y_2+ \\ \ldots+\frac{\left(x-x_1\right)\left(x-x_2\right) \ldots\left(x-x_{i-1}\right)\left(x-x_{i+1}\right) \ldots\left(x-x_n\right)}{\left(x_i-x_1\right)\left(x_i-x_2\right) \ldots\left(x_i-x_{i-1}\right)\left(x_i-x_{i+1}\right) \ldots\left(x_i-x_n\right)} y_i+\ldots+ \qquad& (6.44)\\ \frac{\left(x-x_1\right)\left(x-x_2\right) \ldots\left(x-x_{n-1}\right)}{\left(x_n-x_1\right)\left(x_n-x_2\right) \ldots\left(x_n-x_{n-1}\right)} y_n \end{gathered}
the Lagrange polynomial for the five given points is:
f(x)=\frac{(x-2)(x-4)(x-5)(x-7)}{(1-2)(1-4)(1-5)(1-7)} 52+\frac{(x-1)(x-4)(x-5)(x-7)}{(2-1)(2-4)(2-5)(2-7)} 5+\frac{(x-1)(x-2)(x-5)(x-7)}{(4-1)(4-2)(4-5)(4-7)}(-5)+\frac{(x-1)(x-2)(x-4)(x-7)}{(5-1)(5-2)(5-4)(5-7)}(-40)+\frac{(x-1)(x-2)(x-4)(x-5)}{(7-1)(7-2)(7-4)(7-5)} 10
(b) The interpolated value for x=3 is obtained by substituting the x in the polynomial:
f(3)=\frac{(3-2)(3-4)(3-5)(3-7)}{(1-2)(1-4)(1-5)(1-7)} 52+\frac{(3-1)(3-4)(3-5)(3-7)}{(2-1)(2-4)(2-5)(2-7)} 5+\frac{(3-1)(3-2)(3-5)(3-7)}{(4-1)(4-2)(4-5)(4-7)}(-5)+\frac{(3-1)(3-2)(3-4)(3-7)}{(5-1)(5-2)(5-4)(5-7)}(-40)+\frac{(3-1)(3-2)(3-4)(3-5)}{(7-1)(7-2)(7-4)(7-5)} 10
f(3)=-5.778+2.667-4.444+13.333+0.222=6(c) The MATLAB user-defined function for interpolation using Lagrange polynomials is named Yint=LagrangeINT (x, y, X int ) . x and y are vectors with the coordinates of the given data points, and X int is the coordinate of the point at which y is to be interpolated.
f(x)=\sum\limits_{i=1}^n y_i L_i(x)=\sum\limits_{i=1}^n y_i \prod\limits_{\substack{j=1 \\ j \neq i}}^n \frac{\left(x-x_j\right)}{\left(x_i-x_j\right)} (6.45)
L_{i}=\prod\limits_{\substack{j=1 \\ j \neq i}}^{n} \frac{\left(x-x_{j}\right)}{\left(x_{i}-x_{j}\right)} \text { where } x=\mathrm{Xint}
f(x)=\sum\limits_{i=1}^{n} y_{i} L_{i}
Program 6-4: User-defined function. Interpolation using a Lagrange polynomial.
function Yint = LagrangeINT(x,y,Xint)
% LagrangeINT fits a Lagrange polynomial to a set of given points and
% uses the polynomial to determine the interpolated value of a point.
% Input variables:
% x A vector with the x coordinates of the given points.
% y A vector with the y coordinates of the given points.
% Xint The x coordinate of the point at which y is to be interpolated.
% Output variable:
% Yint The interpolated value of Xint.
n = length (x) ;
for i = 1 :n
L(i) =1;
for j = 1:n
if j ~= i
L(i)= L(i)*(Xint-x(j))/(x(i)-x(j));
end
end
end
Yint = sum(y . *L);
The length of the vector \mathrm{x} gives the number of terms in the polynomial
Calculate the product terms L_{i}.
Calculate the value of the polynomial f(x)=\sum\limits_{i=1}^{n} y_{i} L_{i}.
The Lagrange (x, y, x int) function is then used in the Command Window for calculating the interpolated value of x=3.
>> x = [1 2 4 5 7];
>> y = [52 5 -5 -40 10] ;
>> Yinterpolated= LagrangeINT(x,y,3)
Yinterpolated =
6.0000