Lecture8 Polynomials
Lecture8 Polynomials
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
y=x+1 y = x2 + x + 1
y = x3 + x2 + x + 1 y = x4 + x3 + x2 + x + 1
4
Defining Polynomials as Vectors in Matlab
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]
6
Calculating Value of a Polynomial
for an x value
polyval(p, x)
>> 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);
grid
xlabel('x')
ylabel('y')
Example:
>> r = [-2 2];
>> p = poly(r)
p =
1 0 -4
Correspond ng polynom al s :
f(x) = x2 - 4
11
>> 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)
13
Multiplication Example
p1 p2
polynom al polynom al
>> p1 = [2 3 1];
>> p2 = [5 -2];
p3 =
10 11 -1 -2
x3 x2 x1 x0
14
Division of Polynom als
N numerator
D denominator (d v sor) = +
Q quotient (result)
R remainder
[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.
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
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
19
20
Example Problem
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)
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)
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
• We can find the linear function that fits the data by using:
p = polyfit(x, y, 1)
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
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