Linear splines.
The set of the following four data points is given:
\begin{array}{rrrrr}x & 8 & 11 & 15 & 18 \\y & 5 & 9 & 10 & 8\end{array}(a) Determine the linear splines that fit the data.
(b) Determine the interpolated value for x=12.7.
(c) Write a MATLAB user-defined function for interpolation with linear splines. The inputs to the function are the coordinates of the given data points and the x coordinate of the point at which y is to be interpolated. The output from the function is the interpolated y value at the given point. Use the function for determining the interpolated value of y for x=12.7.
(a) There are four points and thus three splines. Using Eq. (6.65)
f_i(x)=\frac{\left(x-x_{i+1}\right)}{\left(x_i-x_{i+1}\right)} y_i+\frac{\left(x-x_i\right)}{\left(x_{i+1}-x_i\right)} y_{i+1} \text { for } i=1,2, \ldots, n-1 (6.65)
the equations of the splines are:
f_{1}(x)=\frac{\left(x-x_{2}\right)}{\left(x_{1}-x_{2}\right)} y_{1}+\frac{\left(x-x_{1}\right)}{\left(x_{2}-x_{1}\right)} y_{2}=\frac{(x-11)}{(8-11)}5+\frac{(x-8)}{(11-8)}9 =\frac{5}{-3}(x-11)+\frac{9}{2}(x-8) \quad for \quad 8 \leq x \leq 11
f_2(x)=\frac{\left(x-x_3\right)}{\left(x_2-x_3\right)} y_2+\frac{\left(x-x_2\right)}{\left(x_3-x_2\right)} y_3=\frac{(x-15)}{(11-15)} 9+\frac{(x-11)}{(15-11)} 10=\frac{9}{-4}(x-15)+\frac{10}{4}(x-11) \quad \text { for } \quad 11 \leq x \leq 15f_{3}(x)=\frac{\left(x-x_{4}\right)}{\left(x_{3}-x_{4}\right)} y_{3}+\frac{(x-x_{3} )}{(x_{4}-x_{3}) } y_{4}=\frac{(x-18)}{(15-18)}10+\frac{(x-15)}{(18-15)}8=\frac{10}{-3} (x-18)+\frac{8}{3}(x-15) \quad for \quad 15 \leq x \leq 18
(b) The interpolated value of y for x=12.7 is obtained by substituting the value of x in the equation for f_{2}(x) above:
f_{2}(12.7)=\frac{9}{-4}(12.7-15)+\frac{10}{4}(12.7-11)=9.425(c) The MATLAB user-defined function for linear spline interpolation is named Yint=LinearSpline (x, y, Xint ) \cdot x and y are vectors with the coordinates of the given data points, and Xint is the coordinate of the point at which y is to be interpolated.
Program 6-6: User-defined function. Linear splines.
function Yint = LinearSpline(x, y, Xint)
% LinearSpline calculates interpolation using linear splines.
% Input variables:
% x A vector with the coordinates x of the data points.
% y A vector with the coordinates y of the data points.
% Xint The x coordinate of the interpolated point.
% Output variable:
% Yint The y value of the interpolated point.
n = length(x);
for i = 2:n
if Xint < x(i)
break
end
end
Yint=(Xint-x(i))*y(i-1)/(x(i-1)-x(i))+(Xint-x(i-1))*y(i)/(x(i)-x(i-1));
The length of the vector x gives the number of terms in the data.
Find the interval that includes Xint.
Calculate Yint with Eq. (6.65).
The Linearspline (x, y, Xint) function is then used in the Command Window for calculating the interpolated value of x=12.7.
>> x = [8 11 15 18];
>> y = [5 9 10 8] ;
>> Yint = LinearSpline(x,y,12.7)
Yint =
9.4250