Presentation is loading. Please wait.

Presentation is loading. Please wait.

Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.

Similar presentations


Presentation on theme: "Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays."— Presentation transcript:

1 Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays

2 Working with Arrays Chapter Outline Creating Vector and Matrix Accessing and Addressing Matrix Mathematical Operations with Matrix Functions for Analyzing Matrix Chapter Case Study

3 Working with Arrays Creating Vectors a n subdivision points b dx >> x = a:dx:b; >> x = linspace(a,b,n); >> x = x'; x row vectors convert to column vector

4 Working with Arrays Creating Vectors - Examples 1)Create a COLUMN VECTOR with the elements : 55, 14, log(51), 0, sin(pi/3) 2) Create a ROW VECTOR which the first element is 1, the last element is 33, with a increment of 2 between elements

5 Working with Arrays Creating Matrices >> A = [1,2,3; 4,5,6; 7,8,9]; >> A = [1 2 3; 4 5 6; 7 8 9]; >> A = [1 2 3 4 5 6 7 8 9] >> A = A' or data entry mode transpose

6 Working with Arrays Array Operations A = 1 2 3 4 5 6 7 8 9 B = 1 2 4 3 4 5 6 5 4 C = A + B C = 2 4 7 7 9 11 13 13 13 A = 1 2 3 4 5 6 7 8 9 B = 1 2 4 3 4 5 6 5 4 C = A.*B C = 1 4 12 12 20 30 42 40 36 Array addition Array multiplication A = 1 2 3 4 5 6 7 8 9 C = A.^2 C = 1 4 9 16 25 36 49 64 81 Array power

7 Working with Arrays Array Multiplication Matrices must have the same dimensions Dimensions of resulting matrix = dimensions of multiplied matrices Resulting elements = product of corresponding elements from the original matrices >> a = [1 2 3 4; 5 6 7 8]; >> b = [1:4; 1:4]; >> c = a.*b c = 1 4 9 16 5 12 21 32 >> a = [1 2 3 4; 5 6 7 8]; >> b = [1:4; 1:4]; >> c = a.*b c = 1 4 9 16 5 12 21 32 c(2,4) = a(2,4)*b(2,4)

8 Working with Arrays Matrix Operations A = 1 2 3 4 5 6 7 8 9 B = 2*A B = 2 4 6 8 10 12 14 16 18 A = 1 2 3 4 5 6 7 8 9 B = 2 + A B = 3 4 5 6 7 8 9 10 11 Scalar Multiplication Scalar Expansion A = 1 2 3 4 5 6 7 8 9 B = 1 2 4 3 4 5 6 5 4 C = A * B C = 25 25 26 55 58 65 85 91 104 Matrix Multiplication

9 Working with Arrays >> e=[1 2;3 4] + 5 1 2 = + 5 3 4 1 2 5 5 = + 3 4 5 5 6 7 = 8 9 >> e=[1 2;3 4] + 5 1 2 = + 5 3 4 1 2 5 5 = + 3 4 5 5 6 7 = 8 9 Matrix Calculation-Scalar Expansion >> e=[1 2;3 4] + 5 e = 6 7 8 9 >> e=[1 2;3 4] + 5 e = 6 7 8 9 Scalar expansion

10 Working with Arrays Matrix Multiplication Inner dimensions must be equal. Dimension of resulting matrix = outermost dimensions of multiplied matrices. Resulting elements = dot product of the rows of the 1st matrix with the columns of the 2nd matrix. >> a = [1 2 3;4 5 6]; >> b = [3,1;2,4;-1,2]; >> c = a*b c = 4 15 16 36 >> a = [1 2 3;4 5 6]; >> b = [3,1;2,4;-1,2]; >> c = a*b c = 4 15 16 36 [2x3] [3x2] [2x3]*[3x2] [2x2] a(2nd row). b(2nd column)

11 Working with Arrays Array Addressing m(2:4,3) m(3,1)

12 Working with Arrays More Example on indexing >> a=[3 11 6; 4 7 10; 13 9 0] a = 3 11 6 4 7 10 13 9 0 >> a(3,1)= 20 a = 3 11 6 4 7 10 20 9 0 >> a(2,3)-a(1,2) ans = >> a=[3 11 6; 4 7 10; 13 9 0] a = 3 11 6 4 7 10 13 9 0 >> a(3,1)= 20 a = 3 11 6 4 7 10 20 9 0 >> a(2,3)-a(1,2) ans = Create a 3 x 3 matrix Use square brackets [ ] Matrices must be rectangular. (Undefined elements set to zero) Assign a new value to the (3,1) element Use elements in a mathematical expression

13 Working with Arrays More Example on Colon indexing >> a=[3 11 6; 4 7 10; 13 9 0] a = 3 11 6 4 7 10 13 9 0 >> b = a(:,3) b = 6 10 0 >> c = a(2,:) c = 4 7 10 >> d = a(2:3,1:2) d = [4 7 ] [13 9 ] >> a=[3 11 6; 4 7 10; 13 9 0] a = 3 11 6 4 7 10 13 9 0 >> b = a(:,3) b = 6 10 0 >> c = a(2,:) c = 4 7 10 >> d = a(2:3,1:2) d = [4 7 ] [13 9 ] Create a 3 x 3 matrix Define a column vector b from elements in all rows of column 3 in matrix a Define a row vector c from elements in all columns of row 2 in matrix a Create a matrix d from elements in rows 2&3 and columns 1&2 in matrix a

14 Working with Arrays Solve this set of simultaneous equations Array Division using “Left Division” >> A = [-1 1 2; 3 -1 1;-1 3 4]; >> b = [2;6;4]; >> x = inv(A)*b x = 1.0000 2.0000 >> x = A\b x = 1.0000 2.0000 >> A = [-1 1 2; 3 -1 1;-1 3 4]; >> b = [2;6;4]; >> x = inv(A)*b x = 1.0000 2.0000 >> x = A\b x = 1.0000 2.0000 -x 1 + x 2 + 2x 3 = 2 3x 1 - x 2 + x 3 = 6 -x 1 + 3x 2 + 4x 3 = 4

15 Working with Arrays FunctionDescriptionExample C=max(A)If A is vector, C is the largest element in A A = [5 9 2] C = max(A) sum(A)If A is vector, returns the sum of elements of A A = [5 9 2] sum(A) sort(A)If A is vector, arranges elements of vector in ascending order A = [5 9 2] sort(A) det(A)Returns the determinant of a square matrix A A = [2 4; 3 5]; det (A) inv(A)Returns the inverse of a square matrix A A = [2 4; 3 5]; inv(A) Functions for Analyzing Matrix

16 Working with Arrays Sample Problem 2 : Friction Experiment The coefficient of friction, μ, can be determined in an experiment by measuring the force F required to move a mass m. When F is measured and m is known, the coefficient of friction can be calculated by: µ = F / (mg) where g = 9.81 m/s 2 Results from measuring F in six tests are given in the table below. Determine the coefficient of friction in each test, and the average from all tests. Test #123456 Mass m (kg)245102050 Force F (N)12.523.53061117294

17 Working with Arrays Summary Creating Vector and Matrix Accessing and Addressing Matrix Mathematical Operations with Matrix Functions for Analyzing Matrix


Download ppt "Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays."

Similar presentations


Ads by Google