%%%%%%% Lesson 3: Working with Complex Numbers and Polynomials % % 1) Complex Numbers: operations and special related functions % 2) Polynomials: MATLAB representation and special related functions %___________________________________________________________________________ % Complex numbers: MATLAB interprets the letter i to represent sqrt(-1) % PROVIDED THE USER HASN"T DEFINED IT AS SOMETHING ELSE %___________________________________________________________________________ sqrt(-1) ans = 0 + 1.0000i s1 = 3 + 4i %Defining complex numbers s1 = 3.0000 + 4.0000i s2 = 5 - 12j; %Can use j also -- MATLAB caters to electrical engineers 8-) %____________________________________________ %MATLAB does "everything" with complex numbers as with real!!! A = [s1 j; j s2] %Generate matrix of complex entries A = 3.0000 + 4.0000i 0 + 1.0000i 0 + 1.0000i 5.0000 -12.0000i %can do s1 + s2, s1-s2, etc.. Ainv = inv(A) %Matrix inverse Ainv = 0.1176 - 0.1581i 0.0037 - 0.0147i 0.0037 - 0.0147i 0.0294 + 0.0699i %____________________________________________ %MATLAB also provides special functions for complex numbers a = real([s1 s2]); b=imag([s1 s2]);%extract real and imaginary part a = b= 3 5 4 -12 r = abs([s1 s2]) %calculate magnitude -- element-wise r = 5 13 alpha = angle([s1; s2]) %calculate phase angle in rad -- element-wise alpha = 0.9273 -1.1760 %_________________________________________________________ %%%%%%%%%%%%%%% Polynomials %MATLAB represents polynomials by a vector of its coefficients %in order of descending powers p1 = [1 4 4] %Represents the polynomial s^2 + 4s + 4 p2 = [2 0 0 -1] %Represents the polynomial 2s^3 +0s^2 + 0s - 1 %____________________________________________ %MATLAB also provides special functions for polynomials r2 = roots(p2) %three roots of third-order polynomial -- via "roots" r2 = -0.3969 + 0.6874i -0.3969 - 0.6874i 0.7937 %____________________________________________ p3 = poly(r2) %Coefficients of polynomial with given set of roots p3 = 1.0000 0.0000 0 -0.5000 a = polyval(p3,5) %Evaluate polynomial p3 at s = 5 a = 124.5000 %____________________________________________ %Coefficients of product of two polynomials p4 = conv([1 1],[1 1]) %(s+1)*(s+1) = s^2 + 2s + 1 p4 = 1 2 1 poly2str(p4,'s') %display polynomial as equation in variable s ans = s^2 + 2 s + 1 %____________________________________________ %We use polynomials primarily to express system functions num = [1 0]; den = [1 1 5]; printsys(num,den,'s') %Show system function in "pretty" form num/den = s ------------ s^2 + s + 5 %____________________________________________________________________________