Numerical Analysis

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

NUMERICAL ANALYSIS LAB

NS-321L
LECTURE – 3: POLYNOMIALS
Polynomial

• Degree : ??

• For help abut polynomials in matlab, type help polyfun

2
Polynomials in MATLAB
• MATLAB provides a number of functions for the manipulation
of polynomials. These include,
• Evaluation of polynomials
• Finding roots of polynomials
• Addition, subtraction, multiplication, and division of polynomials
• Dealing with rational expressions of polynomials
• Curve fitting

Polynomials are defined in MATLAB as row vectors made up of the


coefficients of the polynomial, whose dimension is n+1, n being the degree
of the polynomial

p = [1 -12 0 25 116] represents x4 - 12x3 + 25x + 116

3
Roots
• >>p = [1, -12, 0, 25, 116]; % 4th order polynomial
>>r = roots(p)
r =
11.7473
2.7028
-1.2251 + 1.4672i
-1.2251 - 1.4672i
• From a set of roots we can also determine the polynomial
>>pp = poly(r)
r =
1 -12 -1.7764e-014 25 116

4
Exercise

5x^12−2x^6+x^5−198x+1 degree :12

x^4−x^3+x^2−x+1 degree :4

56x^23 degree :23

5x−7 degree :1

−8 degree :0
Addition and Subtraction
• MATLAB does not provide a direct function for adding or subtracting
polynomials unless they are of the same order, when they are of the same
order, normal matrix addition and subtraction applies, d = a + b and e = a – b
are defined when a and b are of the same order.
• When they are not of the same order, the lesser order polynomial must be
padded with leading zeroes before adding or subtracting the two polynomials.
>>p1=[3 15 0 -10 -3 15 -40];
>>p2 = [3 0 -2 -6];
>>p = p1 + [0 0 0 p2];
>>p =
3 15 0 -7 -3 13 -46
The lesser polynomial is padded and then added or subtracted as appropriate.

6
Exercise

• Add (𝟔𝟔𝟔𝟔𝟓𝟓 − 𝟏𝟏𝟏𝟏𝒙𝒙𝟒𝟒 − 𝟔𝟔𝟔𝟔𝟑𝟑 − 𝟏𝟏𝟏𝟏𝟔𝟔𝟔𝟔𝟐𝟐 + 𝟒𝟒𝟒𝟒𝟒𝟒 − 𝟗𝟗𝟗𝟗) 𝒕𝒕𝒕𝒕 (𝟏𝟏𝒙𝒙𝟐𝟐 − 𝟗𝟗𝟗𝟗 + 𝟓𝟓)
>> p1 = [6 -10 -6 -106 45 -99]
>> p2 = [0 0 0 1 -9 5] (Padding!!!)
>> p1 + p2

MATRIX DIMENSIONS MUST AGREE!!!!

• Subtract (𝟓𝟓𝟓𝟓𝟑𝟑 − 𝟗𝟗𝒙𝒙𝟐𝟐 − 𝟑𝟑𝟑𝟑𝒙𝒙𝟑𝟑 − 𝟗𝟗𝒙𝒙𝟐𝟐 + 𝒙𝒙 − 𝟑𝟑) 𝒕𝒕𝒕𝒕 (𝒙𝒙𝟐𝟐 − 𝒙𝒙 + 𝟏𝟏)


Multiplication
• Polynomial multiplication is supported by the conv function. For the two
polynomials
a(x) = x3 + 2x2 + 3x + 4
b(x) = x3 + 4x2 + 9x + 16
>>a = [1 2 3 4];
>>b = [1 4 9 16];
>>c = conv(a,b)
c =
1 6 20 50 75 84 64
or c(x) = x6 + 6x5 + 20x4 + 50x3 + 75x2 + 84x + 64

8
Exercise

• 4x^2(x^2−6^x+2)

• (3x+5)(x−10)

• (4x^4−x)(6−3x)

• (3x^5+7y)(x^4−2y)

• (2x+3)(x^2−x+1)
Multiplication II

• Couple observations
• Multiplication of more than two polynomials
requires repeated use of the conv function.
• Polynomials need not be of the same order to use
the conv function.
• Remember that functions can be nested so
conv(conv(a,b),c) makes sense.
Examples
• (3x+5)(x−10)(4x^4−x)(
• (3x^5+7y)(x^4−2y)(x^2−x+1)

10
Division
• Division takes care of the case where we want to divide one polynomial
by another, in MATLAB we use the deconv function.
• In general polynomial division yields a quotient polynomial and a
remainder polynomial. Let’s look at two cases;
• Case 1: suppose f(x)/g(x) has no remainder;
>>f=[2 9 7 -6];
>>g=[1 3];
>>[q,r] = deconv(f,g)
q=
2 3 -2 q(x) = 2x2 + 3x -2
r =
0 0 0 0 r(x) = 0

11
Division-II
• The representation of r looks strange, but MATLAB outputs r
padding it with leading zeros so that length(r) = length of f, or
length(f).
• Case 2: now suppose f(x)/g(x) has a remainder,
>>f=[2 -13 0 75 2 0 -60];
>>g=[1 0 -5];
>>[q,r] = deconv(f,g)
q=
2 -13 10 10 52 q(x) = 2x4 - 13x3 + 10x2 + 10x + 52
r=
2 – 6 -12 r(x) = -2x2 – 6x -12

12
Exercise

• (x^2−6^x+2)/(4x^2)

• (3x+5)/(x−10)

• (4x^4−x)/(6−3x)

• (3x^5+7y)/(x^4−2y)

• (x^2−x+1)/ (2x+3)
Evaluation
• MATLAB provides the function polyval to evaluate polynomials. To use polyval
you need to provide the polynomial to evaluate and the range of values where
the polynomial is to be evaluated. Consider,
>>p = [1 4 -7 -10];
To evaluate p at x=5, use
>> polyval(p,5)
To evaluate for a set of values,
>>y = polyval(p,[x1,x2,x3….]);

Exercise
What's the value of 5x2 – 3x – 4 when x = -1?
What's the value of x4 – 16 when x = 2,6,7?
Evaluate – x4 +2x2 for x = 3, -2?

14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy