Starting from:
$35

$29

Homework 6 Solution

    1. Write a Matlab code which will  nd the linear spline function of the given sample points.

    2. Write a Matlab code which will  nd the natural cubic spline function of the given sample points.

    3. We are now going to compare interpolation polynomials and spline functions. Consider the Runge
function f(x) =
1
on the interval [  5; 5] using 11 equally spaced points.

1 + x2



        (a) Use either your LagrangeInt or NewtonInt de ned in homework 5 to nd the degree 12 polyno-mial through these points. Plot f and this polynomial on the same graph.

        (b) Use your code in problem 1 to nd the linear spline function of f through the sample points. Plot f and its linear spline function on the same graph.

        (c) Use your code in problem 2 to nd the cubic spline function of f through the sample points. Plot f and its cubic spline function on the same graph.

        (d) Based on your results, discuss the di erence between the interpolation polynomial and the spline functions.

    4. Computer libraries often use tables of function values together with piecewise linear interpolation to evaluate elementary functions such as sin(x), because table lookup and interpolation can be faster than using a Taylor series expansion. Write a Matlab le to accomplish the following.

        (a) Create a vector ~x of 1000 uniformly-spaced values between 0 and . Then, create a vector ~y with the values of the sine function at each of these points. This will serve as your lookup table.

        (b) For a random value r, estimate sin(r) as follows:
Find the two consecutive x entries, xi  and xi+1 which satisfy

xi r xi+1. Having identi ed the subinterval containing r, use linear interpolation with sample points xi and xi+1 to estimate sin(r).

        (c) Pick 100 random r value and evaluate sin(r) using the method in (b). Compare your results with the accurate value obtained by typing sin(r). Find the maximum absolute error and maximum relative error.

        (d) Repeat (c), but this time start from a look up table with 2000 points. How does this change the relative error?

    5. Show that s(x) is a natural cubic spline through the points (0; 1); (1; 1); (2; 0); and (3; 10).
s(x) =
81  2(x  1)  3(x  1)2 + 4(x  1)3
;  if 1
x < 2

>
1 + x  x3;


if 0

x < 1


4(x  2) + 9(x
2
3
if 2

x

3

<

2)
3(x  2) ;






>









:








    6. Spline function can use polynomial with di erent degrees on di erent pieces. You can de ne spline function in any form as long as the number of variables/equations match.

(a) Find
(
P (x) =    P1(x);  if 0   x   1
P2(x);    if 1    x    2

such that

1

    i. P1(x) is linear,
    ii. P2(x) is quadratic,

    iii. P (x) and P 0 (x) are continuous at x = 1,
iv. P (0) = 1, P (1) =    1, and P (2) = 0.

        (b) Graph this function P (x).

        (c) If you want to de ne a spline function in (a) with P1(x) quadratic and P2(x) cubic, how many equations do you need? List them (pick the ones you think are reasonable).

    7. Another popular cubic spline function is called the Cubic Hermite Spline. Google this concept and complete the following

        (a) Summarize the idea of Cubic Hermite Spline.

        (b) Compare Cubic Hermite Spline and Natural Cubic Spline in accuracy, e ciency. Figure out when to use which. (You can code both and compare, or you can nd this information online. )






















































2

More products