ECE120L – INTRODUCTION TO MATLAB
LABORATORY ACTIVITY #3
Polynomials and Partial Fraction Expansion
I. Learning Outcomes:
At the end of the laboratory activity, the students should be able to:
1. To describe polynomials in MATLAB
2. To implement polynomials addition, subtraction, scalar multiplication, multiplication, and division with
MATLAB
3. To determine the roots of polynomials
4. To implement partial – fraction expansion with MATLAB
II. Primer:
A. Polynomials
MATLAB is capable working with polynomials which are used in many advanced courses and applications
in engineering. To describe a polynomial in MATLAB with a row vector whose elements are the polynomial’s
coefficients, consider: f ( x) = 4 x3 − 8x 2 + 7 x − 5
>> % to represent the polynomial 4x3-8x2+7x-5
>> clear
>> f = [4, -8, 7, -5]
f=
4 -8 7 -5
Polynomial Algebra
1. Addition and Subtraction
To add and/or subtract two polynomials, add/ subtract the arrays that describe their coefficients. If the
polynomials are of different degrees, add zeros to the coefficient array of the lower degree polynomial.
Consider: f ( x) = 10x 3 − 6x 2 + 2 x + 10 and g ( x) = 5x 2 − 4 x + 3 ,
>> % Polynomial addition and subtraction
>> clear
>> f = [10, -6, 2, 10];
>> g = [0, 5, -4, 3];
>> h = f + g %addition
h=
10 -1 -2 13
>> k = f – g %subtraction
k=
10 -11 6 7
Thus the result for addition is h( x) = 10x 3 − x 2 − 2 x + 13 and for subtraction is k ( x) = 10x 3 − 11x 2 + 6x + 7
PREPARED BY: RONEL V. VIDAL, PECE 1
2. Scalar Multiplication of Polynomials
To multiply a polynomial by a scalar, simply multiply the coefficient array by that scalar. Consider
f ( x) = 10x 3 − 6x 2 + 2 x + 10 and g ( x) = 5x 2 − 4 x + 3 .
>> % Scalar multiplication of Polynomial
>> clear
>> f = [10, -6, 2, 10];
>> f1 = 5*f %to multiply f(x) by a scalar 5
f1 =
50 -30 10 50
>> g = [0, 5, -4, 3];
>> g1 = 6*g %to multiply g(x) by a scalar 6
g1 =
0 30 -24 18
Thus the result of scalar multiplication is f1 ( x) = 50x 3 − 30x 2 + 10x + 50 and g1 ( x) = 30x 2 − 24 x + 18 .
3. Multiplication and Division of Polynomials
The command conv(a,b) (stands for “convolve”) computes the product of the two polynomials described
by the coefficient arrays “a” and “b”. The two polynomials need not be the same degree. The result is the
coefficient array of the product polynomial.
To perform synthetic division of polynomials, use the command [quotient; remainder] = deconv(num,
den) which computes the results of dividing a numerator polynomial, whose coefficient array is “num”, by a
denominator polynomial represented by the coefficient array “den”. The quotient polynomial is given by the
coefficient array “quotient”, “remainder” is the remainder polynomial.
Consider f ( x) = 20x 3 − 7 x 2 + 5x + 10 and g( x) = 4 x 2 + 12x − 3 .
>> %Multiplication of two Polynomials
>> clear
>> f = [20, -7, 5, 10];
>> g = [4, 12, -3];
>> product = conv(f,g)
product =
80 212 -124 121 105 -30
Thus the result is product = 80x 5 + 212x 4 − 124x 3 + 121x 2 + 105x − 30 .
>> %Division of two Polynomials
>> clear
>> f = [20, -7, 5, 10];
>> g = [4, 12, -3];
>> [quotient, remainder] = deconv(f,g)
quotient =
5.0000 -16.7500
remainder =
PREPARED BY: RONEL V. VIDAL, PECE 2
0 0 221.0000 -40.2500
Thus the result is quotient = 5x − 16.75 and with remainder = 221x − 40.25 .
4. Polynomial Roots
The command roots(a) computes an array containing the roots of a polynomial specified by the
coefficient array “a”. The result is a column array that contains the polynomial’s roots. Consider
f (t ) = t 3 − 7t 2 + 40t − 34
>> %Polynomial Roots or >> %Polynomial Roots
>> clear >> clear
>> f =[1, -7, 40, -34]; >> roots([1, -7, 40, -34])
>> roots(f) ans =
ans = 3.0000 + 5.0000i
3.0000 + 5.0000i 3.0000 - 5.0000i
3.0000 - 5.0000i 1.0000
1.0000
The roots are t = 1, 3 + 5j, and 3 – 5j
5. Polynomials Coefficient from Roots
The poly(r) function computes coefficients of the polynomial whose roots are specified by the array “r”.
The result is a row array that contains the polynomial’s coefficients.
Given the roots 1, 3 + 5i, and 3 – 5i, find the polynomial coefficients.
>> %To find polynomial coefficients from the given roots
>> clear
>> r = [1, 3+5i, 3-5i];
>> poly(r)
ans =
1 -7 40 -34
Thus the polynomial is x 3 − 7 x 2 + 40x − 34 .
6. 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”
Consider f ( x) = 9 x3 − 5x 2 + 3x + 7 . To evaluate at the points x = 0, 2, 4, 6, 8, 10, type
>> %Evaluates a polynomial at specified values of x
>> clear
>> a = [9 -5 3 7];
>> x = [0:2:10];
>> f = polyval(a,x)
f=
7 65 515 1789 4319 8537
PREPARED BY: RONEL V. VIDAL, PECE 3
B. Partial – Fraction Expansion with MATLAB
Consider:
B( s ) Num b0 s n + b1 s n −1 + ...... + bn
= = n
A( s ) Den s + a1 s n −1 + ...... + a n
Num = [b0, b1, ……. bn] – coefficient of the numerator
Den = [1, a1, a2, ……an] – coefficient of the denominator
The command [r,p,k] = residue(Num,Den) finds the residue (r), poles (p), and direct terms (k) of a
partial – fraction expansion.
B( s ) r (1) r ( 2) r(n)
= + + ........ + + k ( s)
A( s ) s − p(1) s − p( 2) s − p( n )
Example:
Consider:
B( s ) 2 s 3 + 5s 2 + 3s + 6
=
A( s ) s 3 + 6s 2 + 11s + 6
The MATLAB program:
>> %partial - fraction expansion of polynomials(Num & Den)
>> Num = [2 5 3 6]; %coefficient of the numerator
>> Den = [1 6 11 6]; %coefficient of the denominator
>> [r,p,k] = residue(Num,Den)
r=
-6.0000
-4.0000
3.0000
p=
-3.0000
-2.0000
-1.0000
k=
2
Partial – fraction expansion of B(s)/A(s):
B( s) − 6 −4 3
= + + +2
A( s) s + 3 s + 2 s + 1
PREPARED BY: RONEL V. VIDAL, PECE 4
III. Laboratory Excercises
A. Polynomials
𝑓(𝑥) = 𝑥 4 − 8𝑥 3 − 9𝑥 − 6
𝑔(𝑥) = 𝑥 3 + 𝑥 2 − 3𝑥 + 1
ℎ(𝑥) = 3𝑥 4 − 8𝑥 3 − 37𝑥 2 + 2𝑥 + 4
𝑝(𝑥) = 2𝑥 3 + 3𝑥 2 − 2𝑥 + 5
1. Add, subtract, multiply, and divide each pair of polynomials
2. Multiply the four polynomials by 4, 5, 6, and 7, in that order
3. Find the roots of each four given polynomials
4. Use the results of part 3 to find the polynomial coefficients from the given roots
5. Evaluate the four polynomials at the point x =1, 3, 5, and –2
B. Partial – Fraction Expansion with MATLAB
Determine the partial fraction expansion of the following:
10(𝑠 + 7)
𝑇(𝑠) =
(𝑠 + 3)(𝑠 + 6)
𝑠 5 + 2𝑠 4 + 4𝑠 3 + 𝑠 2 + 3
𝐹(𝑠) = 6
𝑠 + 7𝑠 5 + 3𝑠 4 + 2𝑠 3 + 𝑠 2 + 3
𝑠 4 + 2𝑠 3 + 5𝑠 2 + 𝑠 + 1
𝐺(𝑠) = 5
𝑠 + 3𝑠 4 + 2𝑠 3 + 4𝑠 2 + 5𝑠 + 2
𝑠 3 + 4𝑠 2 + 6𝑠 + 5
𝑌(𝑠) =
(𝑠 + 8)(𝑠 2 + 8𝑠 + 3)(𝑠 2 + 5𝑠 + 7)
PREPARED BY: RONEL V. VIDAL, PECE 5