28.1 Evaluating Polynomials

The value of a polynomial represented by the vector c can be evaluated at the point x very easily, as the following example shows:

N = length (c) - 1;
val = dot (x.^(N:-1:0), c);

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 polyval does.

In the case where x is a square matrix, the polynomial given by c is still well-defined. As when x is a scalar the obvious implementation is easily expressed in Octave, but also in this case more elegant algorithms perform better. The polyvalm function provides such an algorithm.

: y = polyval (p, x)
: y = polyval (p, x, [], mu)
: [y, dy] = polyval (p, x, s)
: [y, dy] = polyval (p, x, s, mu)

Evaluate the polynomial p at the specified values of x.

If x is a vector or matrix, the polynomial is evaluated for each of the elements of x.

When mu is present, evaluate the polynomial for (x - mu(1)) / mu(2).

In addition to evaluating the polynomial, the second output represents the prediction interval, y +/- dy, which contains at least 50% of the future predictions. To calculate the prediction interval, the structured variable s, originating from polyfit, must be supplied.

See also: polyvalm, polyaffine, polyfit, roots, poly.

: polyvalm (c, x)

Evaluate a polynomial in the matrix sense.

polyvalm (c, x) will evaluate the polynomial in the matrix sense, i.e., matrix multiplication is used instead of element by element multiplication as used in polyval.

The argument x must be a square matrix.

See also: polyval, roots, poly.

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/Evaluating-Polynomials.html