Engr/Math/Physics 25 Chp2 MATLAB Arrays: Part-2 Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Learning Goals Learn to Construct 1D Row and Column Vectors Create MULTI-Dimensional ARRAYS and MATRICES Perform Arithmetic Operations on Vectors and Arrays/Matrices Analyze Polynomial Functions Engineering/Math/Physics 25: Computational Methods 2 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Last Time Vector/Array Scalar Multiplication by Term-by-Term Scaling >> r = [ 7 11 19]; >> v = 2*r v = 14 22 38 Vector/Array Addition & Subtraction by Term-byTerm Operations w rv • “Tip-toTail” Geometry Engineering/Math/Physics 25: Computational Methods 3 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Scalar-Array Multiplication Multiplying an Array B by a scalar w produces an Array whose elements are the elements of B multiplied by w. 8 33.3 29.6 9 3.7 12 14 44.4 51.8 Using MATLAB >> B = [9,8;-12,14]; >> 3.7*B ans = 33.3000 29.6000 -44.4000 51.8000 Engineering/Math/Physics 25: Computational Methods 4 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Array-Array Multiplication Multiplication of TWO ARRAYS is not nearly as straightforward as Scalar-Array Mult. MATLAB uses TWO definitions for Array-Array multiplication: 1. ARRAY Multiplication – also called element-by-element multiplication 2. MATRIX Multiplication (see also MTH6) DIVISION and EXPONENTIATION must also be CAREFULLY defined when dealing with operations between two arrays. Engineering/Math/Physics 25: Computational Methods 5 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Element-by-Element Operations Symbol Operation Form Example + Scalar-array addition A + b [6,3]+2=[8,5] - Scalar-array subtraction A – b [8,3]-5=[3,-2] + Array addition A + B [6,5]+[4,8]=[10,13] - Array subtraction A – B [6,5]-[4,8]=[2,-3] .* Array multiplication A.*B [3,5].*[4,8]=[12,40] ./ Array right division A./B [2,5]./[4,8]=[2/4,5/8] = [0.5,0.625] .\ Array left division A.\B [2,5].\[4,8]=[2\4,5\8] = [2,1.6] A.^B [3,5].^2=[3^2,5^2] 2.^[3,5]=[2^3,2^5] [3,5].^[2,4]=[3^2,5^4] .^ Array exponentiation Engineering/Math/Physics 25: Computational Methods 6 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Array-Array Operations Array or Element-by-Element multiplication is defined ONLY for arrays having the SAME size. The definition of the product x.*y, where x and y each have n×n elements: x.*y = [x(1)y(1), x(2)y(2), ... , x(n)y(n)] if x and y are row vectors. For example, if x = [2, 4, – 5], y = [– 7, 3, – 8] then z = x.*y gives z 2 7, 43, 5 8 14, 12, 40 Engineering/Math/Physics 25: Computational Methods 7 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Array-Array Operations cont If u and v are column vectors, the result of u.*v is a column vector. The Transpose operation z = (x’).*(y’) yields Note that x’ is a 2 7 14 column vector with size z 43 12 3 × 1 and thus does not have the same size as 5 8 40 y, whose size is 1 × 3 Thus for the vectors x and y the operations x’.*y and y.*x’ are NOT DEFINED in MATLAB and will generate an error message. Engineering/Math/Physics 25: Computational Methods 8 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Array-Array Operations cont The array operations are performed between the elements in corresponding locations in the arrays. For example, the array multiplication operation A.*B results in an array C that has the same size as A and B and has the elements cij = aij bij . For example, if 11 5 A 9 4 7 8 Then C = A.*B B Yields 6 2 11 7 58 77 40 C 9 6 4 2 54 8 Engineering/Math/Physics 25: Computational Methods 9 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Array-Array Operations cont The built-in MATLAB functions such as sqrt(x) and exp(x) automatically operate on array arguments to produce an array result of the same size as the array argument x • Thus these functions are said to be VECTORIZED Some Examples >> r = [7 11 19]; >> h = sqrt(r) h = 2.6458 3.3166 4.3589 Engineering/Math/Physics 25: Computational Methods 10 >> u = [1,2,3]; >> f = exp(u) f = 2.7183 7.3891 20.0855 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Array-Array Operations cont However, when multiplying or dividing these functions, or when raising them to a power, we must use element-by-element dot (.) operations if the arguments are arrays. To Calc: z = (eu sinr)•cos2r, enter command >> z = exp(u).*sin(r).*(cos(r)).^2 z = 1.0150 -0.0001 2.9427 MATLAB returns an error message if the size of r is not the same as the size of u. The result z will have the same size as r and u. Engineering/Math/Physics 25: Computational Methods 11 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Array-Array DIVISION The definition of array division is similar to the definition of array multiplication except that the elements of one array are divided by the elements of the other array. Both arrays must have the same size. The symbol for array right division is ./ Recall r = [ 7 11 19] then z = r./u gives >> z = r./u z = 7.0000 5.5000 Engineering/Math/Physics 25: Computational Methods 12 u = [1,2,3] z 7 1 11 2 19 3 7 5.5 6.333 6.3333 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Array-Array DIVISION cont. Consider 24 20 A 9 4 4 5 B 3 2 24 4 20 5 6 4 Taking C C = A./B yields 9 3 4 2 3 2 A = 24 -9 20 4 -4 3 5 2 B = Engineering/Math/Physics 25: Computational Methods 13 >> A./B ans = -6 -3 4 2 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Array EXPONENTIATION MATLAB enables us not only to raise arrays to powers but also to raise scalars and arrays to ARRAY powers. Use the .^ symbol to perform exponentiation on an element-by-element basis • if x = [3, 5, 8], then typing x.^3 produces the array [33, 53, 83] = [27, 125, 512] We can also raise a scalar to an array power. For example, if p = [2, 4, 5], then typing 3.^p produces the array [32, 34, 35] = [9, 81, 243]. Engineering/Math/Physics 25: Computational Methods 14 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Array to Array Power >> A = [5 6 7; 8 9 8; 7 6 5] A = 5 6 7 8 9 8 7 6 5 >> B = [-4 -3 -2; -1 0 1; 2 3 4] B = -4 -3 -2 -1 0 1 2 3 4 >> C = A.^B C = 0.0016 0.0046 0.1250 1.0000 49.0000 216.0000 Engineering/Math/Physics 25: Computational Methods 15 0.0204 8.0000 625.0000 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Matrix-Matrix Multiplication Multiplication of MATRICES requires meeting the CONFORMABILITY condition The conformability condition for multiplication is that the COLUMN dimensions (k x m) of the LEAD matrix A must be EQUAL to the ROW dimension of the LAG matrix B (m x n) 6 2 If 9 8 A 10 3 B Then 5 12 4 7 AB C C is 3x2 Engineering/Math/Physics 25: Computational Methods 16 BA Error Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Matrix-Mult. Mechanics Multiplication of A (k x m) and B (m x n) CONFORMABLE Matrices produces a Product Matrix C with Dimensions (k x n) The elements of C are the sum of the products of like-index Row Elements from A, and Column Elements from B; to whit 6 2 69 2 5 68 212 64 24 9 8 75 116 C 10 3 10 9 3 5 10 8 3 12 5 12 4 7 49 7 5 48 712 1 116 Engineering/Math/Physics 25: Computational Methods 17 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Matrix-Vector Multiplication A Vector and Matrix May be Multiplied if they meet the Conformability Critera: (1xm)*(mxp) or (kxm)*(mx1) • Given Vector-a, Matrix-B, and aB; Find Dims for all c11 b11 b12 b13 aB a11 a12 c b21 b22 b23 c12 c13 a11b11 a12b21 a11b12 a12b22 a11b13 a12b23 Then the Dims: a(1x2), B(2x3), c(1x3) Engineering/Math/Physics 25: Computational Methods 18 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Summation Notation Digression Greek letter sigma (Σ, for sum) is another convenient way of handling several terms or variables – The Definition q sum x p x1 x2 x3 xq 2 xq 1 xq p 1 For the previous example c 11 c12 c13 a11b11 a12b21 a11b12 a12b22 a11b13 a12b23 a p 1 b 1 p p1 Engineering/Math/Physics 25: Computational Methods 19 2 2 2 a p 1 b 1p p2 a p 1 b 1 p p3 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Matrix Mult by Σ-Notation In General the c11 c product of 21 Conformable AB C Matrices A & B when A k m B m n ck 1 c12 c22 ck 2 c1n c2 n ckn Then Any Element, cij, of Matrix C for • i = 1 to k (no. Rows) j = 1 to n (no. Cols) p m p 7 p 1 p 1 cij aipbpj e.g.; c53 a5 p bp 3 Engineering/Math/Physics 25: Computational Methods 20 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Matrix Mult Example >> A = [3 1.7 -7; 8.1 -0.31 4.6; -1.2 2.3 0.73; 4 -.32 8; 7.7 9.9 -0.17] A = 3.0000 8.1000 -1.2000 4.0000 7.7000 1.7000 -0.3100 2.3000 -0.3200 9.9000 -7.0000 4.6000 0.7300 8.0000 -0.1700 A is Then 5x3 Engineering/Math/Physics 25: Computational Methods 21 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Matrix Mult Example cont >> B = [0.67 -7.6; 4.4 .11; -7 -13] >> C = A*B C = B = 0.6700 4.4000 -7.0000 -7.6000 0.1100 -13.0000 B is Then 3x2 Engineering/Math/Physics 25: Computational Methods 22 58.4900 68.3870 -28.1370 -121.3941 4.2060 -0.1170 -54.7280 -134.4352 49.9090 -55.2210 Result, C, is Then 5x2 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Matrix-Mult NOT Commutative Matrix multiplication is generally not commutative. That is, AB ≠ BA even if BA is conformable • Consider an Illustrative Example 1 A 3 2 0 & 4 6 1 B 7 10 26 1 1 27 12 13 AB 3 0 4 6 3 1 4 7 24 25 01 13 02 14 3 4 BA 6 1 7 3 6 2 7 4 27 40 Engineering/Math/Physics 25: Computational Methods 23 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Commutation Exceptions Two EXCEPTIONS to the NONcommutative property are the NULL or ZERO matrix, denoted by 0 and the IDENTITY, or UNITY, matrix, denoted by I. • The NULL matrix contains all ZEROS and is NOT the same as the EMPTY matrix [ ], which has NO elements. Commutation of the Null & Identity Matrices 0A A0 0 IA AI A Strictly speaking 0 & I are always SQUARE Engineering/Math/Physics 25: Computational Methods 24 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Identity and Null Matrices Identity Matrix is a 1 0 0 square matrix and also it 1 0 0 1 0 etc. or . is a diagonal matrix with 0 1 1’s along the diagonal 0 0 1 • similar to scalar “1” Null Matrix is one in which all elements are 0 • similar to scalar “0” Both are “idempotent” Matrices: for A = 0 or I → Engineering/Math/Physics 25: Computational Methods 25 0 0 0 0 0 0 0 0 0 A AT and A A 2 A3 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt eye and zeros Use the eye(n) command to Form an nxn Identity Matrix Use the zeros(mxn) to Form an mxn 0-Filled Matrix • Strictly Speaking a NULL Matrix is SQUARE Engineering/Math/Physics 25: Computational Methods 26 >> I = eye(5) I = 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 >> Z24 = zeros(2,4) Z24 = 0 0 0 0 0 0 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt 0 0 PolyNomial Mult & Div Function conv(a,b) computes the product of the two polynomials described by the coefficient arrays a and b. The two polynomials need not be the same degree. The result is the coefficient array of the product polynomial. function [q,r] = deconv(num,den) produces the result of dividing a numerator polynomial, whose coefficient array is num, by a denominator polynomial represented by the coefficient array den. The quotient polynomial is given by the coefficient array q, and the remainder polynomial is given by the coefficient array r. Engineering/Math/Physics 25: Computational Methods 27 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt PolyNomial Mult Example Find the PRODUCT for 3 2 f x 2x 7 x 9x 6 >> f = [2 -7 9 -6]; >> g = [13,-5,3]; >> prod = conv(f,g) prod = 26 -101 158 -144 2 g x 13x 5x 3 57 -18 prod x 26 x5 101x 4 158x3 144 x 2 57 x 18 Engineering/Math/Physics 25: Computational Methods 28 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt PolyNomial Quotient Example Find the QUOTIENT 2 x3 7 x 2 9 x 6 quot 13x 2 5 x 3 >> f = [2 -7 9 -6]; >> g = [13,-5,3]; >> [quot1,rem1] = deconv(f,g) quot1 = 0.1538 -0.4793 rem1 = 0 0.0000 6.1420 quot x 0.1538x 0.4973 Engineering/Math/Physics 25: Computational Methods 29 -4.5621 rem 6.142 x 4.5621 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt PolyNomial Roots The function roots(h) computes the roots of a polynomial specified by the coefficient array h. The result is a column vector that contains the polynomial’s roots. >> r = roots([2, 14, 20]) r = 2 2 x 14 x 20 0 x 5 x 2 -5 -2 >> rf = roots(f) >> rg = roots(g) rf = rg = 2.0000 0.1923 + 0.4402i 0.7500 + 0.9682i 0.1923 - 0.4402i 0.7500 - 0.9682i Engineering/Math/Physics 25: Computational Methods 30 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Plotting PolyNomials The function polyval(a,x)evaluates a polynomial at specified values of its independent variable x, which can be a matrix or a vector. The polynomial’s coefficients of descending powers are stored in the array a. The result is the same size as x. Plot over (−4 ≤ x ≤ 7) the function f x 2 x 7 x 9 x 6 3 Engineering/Math/Physics 25: Computational Methods 31 2 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt The Demo Plot Engineering/Math/Physics 25: Computational Methods 32 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt All Done for Today Cell-Arrays & Structure Arrays Engineering/Math/Physics 25: Computational Methods 33 Not Covered in Chapter 2 • §2.6 = Cell Arrays • §2.7 = Structure Arrays Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Engr/Math/Physics 25 Appendix Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engineering/Math/Physics 25: Computational Methods 34 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Chp2 Demo x = linspace(-4,7,20); p3 = [2 -7 9 -6]; y = polyval(p3,x); plot(x,y, x,y, 'o', 'LineWidth', 2), grid, xlabel('x'),... ylabel('y = f(x)'), title('f(x) = 2x^3 - 7x^2 + 9x - 6') f(x) = 2x 3 - 7x 2 + 9x - 6 500 400 300 y = f(x) 200 100 0 -100 -200 -300 -4 -2 Engineering/Math/Physics 25: Computational Methods 35 0 2 x 4 6 8 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt Chp2 Demo >> f = [2 -7 9 -6]; >> x = [-4:0.02:7]; >> fx = polyval(f,x); >> plot(x,fx),xlabel('x '),ylabel('f(x)'), title('chp2 Demo'), grid Engineering/Math/Physics 25: Computational Methods 36 Bruce Mayer, PE BMayer@ChabotCollege.edu • ENGR-25_Arrays-2.ppt