next up previous
Next: Cubic splines Up: tutorial10 Previous: Overview

Polynomial interpolation

The following table shows data $y_i$ obtained at locations $x_i$, $i=1,\dots,10$ during a biological experiment.

$x$ 0 0.2 0.4 0.6 0.72 0.75 1 1.2 1.3 1.35
$y$ 0.8 0.75 0.9 1.0 0.8 0.83 0.95 0.87 0.7 0.55

The problem is to estimate the value of $y$ at the location $x = 0.5$. In order to do this we shall first attempt to fit polynomials to the data.

\bgroup\color{red}\framebox{\em MAKE A NEW FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroupAs the first step create a file called interpolate.m.

\bgroup\color{red}\framebox{\em ADD CODE TO FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroupInclude code in this file to set up two row vectors, one called x , say, containing the locations \bgroup\color{black}$x_i$\egroup and the other ( y ) the data points \bgroup\color{black}$y_i$\egroup.

The inbuilt MATLAB routines that carry out polynomial interpolation are polyfit.m and polyval.m. Recall from lectures that if we have \bgroup\color{black}$n$\egroup data points then there is just one polynomial of degree \bgroup\color{black}$n-1$\egroup, i.e.

\begin{displaymath}\bgroup\color{black}
p_{n-1}(x) = c_0 +c_1 x +c_2 x^2 + \dots + c_{n-1} x^{n-1}
\egroup\end{displaymath}

that will pass exactly through those data points. The MATLAB command

c9 = polyfit(x,y,9) ;

will return the coefficients \bgroup\color{black}$c_i$\egroup of the ninth degree polynomial passing exactly through all ten data points. The coefficients are found in the vector c9. (Note that the coefficients are given in reverse order, e.g. the first element in c9 will be \bgroup\color{black}$c_9$\egroup.) The MATLAB command

yp9 = polyval(c9,xout) ;

evaluates \bgroup\color{black}$p_9(x)$\egroup at all \bgroup\color{black}$x$\egroup-points contained in the vector xout and returns a vector of the results in yp9.

\bgroup\color{red}\framebox{\em ADD CODE TO FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroup In interpolate.m set up a vector xout = min(x):0.01:max(x) and then include code to use the MATLAB routines above to calculate the approximating polynomial \bgroup\color{black}$p_9(x)$\egroup. Make sure to include some comments in your file explaining what the MATLAB commands are doing.

\bgroup\color{red}\framebox{\em ADD CODE TO FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroupInclude code to plot out the original data y as a function of x (use red circles) and yp9 as a function of xout . The commands xlabel , ylabel and title should be used to annotate the graph.

Check that the approximating polynomial does indeed pass through the data points? From the graph, do you think that it is it a useful approximation near \bgroup\color{black}$x=0.5$\egroup?

\bgroup\color{red}\framebox{\em ADD CODE TO FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroupCalculate the estimated value of \bgroup\color{black}$y(0.5)$\egroup.
Code like y05=polyval(c9,0.5) will do the job.

\bgroup\color{blue}\framebox{\em CHECKPOINT: submit solution}\egroup \bgroup\color{black}$\phantom{0}$\egroup \bgroup\color{red}\framebox{ 1.}\egroup Evaluate the interpolating polynomial at \bgroup\color{red}$x=0.5$\egroup.

As the ninth degree polynomial is clearly so poor, we can try to fit a lower degree polynomial instead. Thus take the two points nearest to \bgroup\color{red}$x=0.5$\egroup, and attempt a linear (first degree) polynomial fit:

c1 = polyfit(x(3:4),y(3:4),1)

Here x(3:4) e.g. means that we use only \bgroup\color{red}$x$\egroup-points 3 and 4, i.e. those bracketing \bgroup\color{red}$x=0.5$\egroup.

\bgroup\color{red}\framebox{\em ADD CODE TO FILE}\egroup \bgroup\color{black}$\phantom{0}$\egroupAdd extra code to your file interpolate.m to calculate \bgroup\color{black}$p_1(x)$\egroup and plot it on your existing graph (use another linestyle like plot(xout,yp1,'r - -') for example, that will give you a red dashed line and remember to use the switch hold on).

Clearly this is more useful than the ninth degree fit. You can now also try a quadratic fit (take the three nearest \bgroup\color{black}$x$\egroup-points) or a cubic fit (four nearest \bgroup\color{black}$x$\egroup-points). What seems to be the best compromise?

\bgroup\color{blue}\framebox{\em CHECKPOINT: submit solution}\egroup \bgroup\color{black}$\phantom{0}$\egroup \bgroup\color{red}\framebox{ 2.}\egroup Evaluate the interpolating quadratic polynomial at \bgroup\color{red}$x=0.5$\egroup.


next up previous
Next: Cubic splines Up: tutorial10 Previous: Overview
Charlie Macaskill 2004-07-26