The following table shows data
obtained at locations
,
during a biological experiment.
| 0 | 0.2 | 0.4 | 0.6 | 0.72 | 0.75 | 1 | 1.2 | 1.3 | 1.35 | |
| 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
at the location
.
In order to do this we shall first attempt to fit polynomials to the
data.
As the first step create a file called interpolate.m.
Include code in this file to set up two row vectors,
one called
x , say, containing
the locations
and the other (
y ) the data points
.
The inbuilt MATLAB routines that carry out polynomial interpolation
are polyfit.m and polyval.m. Recall from lectures that if
we have
data points then there is just one polynomial of degree
,
i.e.
c9 = polyfit(x,y,9) ;
will return the coefficients
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
.)
The MATLAB command
yp9 = polyval(c9,xout) ;
evaluates
at all
-points contained in the vector xout and
returns a vector of the results in yp9.
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
.
Make sure to include some comments in your file explaining what
the MATLAB commands are doing.
Include 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
?
Calculate the estimated value of
.
Code like
y05=polyval(c9,0.5) will do the job.
Evaluate the interpolating polynomial at
.
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
,
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
-points 3 and 4, i.e. those
bracketing
.
Add extra code to your file interpolate.m
to calculate
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
-points) or
a cubic fit (four nearest
-points). What seems to be the best
compromise?
Evaluate the interpolating quadratic polynomial at
.