Question 8.3: Determine a cubic interpolation to tan(x) using the interpol...
Determine a cubic interpolation to tan(x) using the interpolation points x = 0, π/3, π/6, and π/4.
To do this example, we will use Program 8.1. We chose to use a vector x for the program that is not in sequential order to illustrate that the interpolation formulas are independent of the ordering of the interpolation points x_i (even though you will normally have the data in ascending or descending order). To generate the function evaluations, we can write a short program that takes in the interpolation points and returns the function at those points:
1 function f = getf(x)
2 n = length (x);
3 f = zeros(n,1);
4 for i = 1:n
5 f(i) = tan(x(i));
6 end
The corresponding values are f = [0, 1.7321, 0.5774, 1.0000].
We can then feed this vector f and the vector x into Program 8.1 to generate the b_i values. While the output of that program is our desired result, it is worthwhile to look at the vector U as the program progresses to see each of the divided differences and how the higher order ones are constructed from the lower order ones. When we reach line 13 of the program, we have
U=\begin{bmatrix} 1.6540 &0 &0 \\ 2.2053 &0 &0 \\1.6144 &0 &0 \end{bmatrix} (8.2.41)
The first column of U corresponds to the three first divided differences between the four points. When we execute the first iteration of the loop in lines 15–19, we have two new values in the second column corresponding to the second divided differences,
U=\begin{bmatrix} 1.6540 &1.0530 &0 \\ 2.2053 &2.2571 &0 \\1.6144 &0 &0 \end{bmatrix} (8.2.42)
There is one more iteration of the loop in lines 15–19 that computes the third divided difference for this problem, giving
U=\begin{bmatrix} 1.6540 &1.0530 &1.5332 \\ 2.2053 &2.2571 &0 \\1.6144 &0 &0 \end{bmatrix} (8.2.43)
The coefficients are thus the first line of this matrix along with the value of f(x_0), meaning
b_0 = 0 (8.2.44)
b_1 = 1.6540 (8.2.45)
b_2 = 1.0530 (8.2.46)
b_3 = 1.5332 (8.2.47)
Once we have the values of b_i, we can use Eq. (8.2.21) to write down the interpolation for this function, noting that x_0 = 0, x_1 = π/3, x_2 = π/6, and x_3 = π/4. The complete result is
f_n(x) = b_0 + b_1(x − x_0) + b_2(x − x_0)(x − x_1) +······+ b_n(x − x_0)(x − x_1) ··· (x − x_{n−1}) (8.2.21)
\tilde{f} _3(x) = 1.6540x + 1.0530x\left(x − \frac{π}{3} \right) + 1.5332x\left(x − \frac{π}{3} \right)\left(x − \frac{π}{6} \right) (8.2.48)
If you plug x = π/4 into this equation, you will get back \tilde{f} _3(x) = 1.0000, which must be the case since π/4 was one of the interpolation points.
Figure 8.4 shows that Eq. (8.2.48) is a good approximation to the tangent function over the range of the interpolation x ∈ [0, π/3]. However, we should be very wary about using interpolation formulas outside the range of the interpolation. For example, if we substitute π/2 into Eq. (8.2.48), we have \tilde{f} _3(x) = 4.7846. This is very different than the value of the tangent function at π/2, which diverges. Constructing accurate interpolation formulas near singular points in a function is numerically challenging, requiring very high-order interpolants.
