Department of Chemical Engineering
MATLAB Tutorial
 

Polynomials                                                                                             (last updated  9/9/99)

The information in this tutorial is located in Chapter 14 of the MATLAB manual,   The Student Edition of MATLAB, Version 4, The MATH WORKS Inc. Prentice-Hall, 1997.

======================================================================================

This tutorial contains the following sections;

Introduction

Calculation of the Roots of a Polynomial Given the Coefficients (the roots command)

Calculation of the Function Value of a Polynomial Given the Coefficients. (the polyval command)

Calculation of the Coefficients of a Polynomial Given the Roots. (the poly command)

Determining the Coefficients of a Polynomial that is the Best Fit of a Given Set of Data (the polyfit command)
 
=============================================================================================================

Introduction

Polynomials are often used to represent data because of our ability to integrate and differentiate them. MATLAB offers a number of specific functions dealing with polynomials.

=============================================================================================================

Calculation of the Roots of a Polynomial Given the Coefficients (the roots command)    Section 14.1

To calculate the roots of a polynomial given the coefficients, enter the coefficients in an array in decending order. Be sure to include zeroes where appropriate.

>> p1 = [ 1 6 7 -6 -8 ];     %    The roots of   x^4 + 6*x^3 + 7*x^2 - ^*x - 8  =  0
>> r1 = roots(p1)
r1 =
    -4.0000
    -2.0000
    -1.0000
    1.0000

The coefficients could be entered directly in the roots command. The same answer as above would be obtained using the following expression.
 
>> r1 = roots([ 1 6 7 -6 -8 ])
r1 =
    -4.0000
    -2.0000
    -1.0000
    1.0000
 

The roots command can find imaginary roots.

>> p2 = [ 1 -6 18 -30 25 ];
>> r2 = roots(p2)
r2 =
    1.0000 + 2.0000i
    1.0000 - 2.0000i
    2.0000 + 1.0000i
    2.0000 - 1.0000i
 

It can also find repeated roots. Note the imaginary portion of the repeated roots is displayed as zero.

>> p3 = [ 1 7 12 -4 -16 ];
>> r3 = roots(p3)
r3 =
    -4.0000
    -2.0000 + 0.0000i
    -2.0000 - 0.0000i
    1.0000

=============================================================================================================

Calculation of the Function Value of a Polynomial Given the Coefficients. (the polyval command)    Section 14.6

The syntax for determining the value of a polynomial at any point is as follows.

>> s1a = polyval(p1, 3)
s1a =
    280

Where p1 is the vector containing the polynomial coefficients, (see above). Similarly, the coefficients can be entered directly in the polyval command.

>> s1b = polyval([1 6 7 -6 -8], 3)
s1b =
    280
 

The function value at multiple points can be found.

>> z = [ 3 5 7 ];
>> s1c = polyval(p1,z)
s1c =
    280   1512   4752

=============================================================================================================

Calculation of the Coefficients of a Polynomial Given the Roots. (the poly command)

To calculate the coefficients given the roots, use the poly command. The coefficients are returned in descending order.

>> r1 = [ -4 -2 -1 1 ]
>> t1 = poly(r1)
t1 =
    1.0000   6.0000   7.0000   -6.0000   -8.0000
 

The roots can be entered directly in the poly command.

>> t2 = poly([ -4 -2 -1 1 ])
t2 =
    1   6   7   -6   -8
 
=============================================================================================================

Determining the Coefficients of a Polynomial that is the Best Fit of a Given Set of Data (the polyfit command)   Section 15.1

polyfit determines the least squares fit of a set of data, given the data and the order of the polynomial.

>> x = [ 1.0 1.3 2.4 3.7 3.8 5.1 ];
>> y = [ -6.3 -8.7 -5.2 9.5 9.8 43.9 ];
>> coeff = polyfit(x,y,3)         %   Fits a third order polynomial
coeff =
    0.3124   1.5982   -7.3925   -1.4759

After determining the polynomial coefficients, the polyval command can be used to predict the values of the dependent variable at each value of the independent variable.

>> ypred = polyval(coeff,x)
ypred =
    -6.9579   -7.6990   -5.6943   8.8733   10.6506   43.8273

The experimental data points and the predicted points could now be plotted (see Plotting Basics tutorial).