Documentation

Create and Evaluate Polynomials

This example shows how to represent a polynomial as a vector in MATLAB® and evaluate the polynomial at points of interest.

Representing Polynomials

MATLAB® represents polynomials as row vectors containing coefficients ordered by descending powers. For example, the three-element vector

p = [p2 p1 p0];

represents the polynomial

p ( x ) = p 2 x 2 + p 1 x + p 0 .

Create a vector to represent the quadratic polynomial p ( x ) = x 2 - 4 x + 4 .

p = [1 -4 4];

Intermediate terms of the polynomial that have a coefficient of0must also be entered into the vector, since the0acts as a placeholder for that particular power ofx.

Create a vector to represent the polynomial p ( x ) = 4 x 5 - 3 x 2 + 2 x + 3 3 .

p = [4 0 0 -3 2 33];

Evaluating Polynomials

After entering the polynomial into MATLAB® as a vector, use thepolyval计算多项式在一个特定的函数value.

Usepolyvalto evaluate p ( 2 ) .

polyval(p,2)
ans = 153

Alternatively, you can evaluate a polynomial in a matrix sense usingpolyvalm. The polynomial expression in one variable, p ( x ) = 4 x 5 - 3 x 2 + 2 x + 3 3 , becomes the matrix expression

p ( X ) = 4 X 5 - 3 X 2 + 2 X + 3 3 I ,

whereXis a square matrix andIis the identity matrix.

Create a square matrix,X, and evaluatepatX.

X = [2 4 5; -1 0 3; 7 1 5]; Y = polyvalm(p,X)
Y =3×3154392 78561 193065 49001 24104 59692 215378 111419 269614

See Also

|||

Related Topics