Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Array Operations in MATLAB 1.Types of Matrix arithmetic 2.Dot operators Mathematical.

Similar presentations


Presentation on theme: "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Array Operations in MATLAB 1.Types of Matrix arithmetic 2.Dot operators Mathematical."— Presentation transcript:

1 ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Array Operations in MATLAB 1.Types of Matrix arithmetic 2.Dot operators Mathematical Operations with Arrays : Chapter 3

2 ENG 1181 2 Types of Matrix Arithmetic There are two types of matrix arithmetic Matrix math is used in Linear Algebra to solve simultaneous equations (MATLAB text Ch. 3.2 and 3.3 - beyond the scope of this course) We will only introduce element-by-element arithmetic, e.g. If A = [a b c] and B = [x y z], then A + B = [a+x b+y c+z]

3 ENG 1181 3 Types of array arithmetic There are only six cases where matrix math and element-by-element arithmetic differ A*B A/B A^B A^s s^A s/A For these cases, the standard operator has been chosen to represent matrix arithmetic, so we need a new symbol for element-by element arithmetic (dot operators) A.*B A./B A.^B A.^s s.^A s./A If A and B are matrices and s is a scalar,

4 ENG 1181 4 60- 63 The vectors must be the same size. Element-by-element operations for row vectors: If:a = [a1 a2 a3] and b = [b1 b2 b3] Then: a.* b = [a 1 * b 1 a 2 * b 2 a 3 * b 3 ] a./ b = [a 1 / b 1 a 2 / b 2 a 3 / b 3 ] a.^ b = [a 1 ^b 1 a 2 ^b 2 a 3 ^b 3 ] a.^2 = [a 1 ^2 a 2 ^2 a 3 ^2] 2./a = [2/a 1 2/a 2 2/a 3 ] Examples of Dot Operators

5 ENG 1181 5 Element-by-element operations for matrices: Then: Given:and 60- 63 Examples of Dot Operators

6 ENG 1181 6 MATRIX ELEMENT-BY-ELEMENT EXAMPLES >> A = [1, 2, 3; 4, 5, 6] A = 1 2 3 4 5 6 >> B = [1, 2, -1; 2, -2, 5] B = 1 2 -1 2 -2 5 >> A.* B ans = 1 4 -3 8 -10 30 >> A./ B ans = 1.0000 1.0000 -3.0000 2.0000 -2.5000 1.2000 >> B.^ 3 ans = 1 8 -1 8 -8 125 60- 63

7 ENG 1181 7 Dot Operators If you forget a dot, when one is required, you will either get a cryptic error message (involving matrix dimensions) or you will get the wrong answer For the following operations: s+A, s-A, A-s, A+B, A-B, s*A, A/s matrix math is the same as element-by- element arithmetic, so dot operators are optional.

8 ENG 1181 8 SCALAR-ARRAY ADDITION AND SUBTRACTION >> M = M = 1 2 3 4 5 6 7 8 9 >> a = s + M a = 3 4 5 6 7 8 9 10 11 >> b = s - M b = 1 0 -1 -2 -3 -4 -5 -6 -7 >> c = M - s c = -1 0 1 2 3 4 5 6 7 52- 53 The scalar operates on each element in the array. For example, given the following matrix M and the scalar s = 2 we would get the following results for addition and subtraction

9 ENG 1181 9 SCALAR-ARRAY MULTIPLICATION AND DIVISION the scalar operates on each element in the array For example, given the same matrix M and scalar s = 2 we would get the following results for multiplication and division >> M = M = 1 2 3 4 5 6 7 8 9 >> d = s * M d = 2 4 6 8 10 12 14 16 18 >> d = M / s d = 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 52- 53

10 ENG 1181 10 ARRAY-ARRAY ADDITION AND SUBTRACTION The arrays being used in the operation must have the same size. The sum (difference) of two arrays is obtained by adding (subtracting) corresponding array elements, resulting in a new array of the same size. >> A = [1 2; 3 4] A = 1 2 3 4 >> B = [1 -1; 2 0] B = 1 -1 2 0 >> C = A + B C = 2 1 5 4 >> A - B ans = 0 3 1 4 52- 53

11 ENG 1181 11 CALCULATING THE VALUE OF A FUNCTION For the function: calculate y for z = 1, 3, 5, 7, 9, 11, 13 62- 63 SOLUTION USING MATLAB: >> z = [1: 2: 15] z = 1 3 5 7 9 11 13 Create a vector z with seven elements. Where do you have operations between vectors that need a dot operator? >> y = (z.^ 3 + 5 * z)./ (4 * z.^ 2 - 10) y = -1.0000 1.6154 1.6667 2.0323 2.4650 2.9241 3.3964

12 ENG 1181 12 Element-by-Element Calculations NOTE: Even though the calculation is written only one time in the previous example, MATLAB iterates through all elements of z (Element-by-Element) and calculates a corresponding number of elements of solution array y.

13 ENG 1181 13 SOME USEFUL BUILT-IN ARRAY FUNCTIONS MATLAB has many built-in functions that can be used with arrays. Some of them are (A is a vector): max(A)Returns the largest element in A min(A)Returns the smallest element in A mean(A)Returns the average value of the elements in A sum(A)Returns the sum of the elements of A length(A)Returns the number of elements in A sort(A)Sorts the elements of A The Help window gives information about many other functions. 64- 65

14 ENG 1181 14 EXAMPLES OF BUILT IN FUNCTIONS >> sum(A) ans = 48 >> length(A) ans = 6 >> sort(A) ans = 2 5 8 9 10 14 >> A = [8 2 9 5 14 10] A = 8 2 9 5 14 10 >> max(A) ans = 14 >> min(A) ans = 2 >> mean(A) ans = 8 64- 65

15 ENG 1181 15 APPLICATION Element-by-element calculations are useful in processing data and in calculating the value of a mathematical function at many points. EXAMPLE: The coefficient of friction μ is determined by measuring the force F required to move a mass μ: μ = F / (mg) g = 9.81 m/s 2 The results from measuring F in five tests are given in the table. Determine the coefficient of friction in each test, and the average from all tests. m Ffriction 69 Mass: m (kg)245102050 Force: F (N)12.523.23061116294

16 ENG 1181 16 >> mass = [2 4 5 10 20 50]; >> force = [12.5 23.2 30 61 116 294]; >> mu = force./ (9.81 * mass) mu = 0.6371 0.5912 0.6116 0.6218 0.5912 0.5994 >> mu_ave = mean(mu) meu_ave = 0.6087 COEFFICIENT OF FRICTION USING MATLAB 69 Create the mass vector. Create the force vector Calculate mu for each mass-force pair, using element-by-element calculations. Determine the average of the elements in the vector mu by using the function mean().


Download ppt "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 Array Operations in MATLAB 1.Types of Matrix arithmetic 2.Dot operators Mathematical."

Similar presentations


Ads by Google