<html lang="en"> <head> <title>Polynomial Interpolation - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Polynomial-Manipulations.html#Polynomial-Manipulations" title="Polynomial Manipulations"> <link rel="prev" href="Derivatives-_002f-Integrals-_002f-Transforms.html#Derivatives-_002f-Integrals-_002f-Transforms" title="Derivatives / Integrals / Transforms"> <link rel="next" href="Miscellaneous-Functions.html#Miscellaneous-Functions" title="Miscellaneous Functions"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Polynomial-Interpolation"></a> <p> Next: <a rel="next" accesskey="n" href="Miscellaneous-Functions.html#Miscellaneous-Functions">Miscellaneous Functions</a>, Previous: <a rel="previous" accesskey="p" href="Derivatives-_002f-Integrals-_002f-Transforms.html#Derivatives-_002f-Integrals-_002f-Transforms">Derivatives / Integrals / Transforms</a>, Up: <a rel="up" accesskey="u" href="Polynomial-Manipulations.html#Polynomial-Manipulations">Polynomial Manipulations</a> <hr> </div> <h3 class="section">28.5 Polynomial Interpolation</h3> <p>Octave comes with good support for various kinds of interpolation, most of which are described in <a href="Interpolation.html#Interpolation">Interpolation</a>. 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 <code>polyfit</code> function does. <!-- polyfit scripts/polynomial/polyfit.m --> <p><a name="doc_002dpolyfit"></a> <div class="defun"> — Function File: <var>p</var> = <b>polyfit</b> (<var>x, y, n</var>)<var><a name="index-polyfit-2729"></a></var><br> — Function File: [<var>p</var>, <var>s</var>] = <b>polyfit</b> (<var>x, y, n</var>)<var><a name="index-polyfit-2730"></a></var><br> — Function File: [<var>p</var>, <var>s</var>, <var>mu</var>] = <b>polyfit</b> (<var>x, y, n</var>)<var><a name="index-polyfit-2731"></a></var><br> <blockquote><p>Return the coefficients of a polynomial <var>p</var>(<var>x</var>) of degree <var>n</var> that minimizes the least-squares-error of the fit to the points <code>[</code><var>x</var><code>, </code><var>y</var><code>]</code>. <p>The polynomial coefficients are returned in a row vector. <p>The optional output <var>s</var> is a structure containing the following fields: <dl> <dt>‘<samp><span class="samp">R</span></samp>’<dd>Triangular factor R from the QR decomposition. <br><dt>‘<samp><span class="samp">X</span></samp>’<dd>The Vandermonde matrix used to compute the polynomial coefficients. <br><dt>‘<samp><span class="samp">df</span></samp>’<dd>The degrees of freedom. <br><dt>‘<samp><span class="samp">normr</span></samp>’<dd>The norm of the residuals. <br><dt>‘<samp><span class="samp">yf</span></samp>’<dd>The values of the polynomial for each value of <var>x</var>. </dl> <p>The second output may be used by <code>polyval</code> to calculate the statistical error limits of the predicted values. <p>When the third output, <var>mu</var>, is present the coefficients, <var>p</var>, are associated with a polynomial in <var>xhat</var> = (<var>x</var>-<var>mu</var>(1))/<var>mu</var>(2). Where <var>mu</var>(1) = mean (<var>x</var>), and <var>mu</var>(2) = std (<var>x</var>). This linear transformation of <var>x</var> improves the numerical stability of the fit. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dpolyval.html#doc_002dpolyval">polyval</a>, <a href="doc_002dpolyaffine.html#doc_002dpolyaffine">polyaffine</a>, <a href="doc_002droots.html#doc_002droots">roots</a>, <a href="doc_002dvander.html#doc_002dvander">vander</a>, <a href="doc_002dzscore.html#doc_002dzscore">zscore</a>. </p></blockquote></div> <p>In situations where a single polynomial isn't good enough, a solution is to use several polynomials pieced together. The function <code>mkpp</code> creates a piecewise polynomial, <code>ppval</code> evaluates the function created by <code>mkpp</code>, and <code>unmkpp</code> returns detailed information about the function. <p>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. <pre class="example"> 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); </pre> <!-- mkpp scripts/polynomial/mkpp.m --> <p><a name="doc_002dmkpp"></a> <div class="defun"> — Function File: <var>pp</var> = <b>mkpp</b> (<var>breaks, coefs</var>)<var><a name="index-mkpp-2732"></a></var><br> — Function File: <var>pp</var> = <b>mkpp</b> (<var>breaks, coefs, d</var>)<var><a name="index-mkpp-2733"></a></var><br> <blockquote> <p>Construct a piecewise polynomial (pp) structure from sample points <var>breaks</var> and coefficients <var>coefs</var>. <var>breaks</var> must be a vector of strictly increasing values. The number of intervals is given by <var>ni</var><code> = length (</code><var>breaks</var><code>) - 1</code>. When <var>m</var> is the polynomial order <var>coefs</var> must be of size: <var>ni</var> x <var>m</var> + 1. <p>The i-th row of <var>coefs</var>, <var>coefs</var><code> (</code><var>i</var><code>,:)</code>, contains the coefficients for the polynomial over the <var>i</var>-th interval, ordered from highest (<var>m</var>) to lowest (<var>0</var>). <p><var>coefs</var> 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 <var>coefs</var>. The size of first dimension(s) are given by the scalar or vector <var>d</var>. If <var>d</var> is not given it is set to <code>1</code>. In any case <var>coefs</var> is reshaped to a 2-D matrix of size <code>[</code><var>ni</var><code>*prod(</code><var>d</var> <var>m</var><code>)] </code> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dunmkpp.html#doc_002dunmkpp">unmkpp</a>, <a href="doc_002dppval.html#doc_002dppval">ppval</a>, <a href="doc_002dspline.html#doc_002dspline">spline</a>, <a href="doc_002dpchip.html#doc_002dpchip">pchip</a>, <a href="doc_002dppder.html#doc_002dppder">ppder</a>, <a href="doc_002dppint.html#doc_002dppint">ppint</a>, <a href="doc_002dppjumps.html#doc_002dppjumps">ppjumps</a>. </p></blockquote></div> <!-- unmkpp scripts/polynomial/unmkpp.m --> <p><a name="doc_002dunmkpp"></a> <div class="defun"> — Function File: [<var>x</var>, <var>p</var>, <var>n</var>, <var>k</var>, <var>d</var>] = <b>unmkpp</b> (<var>pp</var>)<var><a name="index-unmkpp-2734"></a></var><br> <blockquote> <p>Extract the components of a piecewise polynomial structure <var>pp</var>. The components are: <dl> <dt><var>x</var><dd>Sample points. <br><dt><var>p</var><dd>Polynomial coefficients for points in sample interval. <var>p</var><code> (</code><var>i</var><code>, :)</code> contains the coefficients for the polynomial over interval <var>i</var> ordered from highest to lowest. If <var>d</var><code> > 1</code>, <var>p</var><code> (</code><var>r</var><code>, </code><var>i</var><code>, :)</code> contains the coefficients for the r-th polynomial defined on interval <var>i</var>. <br><dt><var>n</var><dd>Number of polynomial pieces. <br><dt><var>k</var><dd>Order of the polynomial plus 1. <br><dt><var>d</var><dd>Number of polynomials defined for each interval. </dl> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmkpp.html#doc_002dmkpp">mkpp</a>, <a href="doc_002dppval.html#doc_002dppval">ppval</a>, <a href="doc_002dspline.html#doc_002dspline">spline</a>, <a href="doc_002dpchip.html#doc_002dpchip">pchip</a>. </p></blockquote></div> <!-- ppval scripts/polynomial/ppval.m --> <p><a name="doc_002dppval"></a> <div class="defun"> — Function File: <var>yi</var> = <b>ppval</b> (<var>pp, xi</var>)<var><a name="index-ppval-2735"></a></var><br> <blockquote><p>Evaluate the piecewise polynomial structure <var>pp</var> at the points <var>xi</var>. If <var>pp</var> describes a scalar polynomial function, the result is an array of the same shape as <var>xi</var>. Otherwise, the size of the result is <code>[pp.dim, length(</code><var>xi</var><code>)]</code> if <var>xi</var> is a vector, or <code>[pp.dim, size(</code><var>xi</var><code>)]</code> if it is a multi-dimensional array. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmkpp.html#doc_002dmkpp">mkpp</a>, <a href="doc_002dunmkpp.html#doc_002dunmkpp">unmkpp</a>, <a href="doc_002dspline.html#doc_002dspline">spline</a>, <a href="doc_002dpchip.html#doc_002dpchip">pchip</a>. </p></blockquote></div> <!-- ppder scripts/polynomial/ppder.m --> <p><a name="doc_002dppder"></a> <div class="defun"> — Function File: ppd = <b>ppder</b> (<var>pp</var>)<var><a name="index-ppder-2736"></a></var><br> — Function File: ppd = <b>ppder</b> (<var>pp, m</var>)<var><a name="index-ppder-2737"></a></var><br> <blockquote><p>Compute the piecewise <var>m</var>-th derivative of a piecewise polynomial struct <var>pp</var>. If <var>m</var> is omitted the first derivative is calculated. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmkpp.html#doc_002dmkpp">mkpp</a>, <a href="doc_002dppval.html#doc_002dppval">ppval</a>, <a href="doc_002dppint.html#doc_002dppint">ppint</a>. </p></blockquote></div> <!-- ppint scripts/polynomial/ppint.m --> <p><a name="doc_002dppint"></a> <div class="defun"> — Function File: <var>ppi</var> = <b>ppint</b> (<var>pp</var>)<var><a name="index-ppint-2738"></a></var><br> — Function File: <var>ppi</var> = <b>ppint</b> (<var>pp, c</var>)<var><a name="index-ppint-2739"></a></var><br> <blockquote><p>Compute the integral of the piecewise polynomial struct <var>pp</var>. <var>c</var>, if given, is the constant of integration. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmkpp.html#doc_002dmkpp">mkpp</a>, <a href="doc_002dppval.html#doc_002dppval">ppval</a>, <a href="doc_002dppder.html#doc_002dppder">ppder</a>. </p></blockquote></div> <!-- ppjumps scripts/polynomial/ppjumps.m --> <p><a name="doc_002dppjumps"></a> <div class="defun"> — Function File: <var>jumps</var> = <b>ppjumps</b> (<var>pp</var>)<var><a name="index-ppjumps-2740"></a></var><br> <blockquote><p>Evaluate the boundary jumps of a piecewise polynomial. If there are n intervals, and the dimensionality of <var>pp</var> is d, the resulting array has dimensions <code>[d, n-1]</code>. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmkpp.html#doc_002dmkpp">mkpp</a>. </p></blockquote></div> </body></html>