The Hong Kong Polytechnic University Industrial Centre 1 MatLAB Lesson 4 : Polynomial Edward Cheung email: [email protected] Room W311g 2008
Jan 02, 2016
The Hong Kong Polytechnic UniversityIndustrial Centre
1
MatLABLesson 4 : Polynomial
Edward Cheung
email: [email protected]
Room W311g
2008
2
Polynomial
12
12
31
21 ...)( nnnnnn axaxaxaxaxaxf
•A polynomial is a function and can be written as follows:-
Where
f(x) is a function of x
The degree or order of a polynomial is n, the highest power of x.
ai = 1, 2,…,n, n+1 are coefficients of the polynomial
3
Operations On Polynomials
• A polynomial can be described with a row vector in MatLab
• The coefficients become the elements of the vector starting from the highest power of x
• Interested operations are:- Get roots from polynomial Get polynomial from root Perform arithmetic operations Addition, subtraction, multiplication, division Visualisation
4
Root Finding Functions
>>help polyfun Functions for Interpolation and polynomials.
• roots() – roots of polynomial function
• fzero() – find root for continous function of single variable Usage :- x = fzero(fun,x0,options) fzero looks for a point where fun change sign and defines a
zero as a point where the function crosses the x-axis. and not applicable for zeros of even multiplicity
5
Example
• Example Calculate zero of cosine between 0 & ->> x=fzero(@cos,[0,-pi])x = -1.5708 % -pi/2>> fzero('(x-1)^2',0) % root of even multiplicity search may fail
% returns NaN = not a number% a value or symbol produced as a result of invalid input operands. E.g. 0/0, inf/inf
6
Polynomial Roots
• Consider the following cubic equation
67)( 3 xxxp
>>p=[1 0 -7 6]; % i/p coefficient row vectorr=roots(p) % find roots for p(x)r = % o/p root as a column vector -3.0000 2.0000 1.0000
% r are the values of x that make p(x)=0
7
Find Imaginary Roots
>> r2=roots([1 -6 18 -30 25])r2 = 1.0000 + 2.0000i 1.0000 - 2.0000i 2.0000 + 1.0000i 2.0000 - 1.0000i
2530186)( 234 xxxxxp
Solve:-
8
Coefficient of Polynomial from Roots
• p=Poly(r) Return a row vector p such that the elements of p are
the coefficients of a polynomial with roots given by r>> r=[-4 -2 -1 1];p=poly(r)p = 1 6 7 -6 -8
8676)( 234 xxxxxp
9
Characteristic Polynomial
• For any square matrix A, we can write:-
is called the eigenvalue of A (eigenvert in German means own value or characteristic value)
x is called the eigenvector (x≠0)
• p() Is called the characteristic polynomial of A
• Eigenvalues of matrix A can be computed by solving the equation
xAx
11 ...)()(
)(
nnn cccp
AI
0xAI
0)( p
10
Example on Eigenvalues
• Suppose we want to find the eigenvalues of a matrix X
>> X=[8 -4;2 2]; px=poly(X) %Find characteristic polynomial of Xpx = 1 -10 24>> roots(px) % solve for px give eigenvalues of Xans = 6.0000 4.0000>> eig(X) % use eigenvalue function on X ans = % same result as above 6 4
11
Polynomial Evaluation
>> p=[1 1 -2 3];polyval(p,3)ans =
33
32)( 23 xxxxp
3333233)3( 23 p
>> x=-5:0.1:5;y=polyval(p,x);plot(x,y)grid
Example:- Plot & evaluate the following polynomial at x=3.
12
Polynomial Arithmetic
• Addition & subtraction of polynomial follows the addition and subtraction of vectors You should check the order of matrix and patch zero on
missing terms to avoid error caused by unequal length
• Multiplication of polynomial by a scalar is just scalar multiplication
• Multiplication of two polynomials can be done by the convolve function; conv()
• Division of two polynomials can be done by the deconvolve function; deconv()
13
Convolution & Deconvolution
>> u=[1 2 3]; % u(s) definedv=[4 5 6]; % v(s) definedw=conv(u,v) % multiple u(s) & v(s)w = 4 13 28 27 18>> [q,r]=deconv(w,v) % q=quotient, r=remainderq = 1 2 3r = 0 0 0 0 0
654)(
32)(2
2
sssv
sssu
Consider the following polynomials:-
14
Partial Fraction Expansion
• [r,p,k] = residue(b,a) finds the residues, poles, and direct term of a partial fraction expansion of the ratio of two polynomials, b(s) and a(s), of the form:
• [b,a] = residue(r,p,k) converts the partial fraction expansion back to the polynomials with coefficients in b and a.
• If there are no multiple roots, then:
• If pj=…=p(j+m-1) is a pole of multiplicity m, then the expansion include terms of the form
nn
mm
sasasaa
sbsbsbb
sa
sb
1
23
121
12
31
21
...
...
)(
)(
sn
n kps
r
ps
r
ps
r
sa
sb
...
)(
)(
2
2
1
1
mj
mj
j
j
j
j
ps
r
ps
r
ps
r
)(...
)(1
2
1
15
Example on Partial Fraction Expansion
>> b=[4 -40 98];>> a=[1 -15 74 -120];>> [r,p,k]=residue(b,a)r = 1.0000 2.0000 1.0000p = 6.0000 5.0000 4.0000k = []
1207415
9840423
2
sss
ss
4
1
5
2
6
1
sss
The direct term coefficient vector k is empty because length(a) > length(b)
16
Example on Partial Fraction Expansion (Cont.)
• Given r,p & k, convert the partial fraction expression to factional polynomial.
>> [b2,a2]=residue(r,p,k)b2 = 4 -40 98a2 = 1 -15 74 -120
1207415
98404
)(
)(23
2
2
2
sss
ss
sa
sb
17
Data Analysis & Curve Fitting
• Curve fitting is a technique to model data with an equation
• Better than interpolation
• In curve fitting, one need to find a smooth curve to best fit the data. Best fit is interpreted as minimizing the sums of squared errors at the data points. (least squares method)>> eqn=polyfit(X,Y,N) Fits a polynomial of degree N to data described by the vector
X & Y, where X is the independent variable. Returns a row vector of length N+1 as the coefficients in the order of descending power
18
Example on Linear Regression
>> x=0.1:0.1:10; % create data xy=5+1.5*x; % create data yy1=y+randn(1,100); % perturbation y1plot(x,y1,'*')
19
Example on Linear Regression (cont.)
eq=polyfit(x,y1,1)% eq = 1.4868 5.1147>> y2=polyval(eq,x);plot(x,y1,'x',x,y2,'m')legend('data','fit')
The data can beDescribed by the polynomial
1147.54868.11 xy
20
Example on Non-linear Regression
• Fit non-linear data with higher order polynomial
>> x=0:0.2:1.4;y=cos(x);plot(x,y,'x')
21
Example on Non-linear Regression (cont.)
>> eqn1=polyfit(x,y,2)% eqn1 =% -0.3664 -0.0924 1.0081>> x1=0:0.01:1.4;>> y1=polyval(eqn1,x1);>> hold on>> line1=plot(x1,y1,'g');
22
Example on Non-linear Regression (cont.)
>> delete(line1)>> eqn2=polyfit(x,y,3)% eqn2 =% 0.1040 -0.5848 0.0220 0.9994>> y2=polyval(eqn2,x1);>> line2=plot(x1,y2,'g');
23
3-D Line Plots
>> t=-3*pi:0.1:3*pi;>> plot3(t,sin(t),cos(t))>> grid on
24
3-D Line Plots (cont.)
Different Views>> view([1,0,0])>> view([0,1,0])>> view([0,0,1])
25
Surface Mesh Plots
• If z=f(x,y) represents a surface on xyz axes
• First use [X,Y] = meshgrid(x,y) to generate a grid of points in the xy plane and then evaluate the function z
• If x=[xmin:spacing:xmax] & y=[ymin:spacing:ymax], meshgrid will generate the coordinates of a rectangular grid with one corner at (xmin, ymin) and the opposite corner at (xmax, ymax).
• [X,Y] = meshgrid(x) is the same as [X,Y] = meshgrid(x,x) Usage>> [X,Y]=meshgrid(min:spacing:max);
• Calculate z & use mesh(X,Y,Z) to plot the surface
• Not desirable to use too small spacing because of difficulty for visualize and X,Y matrices can become too large
26
Example – meshgrid generation
>> [X,Y]=meshgrid(-2:2)X = -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2Y = -2 -2 -2 -2 -2 -1 -1 -1 -1 -1 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2
plot(X,Y,'rx'),axis([-3 3 -3 3])
27
Mesh Plot Example
>>Z=sin(sqrt(X.^2+Y.^2)+eps)./(sqrt(X.^2+Y.^2)+eps);
mesh(X,Y,Z)
22
22 )sin(),(
YX
YXYXf
28
Mesh Plot Examples
>> [X,Y]=meshgrid(-2:0.2:2);Z=sin(sqrt(X.^2+Y.^2)+0.0001)./(sqrt(X.^2+Y.^2)+0.0001);mesh(X,Y,Z)
>> [X,Y]=meshgrid(-2:0.1:2);>> Z=X.*exp(-((X-Y.^2).^2+Y.^2));>> mesh(X,Y,Z),xlabel('x'),ylabel('y'),zlabel('z')
])[( 222 yyxxez
29
Contour, Mesh & Surface Plots
>> [X,Y]=meshgrid(-2:0.2:2);Z=sin(X).*sin(Y).*exp(-X.^2-Y.^2);contour(X,Y,Z,10)
30
Other 3-D Plots
• contour(x,y,z) – creates a contour plot
• mesh(x,y,z) – creates a 3D mesh surface plot
• meshc(x,y,z) – same as mesh but draws a contour plot under the surface
• meshz(x,y,z) – same as mesh but draws a series of vertical reference lines under the surface
• surf(x,y,z) – creates a shaded 3D mesh surface plot
• surfc(x,y,z) –same as surf but draws a contour plot under the surface
• waterfall(x,y,z) – same as mesh but draw mesh lines in one direction only
31
Waterfall Plot Example