Exercise No.
6
POLYNOMIALS IN MATLAB
NAME: DATE PERFORMED:
SECTION: DATE SUBMITTED:
GROUP NO. : INSTRUCTOR:
I. Objective:
to learn how to represent polynomials in MATLAB, find roots of polynomials, create polynomials when roots are known and obtain
partial fractions.
II. Overview:
MATLAB provides functions for standard polynomial operations, such as polynomial roots, evaluation, and differentiation. In
addition, there are functions for more advanced applications, such as curve fitting and partial fraction expansion.
Polynomial Function Summary
Function Description
Conv Multiply polynomials
Deconv Divide polynomials
Poly Polynomial with specified roots
Polyder Polynomial derivative
Polyfit Polynomial curve fitting
Polyval Polynomial evaluation
Polyvalm Matrix polynomial evaluation
Residue Partial-fraction expansion (residues)
Roots Find polynomial roots
Symbolic Math Toolbox contains additional specialized support for polynomial operations.
III. Implementation:
1. MATLAB represents polynomials as row vectors containing coefficients ordered by descending powers.
For example, consider the equation
p(x) = x3 – 2x – 5
To enter this polynomial into MATLAB,
type:
p = [1 0 -2 -5];
2. The roots function calculates the roots of a polynomial.
Type:
r = roots(p)
r=
2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i
Note: By convention, MATLAB stores roots in column vectors.
3. The function poly returns to the polynomial coefficients:
p2 = poly(r)
p2 =
1 8.8818e-16 -2 -5
poly and roots are inverse functions,
For LPU Cavite use only Exercise 5 – Polynomials in Matlab
Evaluation copy
Student’s Manual Page 1 of 4
4. The poly function also computes the coefficients of the characteristic polynomial of a matrix:
Type:
A = [1.2 3 -0.9; 5 1.75 6; 9 0 1];
poly(A)
ans =
1.0000 -3.9500 -1.8500 -163.2750
Note: The roots of this polynomial, computed with roots, are the characteristic roots, or eigenvalues, of
the matrix A. (Use eig to compute the eigenvalues of a matrix directly.)
5. The polyval function evaluates a polynomial at a specified value. To evaluate p at s = 5, use
Type:
polyval(p,5)
ans =
110
6. It is also possible to evaluate a polynomial in a matrix sense.
In this case the equation
p(x) = x3 – 2x – 5
becomes p(X) = X3 – 2X – 5I, where X is a square matrix and I is the identity matrix.
For example, create a square matrix X and evaluate the polynomial p at X:
Type:
X = [2 4 5; -1 0 3; 7 1 5];
Y = polyvalm(p,X)
Y=
377 179 439
111 81 136
490 253 639
7. Polynomial multiplication and division correspond to the operations convolution and deconvolution. The functions conv and
deconv implement these operations.
Consider the polynomials a(s) = s2 + 2s + 3 and b(s) = 4s2 + 5s + 6. To compute their product,
Type:
a = [1 2 3]; b = [4 5 6];
c = conv(a,b)
c=
4 13 28 27 18
Use deconvolution to divide back out of the product.
Type:
[q,r] = deconv(c,a)
q=
456
r=
00000
8. The polyder function computes the derivative of any polynomial. To obtain the derivative of the polynomial
Type:
p= [1 0 -2 -5]
q = polyder(p)
q=
3 0 -2
For LPU Cavite use only Exercise 5 – Polynomials in Matlab
Evaluation copy
Student’s Manual Page 2 of 4
9. Polyder also computes the derivative of the product or quotient of two polynomials.
For example, create two polynomials a and b, type:
a = [1 3 5];
b = [2 4 6];
Calculate the derivative of the product a*b by calling polyder with a single output argument:
c = polyder(a,b)
c=
8 30 56 38
Calculate the derivative of the quotient a/b by calling polyder with two output arguments:
[q,d] = polyder(a,b)
q=
-2 -8 -2
d=
4 16 40 48 36
q/d is the result of the operation.
10. ‘residue’ finds the partial fraction expansion of the ratio of two polynomials. This is particularly useful for applications that
represent systems in transfer function form.
For polynomials b and a,
if there are no multiple roots, where r is a column vector of residues, p is a column vector of pole locations, and k is a row vector of
direct terms.
Consider the transfer function
Type:
b = [-4 8];
a = [1 6 8];
[r,p,k] = residue(b,a)
r=
-12
8
p=
-4
-2
k=
[]
11. Given three input arguments (r, p, and k), residue converts back to polynomial form:
Type:
[b2,a2] = residue(r,p,k)
b2 =
-4 8
a2 =
168
For LPU Cavite use only Exercise 5 – Polynomials in Matlab
Evaluation copy
Student’s Manual Page 3 of 4
IV. Exercises:
1. Consider the two polynomials p(s) = s2 + 2s + 1 and and q(s) = s + 1. Using MATLAB, compute:
a. p(s)*q(s) = ______________________________________
Matlab code:
b. Roots of p(s) and q(s) = ____________________________
Matlab code:
c. p(-1) and q(6) = ___________________________________
Matlab code:
2. Use MATLAB command to find the partial fraction of the following:
Answers:
a)
Matlab code:
b)
Matlab code:
V. Generalization:
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
_________________________________________________________________________________________________________
For LPU Cavite use only Exercise 5 – Polynomials in Matlab
Evaluation copy
Student’s Manual Page 4 of 4