Download presentation
Presentation is loading. Please wait.
Published byBrett Lawrence Modified over 9 years ago
1
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engr/Math/Physics 25 Chp2 MATLAB Arrays: Part-2
2
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 2 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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
3
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 3 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Last Time Vector/Array Scalar Multiplication by Term-by-Term Scaling Vector/Array Addition & Subtraction by Term-by- Term Operations “Tip-toTail” Geometry >> r = [ 7 11 19]; >> v = 2*r v = 14 22 38
4
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 4 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Scalar-Array Multiplication Multiplying an Array B by a scalar w produces an Array whose elements are the elements of B multiplied by w. >> B = [9,8;-12,14]; >> 3.7*B ans = 33.3000 29.6000 -44.4000 51.8000 Using MATLAB
5
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 5 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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.
6
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 6 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Element-by-Element Operations SymbolOperation FormExample + 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].^ Array exponentiation A.^B [3,5].^2=[3^2,5^2] 2.^[3,5]=[2^3,2^5] [3,5].^[2,4]=[3^2,5^4]
7
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 7 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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: if x and y are row vectors. For example, if x.*y = [x(1)y(1), x(2)y(2),..., x(n)y(n)] x = [2, 4, – 5], y = [– 7, 3, – 8] then z = x.*y gives
8
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 8 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 column vector with size 3 × 1 and thus does not have the same size as 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.
9
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 9 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 c ij = a ij b ij. For example, if Then C = A.*B Yields
10
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 10 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 >> u = [1,2,3]; >> f = exp(u) f = 2.7183 7.3891 20.0855
11
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 11 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Array-Array Operations cont must 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 = (e u sinr)cos 2 r, 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.
12
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 12 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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./ Recallr = [ 7 11 19]u = [1,2,3] then z = r./u gives >> z = r./u z = 7.0000 5.5000 6.3333
13
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 13 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Array-Array DIVISION cont. Consider >> A./B ans = -6 4 -3 2 Taking C = A./B yields A = 24 20 -9 4 B = -4 5 3 2
14
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 14 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 [3 3, 5 3, 8 3 ] = [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 [3 2, 3 4, 3 5 ] = [9, 81, 243].
15
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 15 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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.0204 0.1250 1.0000 8.0000 49.0000 216.0000 625.0000
16
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 16 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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) If Then
17
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 17 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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
18
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 18 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 Then the Dims: a(1x2), B(2x3), c(1x3)
19
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 19 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Summation Notation Digression Greek letter sigma (Σ, for sum) is another convenient way of handling several terms or variables – The Definition For the previous example
20
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 20 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Matrix Mult by Σ-Notation In General the product of Conformable Matrices A & B when Then Any Element, c ij, of Matrix C for i = 1 to k (no. Rows)j = 1 to n (no. Cols) e.g.;
21
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 21 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 1.7000 -7.0000 8.1000 -0.3100 4.6000 -1.2000 2.3000 0.7300 4.0000 -0.3200 8.0000 7.7000 9.9000 -0.1700 A is Then 5x3
22
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 22 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Matrix Mult Example cont >> B = [0.67 -7.6; 4.4.11; -7 -13] B = 0.6700 -7.6000 4.4000 0.1100 -7.0000 -13.0000 B is Then 3x2 >> C = A*B C = 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
23
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 23 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Matrix-Mult NOT Commutative Matrix multiplication is generally not commutative. That is, AB ≠ BA even if BA is conformable Consider an Illustrative Example
24
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 Strictly speaking 0 & I are always SQUARE
25
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 25 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Identity and Null Matrices Identity Matrix is a square matrix and also it is a diagonal matrix with 1’s along the diagonal 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 →
26
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 26 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods eye and zeros Use the eye( n ) command to Form an nxn Identity Matrix >> I = eye(5) I = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 Use the zeros( mxn ) to Form an mxn 0-Filled Matrix Strictly Speaking a NULL Matrix is SQUARE >> Z24 = zeros(2,4) Z24 = 0 0 0 0 0 0 0 0
27
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 27 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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.
28
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 28 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods PolyNomial Mult Example Find the PRODUCT for >> f = [2 -7 9 -6]; >> g = [13,-5,3]; >> prod = conv(f,g) prod = 26 -101 158 -144 57 -18
29
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 29 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods PolyNomial Quotient Example Find the QUOTIENT >> 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 -4.5621
30
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 30 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 = -5 -2 >> rf = roots(f) rf = 2.0000 0.7500 + 0.9682i 0.7500 - 0.9682i >> rg = roots(g) rg = 0.1923 + 0.4402i 0.1923 - 0.4402i
31
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 31 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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
32
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 32 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods The Demo Plot
33
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 33 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods All Done for Today Not Covered in Chapter 2 §2.6 = Cell Arrays §2.7 = Structure Arrays Cell-Arrays & Structure Arrays
34
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 34 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engr/Math/Physics 25 Appendix
35
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 35 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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')
36
BMayer@ChabotCollege.edu ENGR-25_Arrays-2.ppt 36 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.