Polynomial Interpolation

- MATLAB's representation of a polynomial and the
**polyval**function - Implementing the Lagrange interpolation polynomial algorithm

If you have not already done so, create a directory for this lab and copy the lab 3 files into it. If you start off in your home directory you can do the following:

mkdir ~/cs371/lab3 cd ~/cs371/lab3 cp ~cs371/lab3/* .and (as always) don't forget to type that last period.

[ 4 9 -1 3 ]represents 4x^3 + 9x^2 - x + 3.

The **polyval** function allows you to evaluate such a polynomial
at a particular value of x. **polyval** takes two arguments: the
first is the polynomial, and the second is the value(s) of x at which
you wish to evaluate it. So for example the sequence

>> p = [ 1 -1 2 ]; >> polyval(p,2)returns the value 4, because x^2 - x + 2 evaluated at x=2 is 4. If you want to be clever and give a vector of values as the second parameter, that's OK, and you'll get a vector which contains the function evaluated at each of those points:

>> polyval(p,[0 1 2])or

>> q = [0 1 2]; >> polyval(p,q)will return the vector [ 2 2 4 ] because the value of the polynomial is 2 at x=0 and x=1 and 4 at x=2.

Finally, the **conv** function can be used to multiply two
polynomials together. **conv** takes its name from the
"convolution" of two vectors. It just so happens that if you
interpret the two vectors as polynomials, their convolution is the
product of the polynomials. So for now all you need to remember is
that **conv** takes two polynomials as input and returns their
product:

>> p = [1 -1 2]; >> q = [1 -1]; >> conv(p,q)Since p represents x^2 - x + 2 and q represents x - 1, we expect the answer to be (x^2 - x + 2)(x - 1), and sure enough out comes

ans = 1 -2 3 -2which is x^3 - 2x^2 + 3x - 2.

That's all I wanted to introduce today so I won't bother lecturing
"live" in lab. If there are any questions about MATLAB's polynomial
representation or the **polyval** and **conv** functions, I'll
be happy to answer them individually or in front of the class.

Oh, one more thing. MATLAB has a **polyfit** function which some
of you may know about. The polynomial that **polyfit** returns is
*NOT* going to be the same as the interpolating polynomial.
**polyfit** returns the best *least squares* fit, which is a
different beast. So don't try to use **polyfit** to check your
answers because it will be wrong.

**This week's assignment is to finish the program. There are no
extra questions. If your program shows an interpolation error of more
than about 0.1 then there's a bug in it.**

Use the same overall structure as **interpoly.m**, but instead of
hard-coding the number of terms in each Lagrange polynomial, build
them up with a loop. Also don't include the graphs in
**lagrange.m**, although it's probably a good idea to do some
graphs to be sure your function is correct.

~cs371/submit lab3I will accept solutions through 11:59pm Thursday. If you finish the Extra Credit problem, leave the file

~cs371/submit lab3extraand I will accept these solutions through 11:59pm next Wednesday.