Polynomial Operations Using Arrays Matlab Help

2.5 Polynomial Operations Using Arrays

MATLAB has some convenient vector-based tools for working with polynomials, which are used in many advanced courses and applications in engineering. Type help polyfun for more information on this category of commands. We will use the following notation to describe a polynomial:

f(x) =alxn +a2Xn-1 + a3Xn-2 + … +an_Ix2 +anx + an+1

This polynomial is a function of x. Its degree or order is n, the highest power of x that appears in the polynomial. The a., i = 1,2, … , n + 1 are the polynomial’s coefficients. We can describe a polynomial in MATLAB with. a row vector whose elements are the polynomial’s coefficients, starting with the coefficient of the
highest power of x. This vector is [al. a2, a3, …• an-I, an, an+d. For example, the vector [4, – 8, 7 , – 5) represents the polynomial ax? – 8×2 +7x – 5. Polynomial roots can be found with the root s (a) function, where (a) is
the array containing the polynomial coefficients. For example, to obtain the roots of x3 + 12×2 +45x +50 = 0, you type y = root s ( [1, 12, 45, 50) ) . The answer (y) is a column array containing the values -2, -5, -5.
The poly (r) function computes the coefficients of the polynomial whose roots are specified by the array r. The result is a row array that contains the polynomial’s coefficients. (Note that the root s function returns it column array.) For example, to find the polynomial whose roots are I and 3 ± 5i, the session is
»r = [l,3+5i,3-5i);
»poly(r)
ans =

1 -7 40 -34

Thus the polynomial is x3 – 7×2 + 40x – 34. The two commands could have been combined into the single command poly ( [1, 3+ 5i, 3 – 5 i) ) .

Polynomial Addition and Subtraction

To add two polynomials, add the arrays that describe their coefficients. If the polynomials are of different degrees, add zeros to the coefficient array of the lower-degree polynomial. For example, consider
f(x) = 9×3
– 5×2 + 3x + 7
whose coefficient array is

f = [ 9 , – 5 , 3 , 7] and
g(x) = 6×2 – X + 2
whose coefficient array is g = [6, -1 , 2 ]. The degree of g(x) is one less that of f(x). Therefore, to add f(x) and g(x), we append one zero to g to “fool” MATLAB into thinking g(x) is a third-degree polynomial. That is, we
type g = [0 g] to obtain [0, 6 , -1, 2] for g. This vector represents g(x) = Ox3 + 6×2 – X + 2. To add the polynomials, type h = f+g. The result is h = [9,1,2,9], which corresponds to h(x) = 9×3 + x2 + 2x + 9. Subtraction is
done in a similar way:

Polynomial Multiplication and Division

To multiply a polynomial by a scalar, simply multiply the coefficient array by that ‘scalar. For example, 5th (x) is represented by [45, 5 , 10 , 45 J. . Multiplication of polynomials by hand can be tedious, and polynomial division
is even more so, but these operations are easily done with MATLAB. Use the cony function (it stands for “convolve”) to multiply polynomials and use the deconv function (deconv stands for “deconvolve”) to perform synthetic division. Table 2.5-1 summarizes these functions, as well as the poly, polyval,
and roots functions, which we saw.

Table 2.5-1 Polynomial functions

Table 2.5-1 Polynomial functions

 The product of the polynomials f(x) and g(x) is f(x)g(x) = (9×3 – 5×2 + 3x + 7)(6×2 – X + 2) –
= 54×5 – 39×4 + 41×3 + 29×2 – X + 14
Dividing f(x) by g(x) using synthetic division gives a quotient of f(x) 9×3 – 5×2 + 3x + 7
. = 1.5x – 0.5833 g(x) = 6×2 – X +2
with a remainder of -0.5833x +8.1667. Here is the MATLAB session to perform these operations.
»f = [9,-5,3,7]; »g = [6 , -I, 2] ;
»product  =  conv (f,g) product’ =
54 -39 41 29 »[quotient,. remainder] =
quotient = 1.5 -0.5833
remainder = o 0
-1 14 deconv (f,g)
-0.5833 8.1667

The conv and deconv- functions do not require that the polynomials have the same degree, so we did not have to fool MATLAB as we did when adding the polynomials. Table 25-1 gives the general syntax for the cony and deconv functions.

Plotting Polynomials
The polyval (a, x’) function evaluates a polynomial at specified values of its independent variable x, which can be a matrix or a vector. The polynomial’s coefficient array is a. The result is the same size as x. For example, to evaluate the polynomial f(x) = 9×3 – 5×2 + 3x + 7 at the points x = 0,2,4, … ,10,
type »a = [9, – 5,3,7] ;
»x [0:2:10); »f polyval (a,x);
The resulting vector f contains six values that correspond to /(0). /(2), /(4) /(10). These three commands can be combined into a single command:

»f = poly val ([9,-5,3,7), [0:2:10));
Personal preference determines whether to combine terms in this way; some people think that the single. combined command is less readable than three separate commands.

The poly val function is very useful for plotting polynomials. To do this you should define an array that contains many values of the independent variable x in order to obtain a smooth plot. For example, to plot the polynomial f(x) = 9×3 – 5×2 + 3x +7 for -2 :s x :s 5, you type
»a [9,-5,3,7); »x [-2:0.01:5);
»f polyval (a,x); »plot(x,f),xlabel (‘x’),ylabe1(‘f(x) ‘),grid

Test Your Understanding
12.5-1 Use MATLAB to obtain the roots of

 x3 + 13×2 / +52x +6 =o.

Use the poly function to confirm your answer.

12.5-2 Use MATLAB to confirm that

(20×3 – 7×2 + 5x + 1O)(4×2 + 12x – 3) = 80×5 + 212×4 – 124×3 + 121×2 + 105x – 30

T2.5-:3 Use MATLAB to confirm that
I2x3 + 5×2 – 2x +3 = 4x + II
3×2 -7x +4

with a remainder of 59x – 41.

T2.5-4 Use MATLAB to confirm that

6x: +4×2 – 5 = 0.7108

I2x3 – 7×2 + 3x +9
when x = 2.

T2.5-5 Plot the polynomial

y =x3 + 13×2 +52x + 6

 over the range -7 ≤ x ≥ 1.

Posted on July 14, 2015 in Numeric Cell And Structure Arrays

Share the Story

Back to Top
Share This