0% found this document useful (0 votes)
10 views

Lecture8 Polynomials

The document discusses polynomials, including defining polynomials as vectors in MATLAB, using built-in commands like polyval, roots, poly, conv, and deconv, and curve fitting with polyfit. Key topics covered include representing polynomials as vectors, evaluating polynomials, finding roots, adding, multiplying, and dividing polynomials.

Uploaded by

ceyda.duztas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture8 Polynomials

The document discusses polynomials, including defining polynomials as vectors in MATLAB, using built-in commands like polyval, roots, poly, conv, and deconv, and curve fitting with polyfit. Key topics covered include representing polynomials as vectors, evaluating polynomials, finding roots, adding, multiplying, and dividing polynomials.

Uploaded by

ceyda.duztas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Lecture 8

Polynom als

Polynomials
A polynomial (many terms) s a function that has the following
general form of summation of terms.

where
a0 , .... , an are the coefficients,
x is the unknown scalar variable,
n s the degree of polynomial.

2
Using ezplot command
to plot polynomial functions

The ezplot command (easy plot) draws a symbol c funct on y = f(x),


for x values between -2π and +2π by default.

Degree of Example polynomial Plotting with


polynomial functions Matlab ezplot command

1 y = x+1 ezplot ('x + 1')

2 y = x2 + x + 1 ezplot ('x^2 + x + 1')

3 y = x3 + x 2 + x + 1 ezplot ('x^3 + x^2 + x + 1')

4 y = x4 + x 3 + x 2 + x + 1 ezplot ('x^4 + x^3 + x^2 + x + 1')

Example Plots of 1st, 2nd, 3rd, 4th Degree Polynomials

y=x+1 y = x2 + x + 1

ezplot ('x ^ 3')

y = x3 + x2 + x + 1 y = x4 + x3 + x2 + x + 1

4
Defining Polynomials as Vectors in Matlab

 In Matlab, polynom als can be def ned as normal vectors.


 All of the coefficients ( nclud ng zeros) of polynom al terms are wr tten n vector.
 The terms should be wr tten from left-to-r ght n decreas ng order of degrees.
 Length of the vector s also the degree of polynom al.

Example polynomial Matlab vector representations


functions (Conta ns the coefficients only)

y = 8x + 3 p = [8 3]
y = x2 –
4x + 10 p = [1 -4 10]
y = 6x2 - 150 p = [6 0 -150]
y = x5 + 6x2 - 7x p = [1 0 0 6 -7 0]

Built-in Polynomial Commands

Matlab Command Description

polyval (p, x) Polynomial evaluation for a g ven value

roots (p) Find roots of a polynomial

poly (r) G ve the polynomial with specified roots

conv (p1, p2) Multiply polynomials

deconv (pnum, pdenom) Divide polynomials

polyder (p) Polynomial derivative

polyfit (x, y, ndegree) Polynomial curve fitting

6
Calculating Value of a Polynomial
for an x value

 The value of a polynomial at a point can be calculated


with the polyval command that has the form

polyval(p, x)

 P is a vector with the coefficients of the polynomial


 X can be a scalar number,
or a variable that has an assigned value,
or a computable expression,
can also be a vector

Example : Us ng the polyval command


for a scalar value
For the polynomial function f(x) = x3 + 3x2 - 5x - 17

We want to calculate f (5).

>> p = [1 3 -5 -17];

>> polyval(p, 5)

ans =

158

8
Example : Us ng the polyval command
for a vector
 x s a vector, polyval command returns a correspond ng y vector.
 Program plots the x and y vectors.

f(x) = x3 + 3x2 - 5x - 17
p = [1 3 -5 -17];
x = -5 : 0.1 : 5;
y = polyval(p, x);

% Plotting x and y vectors:


plot(x,y)

grid
xlabel('x')
ylabel('y')

Alternat ve plott ng method:


ezplot ('x^3 + 3*x^2 - 5*x - 17')
9

Finding the roots of a polynomial

 The roots command n Matlab can be used to f nd the roots of a polynomial.


 They are the x values for which the polynomial y function is equal to zero.
r = roots(p)

Example: f(x) = x3+3x2-5x-17 Example: f(x) = 4x2+10x-8


>> p = [1 3 -5 -17]; >> roots( [4 10 -8] )

>> r = roots( p ) ans =


-3.1375
r = 0.6375
2.3186
-2.6593 + 0.5099i Two roots found.
-2.6593 - 0.5099i
Three roots found.
Two of them are complex numbers.
10
Determining coefficients of a polynomial
When the roots of a polynomial are known, the poly command
can be used for determining the coefficients of the polynomial.
p = poly(r)

p is a vector conta n ng the coefficients of the polynomial.


r is a vector conta n ng the roots of the polynomial.

Example:
>> r = [-2 2];

>> p = poly(r)

p =
1 0 -4

Correspond ng polynom al s :
f(x) = x2 - 4
11

Add t on and Subtract on of Polynom als


 Two polynomials can be added and substracted
by adding the vectors of the coefficients.
 If orders are not same, we should add zeros (padd ng)
for the m ss ng terms.
 Example:

>> p1 = [3 15 0 -10 -3 15 -40];


>> p2 = [3 0 -2 -6];

>> p1 + p2
??? Error using ==> plus
Matrix dimensions must agree.

>> p1 + [0 0 0 p2]
ans =
3 15 0 -7 -3 13 -46
12
Multiplication of Polynom als
 Two polynomials can be multiplied with the conv (convolut on) command
which has the follow ng form:
c = conv(a, b)

 c is a vector of the coefficients of the polynomial that is the product


of the multiplication.
 a and b are the vectors of the polynomials that are being multiplied.
 The two polynomials do not have to be of the same order.

13

Multiplication Example

p1 p2
polynom al polynom al

>> p1 = [2 3 1];
>> p2 = [5 -2];

>> p3 = conv(p1, p2)

p3 =
10 11 -1 -2
x3 x2 x1 x0
14
Division of Polynom als

Suppose all of the follow ngs are polynomials.

N numerator
D denominator (d v sor) = +
Q quotient (result)
R remainder

A polynomial can be d v ded by another with


the deconv (deconvolution) command.

[Q R] = deconv(N, D)

15

Division Example

D v de 2x3+9x2+7x-6 by x+3

>> u = [2 9 7 -6];
>> v = [1 3];

>> [a b] = deconv(u , v)

a =
2 3 -2 Result of d v s on

b =
0 0 0 0 Rema nder

16
Finding Derivatives of Polynomials
The polyder command s used to f nd the der vat ve of a polynomial.

Or g nal polynom al: >> f = [3 -2 4];


f(x)= 3x2-2x+4
>> k = polyder(f)
Der vat ve of or g nal:
f’(x)= 6x-2 k =
6 -2

17

Curve Fitting
 When N points (xi, yi) are given , we can f nd a polynomial funct on of
degree N-1 that passes through the points.
 The procedure to find a polynomial vector is called Curve Fitting.

Number of
Polynom al funct on Curve F tt ng Type
known points

F rst degree polynom al f tt ng


2 y = ax + b
(L near L ne f tt ng)

3 y = ax2 + bx + c Second degree polynom al f tt ng

4 y = ax3 + bx2 + cx + d Th rd degree polynom al f tt ng

18
The polyfit command
 The polyfit command (Polynom al F t) finds the coefficients of a
polynomial that approx mately represents the data (x and y vectors).
p = polyfit(x, y, n)

 p is the vector of the coefficients of the polynomial that fits the data

 x is a vector with the horizontal coordinate

 y is a vector with the vertical coordinate

 n is the degree of the polynomial


If n = 1 then it is called as Linear Regression
If n > 1 then it is called as Polynomial Regression

19

The polyval command


 The polyval command (Polynom al Values) uses the coefficients
to find new values of y, that correspond to the known values of x.

newy = polyval (coefficients, x);

 Before constructing a polynomial with the polyval command,


it is a good idea to plot the g ven x and y data.

 Plott ng w ll show the d str but on of data,


so we can est mate the degree of polynom al.

20
Example Problem

 Suppose the follow ng (x, y) data po nts are g ven.


(0.9 , 0.9)
(1.5 , 1.5)
(3 , 2.5)
(4 , 5.1)
(6 , 4.5)
(8 , 4.9)
(9.5 , 6.3)

 Wr te a Matlab program to f nd a polynom al (th rd degree)


that s fitted to these po nts by using the polyfit command.
 Also plot the original points as scattered drawing, and
the fitted polynomial as normal drawing.

21

Matlab Program
X = [0.9 1.5 3.0 4.0 6.0 8.0 9.5];
y = [0.9 1.5 2.5 5.1 4.5 4.9 6.3];
katsayilar = polyfit(x, y, 3)

xp = 0.9 : 0.1 : 9.5;


% xp is new vector

yp = polyval(katsayilar, xp);
% yp is new vector

plot(x,y,'o', xp,yp)
grid
xlabel('x, xp')
ylabel('y, yp')
legend('Given data', 'Fitted data')

katsayilar =
Screen
output 0.0220 -0.4005 2.6138 -1.4158
x3 x2 x1 x0 22
Or g nal Data (Scattered) and F tted Curve
(Th rd degree polyf t)

y = 0.0220x3 - 0.4005x2 + 2.6138x - 1.4158


The green curve represents the th rd
degree polynom al funct on found.

The blue dots represent the or g nal
data values (x and y).

23

Finding a Linear Function


(y = a.x + b)

• We can find the linear function that fits the data by using:
p = polyfit(x, y, 1)

• The first element p(1) will be a (slope of l near l ne).


• The second element p(2) will be b.

24
Example Problem
 Suppose that to calibrate an instrument, measurements have been done.
 The following table shows the measurement readings against the standard values.
 Wr te a Matlab program to apply a f rst-degree polynom al f t.
 Also use the results to plot a linear line.

Standard Measured
values values
0 0.5030
1 0.7229
2 0.7802
5 1.2106
10 1.7607
15 2.4649

25

Matlab Program
• First-degree polynom al f t means f nd ng a l near function: y = ax+b
• We w ll calculate the a and b coeff c ents.

t = [0 1 2 5 10 15];
m = [0.5030 0.7229 0.7802 1.2106 1.7607 2.4649];

% F rst-degree polyf t
p = polyfit(t, m, 1)

x = 0 : 0.1 : 15 ;
y = polyval(p, x) ;

plot(t,m,'o', x,y)

title('Calibration curve')
xlabel('True values')
ylabel('Measured values')

26
Screen Outputs
Screen output

p =

0.1267 0.5435

Slope (a) of l near l ne

y = ax + b
y = 0.1267x + 0.5435

The green l ne represents the
l near funct on found.

The blue dots represent the
or g nal data values (x and y).
27

You might also like