With irregular data we might well expect polynomial approximation to be very poor. Try instead now to use a cubic spline fit to the data. This uses nine different cubics, one between each adjacent pair of data points.
Add extra code to interpolate.m as follows.
First create a new figure using the MATLAB command
figure(2) .
Also add the MATLAB commands
clear figure(1) clfat the beginning of your interpolate.m file. The
clf
command clears the current figure.
Then calculate
yspline using
the MATLAB command
yspline=spline(x,y,xout) and plot
yspline against
xout .
Compare this with the original data by
using a plot command like
plot(x,y,'ro') to plot the data points
with red circles. Clearly the spline does a smooth job of interpolating
the data: however, we should be wary of accepting this result as correct
in all circumstances.