### 28.5 Polynomial Interpolation

Octave comes with good support for various kinds of interpolation, most of which are described in Interpolation. One simple alternative to the functions described in the aforementioned chapter, is to fit a single polynomial to some given data points. To avoid a highly fluctuating polynomial, one most often wants to fit a low-order polynomial to data. This usually means that it is necessary to fit the polynomial in a least-squares sense, which just is what the `polyfit` function does.

— Function File: p = polyfit (x, y, n)
— Function File: [p, s] = polyfit (x, y, n)
— Function File: [p, s, mu] = polyfit (x, y, n)

> Return the coefficients of a polynomial p(x) of degree n that minimizes the least-squares-error of the fit to the points `[`x`,` y`]`.

> The polynomial coefficients are returned in a row vector.

> The optional output s is a structure containing the following fields:

> ‘`R`’
>   Triangular factor R from the QR decomposition.
> ‘`X`’
>   The Vandermonde matrix used to compute the polynomial coefficients.
> ‘`df`’
>   The degrees of freedom.
> ‘`normr`’
>   The norm of the residuals.
> ‘`yf`’
>   The values of the polynomial for each value of x.

> The second output may be used by `polyval` to calculate the statistical error limits of the predicted values.

> When the third output, mu, is present the coefficients, p, are associated with a polynomial in xhat = (x-mu(1))/mu(2). Where mu(1) = mean (x), and mu(2) = std (x). This linear transformation of x improves the numerical stability of the fit.

> See also: polyval, polyaffine, roots, vander, zscore.

In situations where a single polynomial isn't good enough, a solution is to use several polynomials pieced together. The function `mkpp` creates a piecewise polynomial, `ppval` evaluates the function created by `mkpp`, and `unmkpp` returns detailed information about the function.

The following example shows how to combine two linear functions and a quadratic into one function. Each of these functions is expressed on adjoined intervals.

         x = [-2, -1, 1, 2];
         p = [ 0,  1, 0;
               1, -2, 1;
               0, -1, 1 ];
         pp = mkpp(x, p);
         xi = linspace(-2, 2, 50);
         yi = ppval(pp, xi);
         plot(xi, yi);

— Function File: pp = mkpp (breaks, coefs)
— Function File: pp = mkpp (breaks, coefs, d)

> Construct a piecewise polynomial (pp) structure from sample points breaks and coefficients coefs. breaks must be a vector of strictly increasing values. The number of intervals is given by ni`= length (`breaks`) - 1`. When m is the polynomial order coefs must be of size: ni x m + 1.

> The i-th row of coefs, coefs`(`i`,:)`, contains the coefficients for the polynomial over the i-th interval, ordered from highest (m) to lowest (0).

> coefs may also be a multi-dimensional array, specifying a vector-valued or array-valued polynomial. In that case the polynomial order is defined by the length of the last dimension of coefs. The size of first dimension(s) are given by the scalar or vector d. If d is not given it is set to `1`. In any case coefs is reshaped to a 2-D matrix of size `[`ni`*prod(`d m`)]`

> See also: unmkpp, ppval, spline, pchip, ppder, ppint, ppjumps.

— Function File: [x, p, n, k, d] = unmkpp (pp)

> Extract the components of a piecewise polynomial structure pp. The components are:

> x
>   Sample points.
> p
>   Polynomial coefficients for points in sample interval. p`(`i`, :)` contains the coefficients for the polynomial over interval i ordered from highest to lowest. If d`> 1`, p`(`r`,` i`, :)` contains the coefficients for the r-th polynomial defined on interval i.
> n
>   Number of polynomial pieces.
> k
>   Order of the polynomial plus 1.
> d
>   Number of polynomials defined for each interval.

> See also: mkpp, ppval, spline, pchip.

— Function File: yi = ppval (pp, xi)

> Evaluate the piecewise polynomial structure pp at the points xi. If pp describes a scalar polynomial function, the result is an array of the same shape as xi. Otherwise, the size of the result is `[pp.dim, length(`xi`)]` if xi is a vector, or `[pp.dim, size(`xi`)]` if it is a multi-dimensional array.

> See also: mkpp, unmkpp, spline, pchip.

— Function File: ppd = ppder (pp)
— Function File: ppd = ppder (pp, m)

> Compute the piecewise m-th derivative of a piecewise polynomial struct pp. If m is omitted the first derivative is calculated.

> See also: mkpp, ppval, ppint.

— Function File: ppi = ppint (pp)
— Function File: ppi = ppint (pp, c)

> Compute the integral of the piecewise polynomial struct pp. c, if given, is the constant of integration.

> See also: mkpp, ppval, ppder.

— Function File: jumps = ppjumps (pp)

> Evaluate the boundary jumps of a piecewise polynomial. If there are n intervals, and the dimensionality of pp is d, the resulting array has dimensions `[d, n-1]`.

> See also: mkpp.