MATLAB Polynomials Nafees Ahmed Asstt Professor EE Deptt
MATLAB Polynomials Nafees Ahmed Asstt. Professor, EE Deptt DIT, Dehra. Dun
Introduction Ø Polynomials x 2+2 x-7 x 4+3 x 3 -15 x 2 -2 x+9 Ø In MATLAB polynomials are created by row vector i. e. s 4+3 s 3 -15 s 2 -2 s+9 >>p=[ 1 3 -15 -2 9]; 3 x 3 -9 >>q=[3 0 0 -9] %write the coefficients of every term Ø Polynomial evaluation : polyval(c, s) Exp: Evaluate the value of polynomial y=2 s 2+3 s+4 at s=1, -3 >>y=[2 3 4]; >>s=1; >>value=polyval(y, s) >>value = 9
Polynomials Evaluation Ø Similarly >>s=-3; >>value=polyval(y, s) >>value = 13 OR >>s=[1 -3]; >> value=polyval(y, s) value = 9 13 OR >> value=polyval(y, [1 -3])
Roots of Polynomials Roots of polynomials: roots(p) >>p=[1 3 2]; >>r=roots(p) r = -2 -1 % p=s 2+3 s+2 Try this: find the roots of s 4+3 s 3 -15 s 2 -2 s+9=0
Polynomials mathematics • Addition >>a=[0 1 2 1]; >>b=[1 0 1 5]; >>c=a+b • Subtraction >>a=[3 0 0 2]; >>b=[0 0 1 7]; >>c=b-a c= -3 0 1 %s 2+2 s+1 % s 3+s+1 %s 3+s 2+3 s+6 %s 3+2 %s+7 %-s 3+s+5 5
Polynomials mathematics • Multiplication : Multiplication is done by convolution operation. Sytnax z= conv(x, y) >>a=[1 2 ]; %s+2 >>b=[1 4 8 ]; % s 2+4 s+8 >>c=conv(a, b) % s 3+6 s 2+16 s+16 c= 1 6 16 16 Try this: find the product of (s+3), (s+6) & (s+2). Hint: two at a time • Division : Division is done by deconvolution operation. Syntax is [z, r]=deconv(x, y) Where x=divident vector y=divisor vector z=Quotients vector r=remainders vector
Polynomials mathematics >>a=[1 6 16 16]; >>b=[1 4 8]; >>[c, r]=deconv(a, b) c= 1 2 r= 0 0 Try this: divide s 2 -1 by s+1 %a=s 3+6 s 2+16 s+16 %b=s 2+4 s+8
Formulation of Polynomials • Making polynomial from given roots: >>r=[-1 -2]; %Roots of polynomial are -1 & -2 >>p=poly(r); %p=s 2+3 s+2 p= 1 3 2 • Characteristic Polynomial/Equation of matrix ‘A”: =det(s. I-A) >>A=[0 1; 2 3]; >>p=poly(A) %p= determinant (s. I-A) p= %p=s 2 -3 s-2 1 -3 -2
Polynomials Differentiation & Integration Polynomial differentiation : syntax is dydx=polyder(y) >>y=[1 4 8 0 16]; %y=s 4+4 s 3+8 s 2+16 >>dydx=polyder(y) %dydx=4 s 3+12 s 2+16 s dydx= 4 12 16 0 Polynomial integration : syntax is x=polyint (y, k) %k=constant of integration OR x=polyint(y) %k=0 >>y=[4 12 16 1]; %y=4 s 3+12 s 2+16 s+1 >>x=polyint(y, 3) %x=s 4+4 s 3+8 s 2+s+3 x= 1 4 8 1 3(this is k)
Polynomials Curve fitting In case a set of points are known in terms of vectors x & y, then a polynomial can be formed that fits the given points. Syntax is c=polyfit(x, y, k) %k is degree of polynomial Ex: Find a polynomial of degree 2 to fit the following data X 0 1 2 4 Y 1 6 20 100 Sol: >>x=[0 1 2 4]; >>y=[1 6 20 100]; >>c=polyfit(x, y, 2) c= 7. 3409 -4. 8409 1. 6818 >>c=polyfit(x, y, 3) c= 1. 0417 1. 3750 2. 5833 %2 nd degree polynomial %3 rd degree polynomial 1. 0000
Polynomials Curve fitting Ex: Find a polynomial of degree 1 to fit the following data Current 10 15 20 25 30 voltage 100 150 200 250 300 Sol: >>current=[10 15 20 25 30]; >>voltage=[100 150 200 250 300]; >>resistance=polyfit(current, voltage, 1) resistance= 10. 0000 -0. 0000 i. e. Voltage = 10 x Current
Polynomials Evaluation with matrix arguments Ex: Evaluate the matrix polynomial X 2+X+2, given that the square matrix X= 2 3 4 5 Sol: >>A=[1 1 2]; >>X=[2 3; 4 5]; >>Z=polyvalm(A, X) Z= 20 24 32 44 %A= X 2+X+2 I %poly+val(evaluate)+m(matix)
- Slides: 13