1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
<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>
|