%____________________________________________________________________________ %%%%%%% Lesson 2: Basic Operations: Matrix and Element-wise %____________________________________________________________________________ % 1) Matrix arithmetic and special functions for matrices % addition, subtraction, multiplication, inverse ("division"), exponent % 2) Element-wise arithmetic and special elememt-wise functions % multiplication, division, exponent, trig and log/exp functions %_____________________________________________________________________________ A1 = [1 7; -4 8]; %Generate "workspace" for lesson A2 = [-1 0; 3 2]; H = [-2 5 -7; 3 1 2]; b2 = [9; 3]; b3 = [b2; 1]; c = [2 0]; alpha = 1; %______________________________________________________ B1 = A1 + A2; B2 = A2 - A1 ; C1=A1*H; %Basic Matrix Operations B1 = B2= *********Pay attention to dimensions!!!!******************* C2 = H'*A1 %Inner dimensions agree now...phew! 8-) C2 = %______________________________________________________ v1 = A1*b2; v2 = alpha*A1*b2; %Example with scalar and vector v1 = v2= %______________________________________________________ A2inv = inv(A2) %Basic Matrix Operations -- inverse A2inv = x = inv(A1)*b2 %Solve linear equation Ax = b for variable x x = %______________________________________________________ Basic Matrix Operations -- division x = A1\b2 %Equivalent to x = inv(A1)*b2 x = y = A1\A2 %Equivalent to x = inv(A1)*A2 y = y = A1/A2 %Equivalent to x = A1*inv(A2) y = %______________________________________________________ Asquared = A1^2 %Basic Matric Operations -- exponent = A1*A1 Asquared = Aroot = A1^0.5 %Matrix square root -- via exponent operation Aroot = Aroot = sqrtm(A1) %Matrix square root -- via "sqrtm" function Aroot = %Notice complex numbers! (See lesson 3) %______________________________________________________ %Can combine operations in many ways %______________________________________________________ T = alpha*(inv(A1)*v1+c')'*H %using standard "order-of-operations" T = -13.0000 58.0000 -71.0000 %______________________________________________________ clear %Clear "workspace" -- take all variables out of memory H = [-2 5 -7; 3 1 2]; b2 = [9; 3]; b3 = [b2; 1]; c = [2 0]; alpha = 1; %______________________________________________________ C1 = A1.*A2 %Basic Element-wise Operations -- multiplication C1 = v1 = b2.*c' %Example with vectors v1 = %______________________________________________________ %Basic Element-wise Operations %______________________________________________________ M1=A1./A2 %division element-by-element M1= Melsqua = A1.^2 %Square each element Melsqua = Melroot = sqrt(A1) %Take square root of each element -- via "sqrt" Melroot = Wow = 10.^A1 %Elements are 10 raised to the power a1 Wow = 1.0e+08 * 0.0000 0.1000 0.0000 1.0000 %Note that Wow's elements can be displayed meaningfully format short e %Change format of MATLAB output Wow = 10.^A1 %Elements are 10 raised to the power a1 Wow = 1.0000e+01 1.0000e+07 1.0000e-04 1.0000e+08 %______________________________________________________ format short %Change format back to MATLAB default setting %Many familiar element-wise functions yrad = [-pi/2 0 pi/2] %Note predefined variable pi yrad = -1.5708 0 1.5708 ydeg = degrees(yrad) %Readily convert from radians to degrees ydeg = -90 0 90 Sinofy = sin(yrad) %Trigonometric functions and their inverses Sinofy = -1 0 1 ydeg0 = degrees(atan(tan(radians(ydeg)))) %Nest functions easily! ydeg0 = -90 0 90 %______________________________________________________ %More element-wise functions y = [-1 0 1] %RECALL: Could have used y = [-1:1] = linspace(-1,1,3) y = -1 0 1 Expofy = exp(y) %Logarithmic/exponential functions Expofy = 0.3679 1.0000 2.7183 Logofy = log(Expofy) %Natural logarithm (base e) Logofy = What = log10(10.^y) %Logarithm base 10 What %_____________________________________________________________________________ %_____________________________________________________________________________ %%%%%%% 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 %____________________________________________________________________________