Math 1070 Matlab Assignment 3
The purpose of this assignment is to compute an interpolating
polynomial for a given function f(x) at a set of points {x_i} or for a
given set of data points {(x_i,y_i)}.
Part 0: Getting started
To access the files needed for this assignment, in the class web page
http://www.pitt.edu/~trenchea/Math1070_Fall_Semester_2016.html
click on
A collection of matlab codes accompanying the text
and then on Chapter4
Copy the files chebyshev_interp.m, interp.m, and divdif.m into your
working directory.
The routine chebyshev_interp(n) creates an interpolant of order n to
the function fcn(x) on [-1,1]. The default is fcn(x) = exp(x). The
nodes are the zeros of the Chebyshev polynomial of the degree n+1 on
[-1,1]. The program gives two plots: first the true function and its
interpolant, and second, the error in the interpolation.
Run the program for different values of n. Then change fcn(x) to be
sin(x), cos(x), or any function of your choice. Notice that in
addition to the plots the program prints the max error in the
interpolation. You do not need to submit anything up to this point.
Note that the program uses two functions, divdif(x_nodes,y_values),
and interp(x_nodes,divdif_y,x_eval).
The function divdif takes an array x_nodes = (x_1,...,x_m) of nodes
and an array of corresponding function values y_values = (y_1,...y_m)
and returns an array of divided differences divdif_y(i) =
f[x_1,...,x_i], i=1,...,m.
The function interp(x_nodes,divdif_y,x_eval) calculates the Newton
divided difference form of the interpolation polynomial of degree m-1,
where the nodes are given in the vector x_nodes, m is the length of
x_nodes, and the divided differences are given in the array divdif_y.
The points at which the interpolation polynomial is to be evaluated
are given in x_eval; and on exit, p_eval contains the corresponding
values of the interpolation polynomial.
The two functions are also provided in separate M_files divdif.m and
interp.m. Here is an example of using these functions.
x = [0 1 2];
y = [2 1 3];
a = divdif(x,y)
At this point a is an array with 3 elements that stores the divided
differences. Now
z = [0.5 2.4 4];
yval = interp(x,a,z)
evaluates the interpolating polynomial at points 0.5, 2.4, and 4 and
stores the results in an array yval.
Part 1
Let p(x) be the polynomial of degree <= 5 that interpolates the points
(1,2), (2,3), (4,8), (5,6), (7,14), and (9,6). Using the functions
divdif and interp write a matlab script that
a) computes the divided difference coefficients
b) computes the values p(1.5), p(3.7), p(6.6), and p(9.5)
c) plots the graph of p(x) on [0,10] along with the 6 given points.
Your plot should look like the first plot created by chebyshev_interp,
without the function fcn(x). See how the plots are created in
chebyshev_interp.
Submit:
a) a listing of your code
b) the divided difference array
c) write down by hand the formula for p(x) (using the Newton divided
difference formula)
d) the computed values p(1.5), p(3.7), p(6.6), and p(9.5)
e) the plot with the graph of p(x)
Part 2
Modify the routine chebyshev_interp to interpolate the function
fcn(x) = 1/(1+25*x^2). Run the program with n = 1,2,4,8,16,32,64.
Record in a table the max error for all cases.
Copy chebyshev_interp.m into uniform_interp.m. Modify uniform_interp.m
to use uniform nodes x_j = -1 + j*2/n, j = 0,...,n. Run the program
with fcn(x) = 1/(1+25*x^2) and n = 1,2,4,8,16,32,64. Record in a table
the max error for all cases.
Submit the two tables and discuss the results.