polyval

Evaluates a polynomial at given points.

Category: Mathematics > Elementary Math

Syntax

y = polyval(p, x)

Evaluates the polynomial with coefficients p at each element of x.

Description

For p = [p1 p2 ... pn] the value is p1*x^(n-1) + p2*x^(n-2) + ... + pn, computed by Horner's scheme, which needs only n-1 multiplications and is more accurate than summing powers. The evaluation is element by element: a matrix x gives a matrix of values, not a matrix polynomial (for that, see polyvalm).

Input arguments

p
Coefficient vector, highest power first. An empty p gives 0.
x
Array of points: real or complex, any size.

Output arguments

y
Array of the same size as x.

Example

Vector and matrix of points

p = [1 2 3];
polyval(p, [0 1 2])
polyval([2 0 -1], [1 2; 3 4])
Output
ans = 1×3

     3     6    11
ans = 2×2

     1     7
    17    31

See also

polyfit polyvalm roots polyder poly