0% found this document useful (0 votes)
740 views11 pages

Polynomial Functions: FX A X A X A X A X A

This document discusses polynomial functions in MATLAB. It covers: 1) The general form of an Nth degree polynomial and how coefficients correspond to powers of x. 2) How to evaluate polynomials over a range of x values using polyval or by directly calculating terms. 3) Common polynomial operations like addition, subtraction, multiplication and division that can be performed using functions like conv and deconv. 4) Examples of evaluating, adding, multiplying and dividing polynomials in MATLAB.

Uploaded by

c.huby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
740 views11 pages

Polynomial Functions: FX A X A X A X A X A

This document discusses polynomial functions in MATLAB. It covers: 1) The general form of an Nth degree polynomial and how coefficients correspond to powers of x. 2) How to evaluate polynomials over a range of x values using polyval or by directly calculating terms. 3) Common polynomial operations like addition, subtraction, multiplication and division that can be performed using functions like conv and deconv. 4) Examples of evaluating, adding, multiplying and dividing polynomials in MATLAB.

Uploaded by

c.huby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

ECE 1010 ECE Problem Solving I

Polynomial Functions
• Since polynomials occur frequently in mathematics and engi-
neering, MATLAB has a collection of very useful functions
for working with them
• The general form of an Nth degree polynomial function is
N N–1 2
f ( x ) = a0 x + a1 x + … + aN – 2 x + aN – 1 x + aN
where degree denotes the highest power of x present
• The polynomial coefficients are 0, a 1, …, a N
• You may be bothered by the fact that the powers of x and
coefficient subscripts appear to be opposite each other
– This is just the notation adopted by MATLAB for express-
ing polynomials; in fact the sum of the coefficient sub-
script and variable exponent are always a constant of N
• A more compact notation for writing the above is to use sum
notation
N


N–n
f(x) = an x
n=0
0
Note: x = 1
Example: A familiar polynomial function is the quadratic poly-
nomial
2
y ( x ) = a0 x + a1 x + a2

Chapter 3: Polynomial Functions 3–12


ECE 1010 ECE Problem Solving I

Polynomial Evaluation
• Using techniques studied thus far we can use MATLAB used
to evaluate f ( x ) over a range of x values as follows:
% Define the range on x
x = x_min:x_step:x_max;
f = a_0*x.^N + a_1*x.^(N-1) + ...
a_Nm2*x.^2 + a_Nm1*x + a_N;
• Taking the quadratic given above, with defined coefficients,
we can write
» a0 = 3; a1 = 2; a2 = 10; % Define the coefficients
» x = 0:20/100:20; % Define the range on x
» f = a0*x.^2 + a1*x + a2; % Evaluate f(x)
• To polynomial evaluation MATLAB comes with the pre-
defined function polyval
» help polyval
POLYVAL Evaluate polynomial.
Y = POLYVAL(P,X), when P is a vector of length N+1
whose elements are the coefficients of a polynomial, Y
is the value of the polynomial evaluated at X.

Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1)

If X is a matrix or vector, the polynomial is evalu-


ated at all points in X. See also POLYVALM for evalua-
tion in a matrix sense.
• To evaluate the quadratic given above we would write
» x = 0:20/100:20;
» y = polyval([3, 2, 10],x);

Chapter 3: Polynomial Functions 3–13


ECE 1010 ECE Problem Solving I

Polynomial Operations
• In loose terms we can think of the polynomial coefficient
vector, a, as the polynomial itself
• The sum of two polynomials, say

3 2
g ( x ) = a0 x + a1 x + a2 x + a3
2
h ( x ) = b0 x + b1 x + b2

is the sum of coefficients corresponding to like powers of x

s(x) = g(x ) + h(x)


3 2
= a 0 x + ( a 1 + b 0 )x + ( a 2 + b 1 )x + ( a 3 + b 2 )

• For MATLAB evaluation this amounts to simply adding cor-


responding coefficient vectors a and b by first prepending
the b vector with a leading zero, i.e.,
g = [a0, a1, a2, a3];
h = [0, b0, b1, b2]; % prepending only required
s = g + h; % for poly add and subtract
• If one polynomial is a scalar multiple of another, say
g(x ) = k ⋅ f( x)
the corresponding coefficient vectors are related by k, i.e., if
g = polyval(a,x);
f = polyval(b,x);
% Or given g and the fact that b = k*a
f = k*g;
• Suppose we wish to multiply or divide two polynomials

Chapter 3: Polynomial Functions 3–14


ECE 1010 ECE Problem Solving I

• Recall how tedious it is to multiply two polynomials

2 2
g ( x ) = ( a0 x + a1 x + a2 ) ( b0 x + b1 x + b2 )
2 2 2
= a0 x ( b0 x + b1 x + b2 ) + a1 x ( b0 x + b1 x + b2 )
2
+a 2 ( b 0 x + b 1 x + b 2 )
4 3 2
= a 0 b 0 x + ( a 0 b 1 + a 1 b 0 )x + ( a 0 b 2 + a 1 b 1 + a 2 b 0 )x
+( a 1 b 2 + a 2 b 1 )x + a 2 b 2

• If c is the corresponding coefficient vector of g it follows


that
c = [(a0*b0), (a0*b1+a1*b0), (a0*b2+a1*b1+a2*b0),
(a1*b2+a2*b1), (a2*b2)];
• A MATLAB function that performs this operation automati-
cally is conv(a,b) which stands for convolution (you
learn more about this function in Linear Systems Theory –
ECE 3510)
• In the above example we would type
% To find the coefficients of g(x) = a(x)*b(x) we
% would write
g = conv(a,b)
2 2
Example: g ( x ) = ( x + 2x + 3 ) ( 2x + x + 2 )
» a = [1, 2, 3]; b = [2, 1, 2];
» g = conv(a,b)
g =
2 5 10 7 6

Chapter 3: Polynomial Functions 3–15


ECE 1010 ECE Problem Solving I

• g ( x ) is thus
4 3 2
g ( x ) = 2x + 5x + 10x + 7x + 6
• Polynomial division can be accomplished in like fashion
using the function deconv(a,b) which stands for decon-
volution
Example:
4 3 2
g(x) 2x + 5x + 10x + 7x + 6
h ( x ) = ---------- = ---------------------------------------------------------------
b(x) 2
x + 2x + 2
• Hand calculation requires long division of the polynomial
coefficient vectors, including a remainder vector
2
2x + x + 4
2 4 3 2
x + 2x + 2 2x + 5x + 10x + 7x + 6
4 3
2x + 4x + 4x 2
3 2
x + 6x + 7x + 6
3 2
x + 2x + 2x
2
4x + 5x + 6
2
4x + 8x + 8
Remainder – 3x – 2

• To find the quotient and remainder polynomials in MATLAB


write
» g = [2, 5, 10, 7, 6];
» b = [1, 2, 2];
» [h,r] = deconv(g,b);

Chapter 3: Polynomial Functions 3–16


ECE 1010 ECE Problem Solving I

» h
h =
2 1 4 % --> h(x) = 2x^2+x+4
» r
r =
0 0 0 -3 -2 % --> r(x) = -3x-2

Example: Practice! p. 82, #2 & #5

3 2
f 2 ( x ) = x – 6x + 12x – 8
3 2
f 4 ( x ) = x – 5x + 7x – 3

• For #2 we must evaluate f 2 ( x ) – 2f 4 ( x ) on [0, 4]


» x = 0:4/50:4; % Use 51 points
» a_f2 = [1 -6 12 -8];
» a_f4 = [1 -5 7 -3];
» a_p2 = a_f2 - 2*a_f4;
» f_p2 = polyval(a_p2,x);
» plot(x,f_p2)
» title('Practice! p. 82, #2','fontsize',18)
» ylabel('f2(x) - 2f4(x)','fontsize',16)
» xlabel('x','fontsize',16)

Chapter 3: Polynomial Functions 3–17


ECE 1010 ECE Problem Solving I

Practice! p. 82, #2
4

0
f2(x) - 2f4(x)

-2

-4
3 2
( 1 – 2 )x + ( – 6 + 10 )x + ( 12 – 14 )x + ( – 8 + 6 )
3 2
-6 = – x + 4x – 2x – 2

-8

-10
0 0.5 1 1.5 2 2.5 3 3.5 4
x

• For #5 we must evaluate ( f 4 ( x ) ) ⁄ ( x – 1 ) on [0, 4]


» [q_f5,r_f5] = deconv(a_f4,[1 -1]); % Divide the polys
» q_f5
q_f5 =
1 -4 3 % The quotient is a quadratic
» r_f5
r_f5 =
0 0 0 0 % Note there is no remainder!
» f_p5 = polyval(q_f5,x);
» plot(x,f_p5)
» title('Practice! p. 82, #5','fontsize',18)
» ylabel('f4(x)/(x-1)','fontsize',16)
» xlabel('x','fontsize',16)

Chapter 3: Polynomial Functions 3–18


ECE 1010 ECE Problem Solving I

Practice! p. 82, #5
3

2.5 2 We Plot
x – 4x + 3
3 2 This
2 x–1 x – 5x + 7x – 3
3 2
x –x
2
f4(x)/(x-1)

1.5
– 4x + 7x – 3
2
– 4x + 4x
1
3x – 3
3x – 3
0.5
0
0

-0.5

-1
0 0.5 1 1.5 2 2.5 3 3.5 4
x

Roots of Polynomials
• A problem related to evaluating polynomials is solving for
the roots of a polynomial
• Recall that the roots of y = f ( x ) are those values of x where
f( x) = 0
• For polynomials with real number coefficients, the roots may
be real numbers and/or pairs of complex conjugate numbers
– For a third-order polynomial the roots may be either three
real roots or one real root and a complex pair
– For a second-order polynomial the roots may be either two
real roots or a complex conjugate pair

Chapter 3: Polynomial Functions 3–19


ECE 1010 ECE Problem Solving I

• If we plot f ( x ) we find that the real roots correspond to those


locations where the polynomial crosses the x-axis
2
• Recall that for a quadratic y ( x ) = a 0 x + a 1 x + a 2 the
roots are (quadratic formula)
2
– a 1 ± a 1 – 4a 0 a 2
r 1, r 2 = -------------------------------------------
2a 0
2
Note: The roots are complex if 4a 0 a 2 > a 1
• Hand calculation of polynomial roots becomes impractical as
the order increases, hence we often turn to a numerical solu-
tion
• The MATLAB function roots(a) finds the roots of a poly-
nomial with coefficient vector a
4 3 2
Example: f ( x ) = 2x + 5x + 10x + 7x + 6
» p = [2 5 10 7 6];
» r = roots(p)
r =
-1.0000 + 1.4142i
-1.0000 - 1.4142i
-0.2500 + 0.9682i
-0.2500 - 0.9682i
» polyval(p,r)% Check by evaluating p
% at the root locations
ans = 1.0e-013 *
-0.1243 + 0.0089i
-0.1243 - 0.0089i
-0.0533 + 0.0355i
-0.0533 - 0.0355i % Functional values are small

Chapter 3: Polynomial Functions 3–20


ECE 1010 ECE Problem Solving I

• Given the roots of a polynomial 1, r 2, …, r Nwe know that

N N–1
f ( x ) = a0 x + a2 x + … + aN – 1 x + aN
= ( x – r 1 ) ( x – r 2 )… ( x – r N )

• The MATLAB function poly(r) effectively reconstructs


the polynomial coefficient vector, a, given the vector of roots
by repeated polynomial multiplication
Example: Continue the previous example
» poly(r)
ans =
1.0000 2.5000 5.0000 3.5000 3.0000
• What happened?
– The poly() function must assume that a 0 is normalized
to unity
– In the earlier example a 0 = 2 , so to get the correct
answer we multiply by two following the poly() opera-
tion, e.g.,
» 2*poly(r)
ans =
2.0000 5.0000 10.0000 7.0000 6.0000

Examples: Practice! p. 86, #4


Given
5 4 3 2
g 4 ( x ) = x – 3x – 10x + 27x + 10x – 24

Chapter 3: Polynomial Functions 3–21


ECE 1010 ECE Problem Solving I

Determine the real roots of g 4 ( x ) and then plot the polynomial


over the appropriate interval to verify that the polynomial crosses
the x-axis at the real root locations
» a_g4 = [1 -3 -11 27 10 -24];
» roots(a_g4)' % Use transpose to make output compact
ans =
4.0000 -3.0000 2.0000 1.0000 -1.0000» x =
-4:(5+4)/100:5; % Plot 101 points
» g4 = polyval(a_g4,x);
» plot(x,g4);grid
» title('Practice! p. 86, #4','fontsize',18)
» ylabel('g4(x)','fontsize',16)
» xlabel('x','fontsize',16)

Practice! p. 86, #4
600

400

200

0
g4(x)

-200

-400

-600

-800
-4 -3 -2 -1 0 1 2 3 4 5
x

Chapter 3: Polynomial Functions 3–22

You might also like