MATLAB Interpolating Functions
Now that you've explored the theory behind piecewise approximation, you'll be able to use the following MATLAB commands relevant to the subject of this tutorial to produce a thorough higher-order polynomial and piecewise lower-order interpolations:
p=polyfit(xdata,ydata,N) – finds the coefficients of an Nth-order polynomial p(x) passing through all data points (xdata(i),ydata(i)), i = 1,2,…,N + 1;
y=polyval(p,x) – returns the interpolation vector y evaluated at x using a polynomial p (a vector of polynomial coefficients found with the polyfit function);
y=spline(xdata,ydata,x) – computes a cubic-spline interpolation with the not-a-knot endpoint conditions. If ydata contains two more values compared to the vector xdata, then the first and last elements in ydata are used as endslopes for the clamped cubic spline;
y=pchip(xdata,ydata,x) – computes a piecewise shape-preserving cubic Hermite interpolating polynomial;
y=interp1(xdata,ydata,x,'method') – interpolates a function of one variable using one of the following methods: 'linear' (the default 'method') – for piecewise linear interpolation, 'nearest' – for a step-type nearest-neighbor interpolation, 'spline' – for a natural cubic spline interpolation, and 'pchip' or 'cubic' – for a shape-preserving cubic Hermite interpolation;
MATLAB also allows interpolation of functions that depend on more than one variable. The following three commands are used for the function of two (w = f(x,y)), three (w = f(x,y,z)) and n (w = f(x1,x2,…,xn)) variables, respectively:
interp2(xdata,ydata,wdata,x,y,'method')
interp3(xdata,ydata,zdata,wdata,x,y,z,'method')
interpn(x1data,x2data,...,xndata,wdata,x1,x2,...,xn,'method')
Instructor Note: This is the end of this tutorial. I hope you enjoyed it and that you now have a basic understanding of piecewise interpolation using lower-order polynomials. I also hope that you understand the difference between global and local splines.
Back to top.