This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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_2x^2+p_1x+p_0.$$

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

p = [1 -4 4];

Intermediate terms of the polynomial that have a coefficient of 0 must also be entered into the vector, since the 0 acts as a placeholder for that particular power of x.

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

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

Evaluating Polynomials

After entering the polynomial into MATLAB® as a vector, use the polyval function to evaluate the polynomial at a specific value.

Use polyval to evaluate $p(2)$.

ans =


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


where X is a square matrix and I is the identity matrix.

Create a square matrix, X, and evaluate p at X.

X = [2 4 5; -1 0 3; 7 1 5];
Y = polyvalm(p,X)
Y =

      154392       78561      193065
       49001       24104       59692
      215378      111419      269614

See Also

| | |

More About

Was this topic helpful?