<html lang="en"> <head> <title>Evaluating Polynomials - 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="next" href="Finding-Roots.html#Finding-Roots" title="Finding Roots"> <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="Evaluating-Polynomials"></a> <p> Next: <a rel="next" accesskey="n" href="Finding-Roots.html#Finding-Roots">Finding Roots</a>, Up: <a rel="up" accesskey="u" href="Polynomial-Manipulations.html#Polynomial-Manipulations">Polynomial Manipulations</a> <hr> </div> <h3 class="section">28.1 Evaluating Polynomials</h3> <p>The value of a polynomial represented by the vector <var>c</var> can be evaluated at the point <var>x</var> very easily, as the following example shows: <pre class="example"> N = length(c)-1; val = dot( x.^(N:-1:0), c ); </pre> <p class="noindent">While the above example shows how easy it is to compute the value of a polynomial, it isn't the most stable algorithm. With larger polynomials you should use more elegant algorithms, such as Horner's Method, which is exactly what the Octave function <code>polyval</code> does. <p>In the case where <var>x</var> is a square matrix, the polynomial given by <var>c</var> is still well-defined. As when <var>x</var> is a scalar the obvious implementation is easily expressed in Octave, but also in this case more elegant algorithms perform better. The <code>polyvalm</code> function provides such an algorithm. <!-- polyval scripts/polynomial/polyval.m --> <p><a name="doc_002dpolyval"></a> <div class="defun"> — Function File: <var>y</var> = <b>polyval</b> (<var>p, x</var>)<var><a name="index-polyval-2700"></a></var><br> — Function File: <var>y</var> = <b>polyval</b> (<var>p, x, </var>[]<var>, mu</var>)<var><a name="index-polyval-2701"></a></var><br> <blockquote><p>Evaluate the polynomial <var>p</var> at the specified values of <var>x</var>. When <var>mu</var> is present, evaluate the polynomial for (<var>x</var>-<var>mu</var>(1))/<var>mu</var>(2). If <var>x</var> is a vector or matrix, the polynomial is evaluated for each of the elements of <var>x</var>. — Function File: [<var>y</var>, <var>dy</var>] = <b>polyval</b> (<var>p, x, s</var>)<var><a name="index-polyval-2702"></a></var><br> — Function File: [<var>y</var>, <var>dy</var>] = <b>polyval</b> (<var>p, x, s, mu</var>)<var><a name="index-polyval-2703"></a></var><br> <blockquote><p>In addition to evaluating the polynomial, the second output represents the prediction interval, <var>y</var> +/- <var>dy</var>, which contains at least 50% of the future predictions. To calculate the prediction interval, the structured variable <var>s</var>, originating from <code>polyfit</code>, must be supplied. <!-- 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_002dpolyvalm.html#doc_002dpolyvalm">polyvalm</a>, <a href="doc_002dpolyaffine.html#doc_002dpolyaffine">polyaffine</a>, <a href="doc_002dpolyfit.html#doc_002dpolyfit">polyfit</a>, <a href="doc_002droots.html#doc_002droots">roots</a>, <a href="doc_002dpoly.html#doc_002dpoly">poly</a>. </p></blockquote></div> <!-- polyvalm scripts/polynomial/polyvalm.m --> <p><a name="doc_002dpolyvalm"></a> <div class="defun"> — Function File: <b>polyvalm</b> (<var>c, x</var>)<var><a name="index-polyvalm-2704"></a></var><br> <blockquote><p>Evaluate a polynomial in the matrix sense. <p><code>polyvalm (</code><var>c</var><code>, </code><var>x</var><code>)</code> will evaluate the polynomial in the matrix sense, i.e., matrix multiplication is used instead of element by element multiplication as used in <code>polyval</code>. <p>The argument <var>x</var> must be a square matrix. <!-- 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_002droots.html#doc_002droots">roots</a>, <a href="doc_002dpoly.html#doc_002dpoly">poly</a>. </p></blockquote></div> </body></html>