Summary

These notes provide an introduction to MATLAB, focusing on fundamental concepts like vectors and matrices. The material covers various operations and functions, such as addition, multiplication, and vectorized operations, crucial in engineering calculations using MATLAB.

Full Transcript

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 O...

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 𝑎 is composed of three Cartesian Components: 𝑎 = 𝑎𝑥 𝑖 + 𝑎𝑦 𝑗 + 𝑎𝑧 𝑘 𝑖, 𝑗, and 𝑘 are 𝐔𝐧𝐢𝐭 𝐕𝐞𝐜𝐭𝐨𝐫𝐬 in the 𝑥, 𝑦, 𝑧 directions. The Length of a Unit Vector is one. The Length of the Vector 𝑎 is called its Magnitude: 𝑎 = 𝑎𝑥2 + 𝑎𝑦2 + 𝑎𝑧2 Vectors are represented in MATLAB as: 𝑎 = 𝑎𝑥 , 𝑎𝑦 , 𝑎𝑧 or 𝑎𝑥 𝑎𝑦 𝑎𝑧 Open up a new MATLAB Script File: Vector Addition: Add the corresponding Components of the two Vectors: 𝑎 + 𝑏 = 𝑎𝑥 , 𝑎𝑦 , 𝑎𝑧 + 𝑏𝑥 , 𝑏𝑦 , 𝑏𝑧 𝑎 + 𝑏 = (𝑎𝑥 +𝑏𝑥 ), (𝑎𝑦 + 𝑏𝑦 ), (𝑎𝑧 + 𝑏𝑧 ) 𝑎 − 𝑏 = (𝑎𝑥 −𝑏𝑥 ), (𝑎𝑦 − 𝑏𝑦 ), (𝑎𝑧 − 𝑏𝑧 ) Modify and run the Script File as follows: Vector Multiplication Scalar × Vector: Distribute the Scalar to each Component of the Vector: 𝑐𝑎 = 𝑐𝑎𝑥 , 𝑐𝑎𝑦 , 𝑐𝑎𝑧 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. 𝑎 ∙ 𝑏 = 𝑎 × 𝑏 × cos 𝜃 = 𝐒𝐜𝐚𝐥𝐚𝐫 𝑎 ∙ 𝑏 = 𝑏 ∙ 𝑎 = 𝑎𝑥 𝑏𝑥 + 𝑎𝑦 𝑏𝑦 + 𝑎𝑧 𝑏𝑧 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. 𝑎 × 𝑏 = ( 𝑎 𝑏 sin 𝜃)𝑛 = 𝐕𝐞𝐜𝐭𝐨𝐫 𝑛 is the Unit Vector Normal to the Plane. 𝑖 𝑗 𝑘 Determinant 𝑎 × 𝑏 = − 𝑏 × 𝑎 = 𝑎𝑥 𝑎𝑦 𝑎𝑧 Method 𝑏𝑥 𝑏𝑦 𝑏𝑧 𝑎 × 𝑏 = 𝑎𝑦 𝑏𝑧 − 𝑎𝑧 𝑏𝑦 𝑖 − 𝑎𝑥 𝑏𝑧 − 𝑎𝑧 𝑏𝑥 𝑗 + 𝑎𝑥 𝑏𝑦 − 𝑎𝑦 𝑏𝑥 𝑘 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. 𝐴11 𝐴12 𝐴13 Row 1 𝐀 = 𝐴21 𝐴22 𝐴23 𝐴31 𝐴32 𝐴33 Column 1 𝐴Row−Column : 𝐴23 = Number in the Second Row, Third Column In MATLAB, use a Comma to separate Columns; use a Semi-Colon to separate Rows: 𝐀 = [𝐴11 , 𝐴12 , 𝐴13 ; 𝐴21 , 𝐴22 , 𝐴23 ; 𝐴31 , 𝐴32 , 𝐴33 ] Modify and run the Script File as follows: Row Vector: A Matrix with one Row, multiple Columns 𝐁 = 𝐵11 , 𝐵12 , 𝐵13 Column Vector: A Matrix with one Column, multiple Rows 𝐶11 𝐂 = 𝐶21 𝐶31 In MATLAB, create a Column Vector by using Semi-Colons to separate Rows 𝐂 = 𝐶11 ; 𝐶12 ; 𝐶13 Square Matrix: Number of Rows = Number of Columns 𝐷11 𝐷12 𝐃= 𝐷21 𝐷22 A Vector can be Transposed by switching the Rows and Columns. 𝐴11 𝐴12 𝐴13 𝐴11 𝐴21 𝐴31 𝐀 = 𝐴21 𝐴22 𝐴23 𝐀𝐓 = 𝐴12 𝐴22 𝐴32 𝐴31 𝐴32 𝐴33 𝐴13 𝐴23 𝐴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) 𝐷11 𝐷12 𝐹11 𝐹12 𝐃+𝐅= + 𝐷21 𝐷22 𝐹21 𝐹22 (𝐷11 +𝐹11 ) (𝐷12 +𝐹12 ) 𝐃+𝐅= (𝐷21 +𝐹21 ) (𝐷22 +𝐹22 ) Modify and run the Script File as follows: Scalar × Matrix Multiplication: The Scalar is Distributed to all of the Elements of the Matrix: 𝑐𝐴11 𝑐𝐴12 𝑐𝐴13 c𝐀 = 𝑐𝐴21 𝑐𝐴22 𝑐𝐴23 𝑐𝐴31 𝑐𝐴32 𝑐𝐴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) 𝐷11 𝐷12 𝐹11 𝐹12 𝐃×𝐅= × 𝐷21 𝐷22 𝐹21 𝐹22 (𝐷11 𝐹11 + 𝐷12 𝐹21 ) (𝐷11 𝐹12 + 𝐷12 𝐹22 ) 𝐃×𝐅= (𝐷21 𝐹11 + 𝐷22 𝐹21 ) (𝐷21 𝐹12 + 𝐷22 𝐹22 ) Modify and run the Script File as follows: 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 𝐾11 𝐾12 𝐿11 𝐿12 𝐿13 𝐊 × 𝐋 = 𝐾21 𝐾22 × 𝐿21 𝐿22 𝐿23 𝐾31 𝐾32 𝐾11 𝐿11 + 𝐾12 𝐿21 𝐾11 𝐿12 + 𝐾12 𝐿22 𝐾11 𝐿13 + 𝐾12 𝐿23 = 𝐾21 𝐿11 + 𝐾22 𝐿21 𝐾21 𝐿12 + 𝐾22 𝐿22 𝐾21 𝐿13 + 𝐾22 𝐿23 𝐾31 𝐿11 + 𝐾32 𝐿21 𝐾31 𝐿12 + 𝐾32 𝐿22 𝐾31 𝐿13 + 𝐾32 𝐿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) 𝐾11 𝐾12 𝐿11 𝐿12 𝐿13 𝐋×𝐊= × 𝐾21 𝐾22 𝐿21 𝐿22 𝐿23 𝐾31 𝐾32 𝐿11 𝐾11 + 𝐿12 𝐾21 + 𝐿13 𝐾31 𝐿11 𝐾12 + 𝐿12 𝐾22 + 𝐿13 𝐾32 = 𝐿21 𝐾11 + 𝐿22 𝐾21 + 𝐿23 𝐾31 𝐿21 𝐾12 + 𝐿22 𝐾22 + 𝐿23 𝐾32 In General, Inner Sizes Must be Equal 𝑛 × 𝑚 × 𝑚 × 𝑝 = (𝑛 × 𝑝) Outer Sizes Can Be Different Another method to calculate the Dot Product of two vectors is to use Matrix Multiplication: 𝐁 = 𝐵11 , 𝐵12 , 𝐵13 𝐂 = 𝐶11 , 𝐶12 , 𝐶13 𝐶11 𝐁 ∙ 𝐂 = 𝐵11 𝐵12 𝐵13 ∗ 𝐶21 𝐶31 Verify this using MATLAB! Let: 𝐁 = 1, 2, 3 𝐂 = 4, 5, 6 E𝐥𝐞𝐦𝐞𝐧𝐭 × E𝐥𝐞𝐦𝐞𝐧𝐭 Multiplication: Element-by-Element Multiplication is often used in engineering calculations. It is defined only for arrays that have the same size. 𝐷11 𝐷12 𝐹11 𝐹12 𝐃.× 𝐅 =.∗ 𝐷21 𝐷22 𝐹21 𝐹22 (𝐷11 𝐹11 ) (𝐷12 𝐹12 ) 𝐃.× 𝐅 = (𝐷21 𝐹21 ) (𝐷22 𝐹22 ) E𝐥𝐞𝐦𝐞𝐧𝐭 × E𝐥𝐞𝐦𝐞𝐧𝐭 Division: Element-by-Element Division is also defined only for arrays that have the same size. 𝐷11 𝐷12 𝐹11 𝐹12 𝐃./𝐅 =./ 𝐷21 𝐷22 𝐹21 𝐹22 (𝐷11 /𝐹11 ) (𝐷12 /𝐹12 ) 𝐃./𝐅 = (𝐷21 /𝐹21 ) (𝐷22 /𝐹22 ) E𝐥𝐞𝐦𝐞𝐧𝐭 × E𝐥𝐞𝐦𝐞𝐧𝐭 Exponentiation: Element-by-Element Exponentiation is also defined only for arrays that have the same size. 𝐷11 𝐷12 𝐹11 𝐹12 𝐃. ^𝐅 =.^ 𝐷21 𝐷22 𝐹21 𝐹22 (𝐷11 ^𝐹11 ) (𝐷12 ^𝐹12 ) 𝐃. ^𝐅 = (𝐷21 ^𝐹21 ) (𝐷22 ^𝐹22 ) 𝐕𝐞𝐜𝐭𝐨𝐫𝐢𝐳𝐞𝐝 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬: 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. 𝐕𝐞𝐜𝐭𝐨𝐫𝐢𝐳𝐞𝐝 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬: 𝐕𝐞𝐜𝐭𝐨𝐫𝐢𝐳𝐞𝐝 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬: 𝐕𝐞𝐜𝐭𝐨𝐫𝐢𝐳𝐞𝐝 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬: 𝐕𝐞𝐜𝐭𝐨𝐫𝐢𝐳𝐞𝐝 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬: 𝐕𝐞𝐜𝐭𝐨𝐫𝐢𝐳𝐞𝐝 𝐅𝐮𝐧𝐜𝐭𝐢𝐨𝐧𝐬: 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 (𝑛 = 3): 5 28 Δx 28 − 5 ∆𝑥 = = 11.5 3−1 5 + 11.5 = 16.5 16.5 + 11.5 = 28 𝑥max − 𝑥min In General, ∆𝑥 = 𝑛−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.

Use Quizgecko on...
Browser
Browser