## MA/CS 371 - Lab 3 Polynomial Interpolation

### Section 1: Introduction

The goal of this lab is to introduce you to:
• 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.

### Section 2: Topics for Lecture

One important concept to know for this lab is MATLAB's representation of a polynomial. MATLAB represents a polynomial as a vector, where the first element is the coefficient of the highest power of x and the last element is the constant term. For example, the vector
```			[ 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   -2
```
which 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.

### Section 3: The Lagrange formula

The file interpoly.m is the skeleton of a program which will compute the interpolation polynomial to a specific function by the Lagrange method. The Lagrange method is discussed on page 134 of the book. Since MATLAB numbers vectors starting with 1 instead of with 0, I've changed the names for the Lagrange polynomials to match -- instead of l0 through l4, they are named l1 through l5.

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.

### Section 4: Extra Credit Problem

Write a MATLAB function lagrange.m which will take two vectors as input: a vector x of function points and a vector f of function values at those points, and compute the interpolating polynomial using the Lagrange method. p = lagrange(x,f) should set p to the interpolating polynomial. Note that length(p) = length(x)-1.

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.

### Section 5: Submitting Your Work

```		~cs371/submit lab3
```		~cs371/submit lab3extra