Chapter 2: Numeric, Cell, and Structure Arrays Topics Covered: Vectors Definition Addition Multiplication Scalar, Dot, Cross Matrices Row, Column, Square Transpose Addition Multiplication Scalar-Matrix, Matrix-Matrix Element-by-Element Operations Multiplication, Division, Exponentiation, Vectorized Functions Array Addressing
Vectors A Scalar is a number that has magnitude but no direction (Temperature reading on a thermometer, pressure reading on a pressure gage) A Vector has both magnitude and direction. Vectors are used by engineers to represent position, velocity, acceleration, force, torque, angular velocity, angular acceleration, linear momentum, angular momentum, heat flux, magnetic flux, etc.
The Vector a is composed of three Cartesian Components: a = a x i + a y j + a z k i, j, and k are Unit Vectors in the x, y, z directions. The Length of a Unit Vector is one. The Length of the Vector a is called its Magnitude: a = a 2 x + a 2 y + a2 z Vectors are represented in MATLAB as: a = a x, a y, a z or a x a y a z
Open up a new MATLAB Script File:
Vector Addition: Add the corresponding Components of the two Vectors: a + b = a x, a y, a z + b x, b y, b z a + b = (a x +b x ), (a y + b y ), (a z + b z ) a b = (a x b x ), (a y b y ), (a z b z ) Modify and run the Script File as follows:
Vector Multiplication Scalar Vector: Distribute the Scalar to each Component of the Vector: c a = ca x, ca y, ca z Modify and run the Script File as follows:
Vector Dot Product Vector Vector: Sum of the products of the corresponding Components of the two Vectors. a b = a b cos θ = Scalar a b = b a = a x b x + a y b y + a z b z Modify and run the Script File as follows:
Vector Cross Product Vector Vector: Results in a Vector Normal (Perpendicular) to the plane defined by the two vectors being crossed. a b = ( a b sin θ) n = Vector n is the Unit Vector Normal to the Plane. a b = b a = i j k a x a y a z b x b y b z Determinant Method a b = a y b z a z b y i a x b z a z b x j + a x b y a y b x k
Modify and run the Script File as follows:
Matrices A Matrix is a rectangular Array of numbers arranged in Rows and Columns. The individual numbers in a Matrix are called Elements. A = A 11 A 12 A 13 A 21 A 22 A 23 A 31 A 32 A 33 Row 1 Column 1 A Row Column : A 23 = Number in the Second Row, Third Column In MATLAB, use a Comma to separate Columns; use a Semi-Colon to separate Rows: A = [A 11, A 12, A 13 ; A 21, A 22, A 23 ; A 31, A 32, A 33 ]
Modify and run the Script File as follows:
Row Vector: A Matrix with one Row, multiple Columns B = B 11, B 12, B 13 Column Vector: A Matrix with one Column, multiple Rows C = C 11 C 21 C 31 In MATLAB, create a Column Vector by using Semi-Colons to separate Rows C = C 11 ; C 12 ; C 13 Square Matrix: Number of Rows = Number of Columns D = D 11 D 12 D 21 D 22
A Vector can be Transposed by switching the Rows and Columns. A = A 11 A 12 A 13 A 21 A 22 A 23 A T = A 31 A 32 A 33 A 11 A 21 A 31 A 12 A 22 A 32 A 13 A 23 A 33 Modify and run the Script File as follows:
Matrix Addition: The Size of the two Matrices must be the same: 2 2 + 2 2 = (2 2) (Two Rows Two Columns) D + F = D 11 D 12 D 21 D 22 + F 11 F 12 F 21 F 22 D + F = (D 11+F 11 ) (D 12 +F 12 ) (D 21 +F 21 ) (D 22 +F 22 ) Modify and run the Script File as follows:
Scalar Matrix Multiplication: The Scalar is Distributed to all of the Elements of the Matrix: ca = ca 11 ca 12 ca 13 ca 21 ca 22 ca 23 ca 31 ca 32 ca 33 Modify and run the Script File as follows:
Matrix Matrix Multiplication: The Size of the two Matrices Can Be the Same: 2 2 2 2 = (2 2) Modify and run the Script File as follows: D F = D 11 D 12 D 21 D 22 F 11 F 12 F 21 F 22 D F = (D 11F 11 + D 12 F 21 ) (D 11 F 12 + D 12 F 22 ) (D 21 F 21 + D 22 F 11 ) (D 21 F 12 + D 22 F 22 )
Matrix Matrix Multiplication: The Sizes of the two Matrices Can Be Different: The Inner Sizes Must be the Same; the Outer Sizes Can be Different. Inner Sizes 3 2 2 3 = (3 3) Outer Sizes K L = K 11 K 12 K 21 K 22 K 31 K 32 L 11 L 12 L 13 L 21 L 22 L 23 = K 11 L 11 + K 12 L 21 K 11 L 12 + K 12 L 22 K 11 L 13 + K 12 L 23 K 21 L 11 + K 22 L 21 K 21 L 12 + K 22 L 22 K 21 L 13 + K 22 L 23 K 31 L 11 + K 32 L 21 K 31 L 12 + K 32 L 22 K 31 L 13 + K 32 L 23
Modify and run the Script File as follows: What Happens when you Reverse the Order of Multiplication? Try it!
2 3 3 2 = (2 2) L K = L 11 L 12 L 13 L 21 L 22 L 23 K 11 K 12 K 21 K 22 K 31 K 32 = L 11K 11 + L 12 K 21 + L 13 K 31 L 11 K 12 + L 12 K 22 + L 13 K 32 L 21 K 11 + L 22 K 21 + L 23 K 31 L 21 K 12 + L 22 K 22 + L 23 K 32 In General, Inner Sizes Must be Equal n m m p = (n p) Outer Sizes Can Be Different
Another method to calculate the Dot Product of two vectors is to use Matrix Multiplication: B = B 11, B 12, B 13 C = C 11, C 12, C 13 B C = B 11 B 12 B 13 Verify this using MATLAB! C 11 C 21 C 31 Let: B = 1, 2, 3 C = 4, 5, 6
Element Element Multiplication: Element-by-Element Multiplication is often used in engineering calculations. It is defined only for arrays that have the same size. D. F = D 11 D 12 D 21 D 22. F 11 F 12 F 21 F 22 D. F = (D 11F 11 ) (D 12 F 12 ) (D 21 F 21 ) (D 22 F 22 )
Element Element Division: Element-by-Element Division is also defined only for arrays that have the same size. D./F = D 11 D 12 D 21 D 22./ F 11 F 12 F 21 F 22 D./F = (D 11/F 11 ) (D 12 /F 12 ) (D 21 /F 21 ) (D 22 /F 22 )
Element Element Exponentiation: Element-by-Element Exponentiation is also defined only for arrays that have the same size. D. ^F = D 11 D 12 D 21 D 22. ^ F11 F 12 F 21 F 22 D. ^F = (D 11^F 11 ) (D 12^F 12 ) (D 21^F 21 ) (D 22^F 22 )
Vectorized Functions: When you use functions like sin(x), atan(x) and exp(x) on vectors, the results of the functions become vectors also. These are called Vectorized Functions. When multiplying or dividing these functions, you must use Element-by-Element Operations.
Vectorized Functions:
Vectorized Functions:
Vectorized Functions:
Vectorized Functions:
Vectorized Functions:
Array Addressing Now that you know how to create arrays, you need to be able to use specific elements, or rows, or columns in calculations. This is called Array Addressing. The Colon operator allows us to select individual elements, rows, columns, or parts of arrays.
Problem 2.1: a. Use two methods to create the vector x having 100 regularly spaced values starting at 5 and ending at 28. b. Use two methods to create the vector x having a regular spacing of 0.2 starting at 2 and ending at 14. Start by looking at a simpler scenario, where three regularly spaced values are desired (n = 3): 5 28 Δx x = 28 5 3 1 = 11.5 5 + 11.5 = 16.5 16.5 + 11.5 = 28 In General, x = x max x min n 1
Problem 2.1: a. Use two methods to create the vector x having 100 regularly spaced values starting at 5 and ending at 28. b. Use two methods to create the vector x having a regular spacing of 0.2 starting at 2 and ending at 14.