Polynomial Calculation
Using MATLAB
• Polynomial
The polynomial is an equation that has two variables one of them
called dependent and the second is independent.
Example 1:
𝑦 = 𝑎𝑥4 + 𝑏 𝑥3 + 𝑐 𝑥2 + 𝑑𝑥 + 𝑒
The dependent variable is (𝑦) and the independent variables is
(𝑥) a, b, c, d, and e are constants coefficients.
2
• Polynomial MATLAB representation
To represent the polynomial in MATLAB for the example 1 the following
syntax will be used. It’s clear that we must define the values of the
constants a, b, c, d, and e before they used.
3
1.𝑦 = 𝑥 4 form.
Write the following equations in
2.𝑦 = 𝑥4 − 12 𝑥3 + 25𝑥 +
MATLAB −2
Example
2: 𝑦 =53 𝑥6 + 𝑥2 −
5𝑥
3. 116
>> p=[1 0 0 0 -2]
p =
1 0 0 -2
>> p=[10 -12 0 25 116]
p =
1 - 25 116
12 0
>> p=[3/5 0 0 0 1 -5 0]
Note: The coefficients are ordered in descending powers. 𝑦 = 𝑝1 𝑥 𝑛 + 𝑝2 𝑥 𝑛−1 +
p = 0.6000 0 0 0 1.0000 -5.0000 0
⋯ + 𝑝𝑛 𝑥 + 𝑝𝑛+1
The polynomial degree is equal to the length of the vector p reduced by one. 4
• Evaluation of Polynomial Values
The (polyval) function can be used to evaluate the values of polynomial according to the specified points.
Syntax
y = polyval(p,x)
y = polyval(p,x) returns the value of a polynomial of degree n evaluated at x. The
input argument p is a vector of length n+1 whose elements are the coefficients in
descending powers of the polynomial to be evaluated.
𝑦 = 𝑝1 𝑥 𝑛 + 𝑝2 𝑥𝑛−1 + ⋯ + 𝑝𝑛𝑥 + 𝑝𝑛+1
5
1.𝑦 = 𝑥 4 − 2
Compute the values of the following equations at x= 3, 6, and
2.𝑦 = 𝑥 4 − 12 𝑥 3 + 25𝑥 +
10.
Example: 3. 116𝑦 =53 𝑥 6 + 𝑥 2 −
5𝑥
6
• Roots of polynomial
The root of equation is the value of 𝑥 that make the equation equal to
zero.
Example1:
Find the roots of the following equations. 2. Mathematical solution: 2. MATLAB
1.𝑦 = 𝑥 2 − 4
First solution
solution
2.𝑦 = 𝑥 2 − 5𝑥 − 6 𝑥2 − 5𝑥 − 6 = 0
Set the value of y=0 then >> p=[1 -5 -6]
(𝑥 + 1)(𝑥 − 6) = 0
p =
𝑥 = −1 𝑜𝑟 𝑥 = 6
1 -5
1. Solution: 1. MATLAB Solution
-6
Mathematical >> p=[1 0 -4]
Second solution can be found
solution: Set the p =
from the following equation >>
value of y=0 then 1 0 -4
𝑥 2 − 4= 0
x =x=roots(p)
>> x=roots(p) 6
𝑥2 = 4
x = -1
𝑥=±2
2.0000
-2.0000
7
Example 2:
Find 100 points (𝑥, 𝑦) that satisfy the following equation. 𝑦 = 𝑥2 −
5𝑥 − 6
solution:
p=[1 -5 -6] % define the polynomial order
r=roots(p) % find the roots of the polynomial function
x=linspace(r(1),r(2),100) % find the set of numbers by defining the first and
last limit with 100 points between them (finding the domain of function)
y=polyval(p,x) % find the value of y by substituting each value of
x which starting -1 to 6 with 100 points between them to find the function)
plot(x,y) % draw a relation between x and y
8
solution: continue
p=
1 -5
-6
r=
6
-1
x =
6.0000 5.9293 5.8586 5.7879 5.7172 5.6465 5.5758 5.5051 5.4343 5.3636 5.2929
5.2222 5.1515 5.0808 5.0101 4.9394 4.8687 4.7980 4.7273 4.6566 4.5859 4.5152
4.4444 4.3737 4.3030 4.2323 4.1616 4.0909 4.0202 3.9495 3.8788 3.8081 3.7374
3.6667 3.5960 3.5253 3.4545 3.3838 3.3131 3.2424 3.1717 3.1010 3.0303 2.9596
2.8889 2.8182 2.7475 2.6768 2.6061 2.5354 2.4646 2.3939 2.3232 2.2525 2.1818
2.1111 2.0404 1.9697 1.8990 1.8283 1.7576 1.6869 1.6162 1.5455 1.4747 1.4040
1.3333 1.2626 1.1919 1.1212 1.0505 0.9798 0.9091 0.8384 0.7677 0.6970 0.6263
0.5556 0.4848 0.4141 0.3434 0.2727 0.2020 0.1313 0.0606 -0.0101 -0.0808 -0.1515
-0.2222 -0.2929 -0.3636 -0.4343 -0.5051 -0.5758 -0.6465 -0.7172 -0.7879 - -
0.8586
0.9293 -1.0000
y=
0 -0.4900 -0.9699 -1.4399 -1.8998 -2.3498 -2.7897 -3.2197 -3.6396 -4.0496 -
4.4495 -4.8395 -5.2195 -5.5894 -5.9494 -6.2994 -6.6393 -6.9693 -7.2893 - -
7.5992
10.3489 -10.5389 -10.7189 -10.8889 -11.0489 -11.1989 -11.3388 -11.4688 -11.5888 -11.6988
7.8992 -8.1892
-11.7988 -11.8888-8.4691
-11.9688-8.7391
-12.0388-8.9991 -9.2491
-12.0988 -9.4890
-12.1488 -9.7190
-12.1888 -9.9390
-12.2188 -10.1490
-12.2388 -
-12.2488
-12.2488 -12.2388 -12.2188 -12.1888 -12.1488 -12.0988 -12.0388 -11.9688 -11.8888 -11.7988
-11.6988 -11.5888 -11.4688 -11.3388 -11.1989 -11.0489 -10.8889 -10.7189 -10.5389 -10.3489
-10.1490 -9.9390 -9.7190 -9.4890 -9.2491 -8.9991 -8.7391 -8.4691 -8.1892 -7.8992 -
7.5992 -7.2893 -6.9693 -6.6393 -6.2994 -5.9494 -5.5894 -5.2195 -4.8395 -4.4495 -4.0496
-3.6396 -3.2197 -2.7897 -2.3498 -1.8998 -1.4399 -0.9699 -0.4900 0
Note: When the roots are known the polynomial coefficients can
be found by using the (poly function).
Example 3:
The roots are ( -1, 6) find the polynomial
Solution:
>> r=[-1 6]
r = -1
6
>> p=poly(r) -5 -6
p =
1 10
• Polynomial Operations
• Polynomial Addition and Subtractions
The coefficient of the sum of two polynomials is the sum of the coefficients of the two
polynomials. The vectors containing the coefficients must be of the same length. For
𝐴(𝑠) = 𝑠4 − 3𝑠3 − 𝑠 + 2
example, to add
𝐵(𝑠) = 4 𝑠3 − 2 𝑠2 + 5𝑠 − 16
𝐶(𝑠) = 𝐴(𝑠) + 𝐵(𝑆) = 𝑠4 + (4 − 3) 𝑠3 − 2 𝑠2 + (5 − 1)𝑠 + (2 − 16) = 𝑠4 + 𝑠3 − 2 𝑠2 + 4𝑠 − 14
MATLAB solution is
>> a = [1 -3 0 -1 2];
>> b = [0 4 -2 5 -16];
>> c = a + b
c =
1 1 -2 4 -14
11
• Polynomial Operations
• Scalar Multiplication
The coefficient vector of the scalar multiple of a polynomial is simply the scalar
𝐶(𝑠) = 3𝐴(𝑠)
times the coefficient vector of the polynomial. To specify
𝐶(𝑠) = 3 𝑠 4 − 9 𝑠 3 − 3𝑠 + 6
MATLAB solution
>> a = [1 -3 0 -1 2];
>> c=3*a
c =
3 -9 0 -3 6
12
• Polynomial Multiplication
• Scalar Multiplication
𝐴(𝑠) = 𝑠 4 − 3𝑠3 − 𝑠 + 2
𝐵(𝑠) = 4 𝑠 3 − 2 𝑠2
−𝑠 (4
𝑠 3 − 2 𝑠2
+ 5𝑠
+ 5𝑠 − 16
𝐶(𝑠) = 𝐴(𝑠) x 𝐵(𝑆) = 𝑠 4 4 𝑠 3 − 2 𝑠2 + 5𝑠 − 16
−
− 3𝑠3
𝑠 3 − 2 𝑠2 ) + 2 𝑠 3 − 2 𝑠2
MATLAB
= 4𝑠5 + 10𝑠4 − 5𝑠3 + 𝑠2 − 53𝑠 + 16
4
synx + 5𝑠 − 16 16 4 + 5𝑠 −
16
x=conv(p1,
p2)
13
Example 1: Find the polynomial y which is the result of multiplication of the
polynomial 𝑦1 and 𝑦2
𝑦1 = 𝑠4 − 3𝑠3 − 𝑠 + 2 and 𝑦2 = 4 𝑠3 − 2 𝑠2 +
5𝑠 − 16
>> p1=[1 -3 0 -1 2]
p1 =
1 -3 0 -1 2
>> p1=[4 -2 5 -16]
p1 =
4 -2 5 -16
>> p=conv(p1,p2)
p=
14
4 10 -5 1 -53 16
Example 2: Find the polynomial y which is the result of multiplication of
polynomial 𝑦1 𝑦1 = 𝑥2 − 2
the
and 𝑦2 𝑦2 = 𝑥2 + 3𝑥
−1
Solution: The MATLAB
solution
𝑦 = 𝑦1 × 𝑦2
The mathematical solution is:
𝑦 = (𝑥2 − 2)(𝑥2 + 3𝑥 − 1)
𝑦 = 𝑥4 + 3 𝑥3 − 𝑥2 − 2 𝑥2 − 6𝑥
+2
𝑦 = 𝑥4 + 3 𝑥3 − 3 𝑥2 − 6𝑥 + 2
15
Class activity:
1. Find the roots of the polynomial
2. It is known that the roots of a polynomial are 1, 2, 3 & 4 .
Compute the coefficients of this polynomial.
3. Let
16
THANK YOU
17
THANK YOU
18