MATLAB is a software package that makes it easy to manipulate matrices. Vectors can also be easilyy handled as a special p case. A matrix in MATLAB is just a 2-D array of numbers. Example: >> A =[1 2; 3 5; 2 4; 6 7] A= 1 2 3 5 2 4 6 7 The dimensions of a matrix is (NxM) where N is the number of rows and N is the number of columns. The matrix A above has dimensions (4x2) We can add or subtract matrices by simply adding or subtracting each element in the arrays located at the same position. Example: >> A = [ 1 2; 3 4] A= 1 2 3 4 >> B = [3 3; 5 5] B= 3 3 5 5 >> C=A+B C A B To add matrices they must be of the same size C= 4 5 8 9 we can also multiply matrices together in the following fashion. Let Amn be the element in an array, y, A,, of dimensions (MxN) ( ) at the mth row and nth column position. Similarly for a matrix B of dimensions (NxK) we have Bnk is the element at the nth row and kth column position . Then the matrix product of A and B, C=A*B, is defined for each element of C as : N Cmk = ∑ Amn Bnk n =1 (MxK) (MxN) (NxK) Note: for this to be meaningful, meaningful we must have the number of columns of A be equal to the number of rows of B but the other dimensions can be different Example: >> A = [ 2 3; 1 5]; >> B =[2 2; 6 3]; >> C=A*B C= 22 13 32 17 which comes from our multiplication rule C11 = A11 B11 + A12 B21 = (2)(2) + (3)(6) = 22 C12 = A11 B12 + A12 B22 = (2)(2) + (3)(3) = 13 C21 = A21 B11 + A22 B21 = (1)(2) + (5)(6) = 32 C22 = A21 B12 + A22 B22 = (1)(2) + (5)(3) = 17 We can consider vectors to be just examples of matrices where one of the dimensions =1. We can either have row or column vectors: >> v1 =[ 1 2 3 5] (1x4) (row) vector v1 = 1 2 3 5 >> v2 =[1;2;3;5] v2 = 1 2 3 5 (4x1) (column) vector we can multiply vectors A and B in the same fashion as other matrices as long as we adhere to the previous rule: we must have the number of columns of A be equal to the number of rows of B in the product A*B Consider the two vectors: >> v1 =[ 1 2 3 4]; >> v2 =[1;2;3;4]; [ ] >> d=v1*v2 (1x4) d= 30 (4x1) (1x1) scalar This is just the dot product ( or the square of the magnitude in this case)!! Now consider reversing the order: >> v1 =[ 1 2 3 4]; >> v2 =[1;2;3;4]; >> p= v2*v1 (4x1) (1x4) p= (4x4) Here 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16 pmn = v 2m v1n We can also multiply matrices with vectors if we follow the same rule: >> M=[1 2; 3 4] M= 1 2 3 4 >> x= [5;6] x= 5 (2x1) 6 >> b= M*x (2x2) (2x1) b= 17 39 This is equivalent to the linear system of equations: M 11 x1 + M 12 x2 = b1 M 21 x1 + M 22 x2 = b2 another way to write these equations is ⎡ M 11 ⎢M ⎣ 21 M 12 ⎤ ⎡ x1 ⎤ ⎡ b1 ⎤ =⎢ ⎥ ⎥ ⎢ ⎥ M 22 ⎦ ⎣ x2 ⎦ ⎣b2 ⎦ We also could set >> x= [5 6] x= 5 6 >> M=[1 2; 3 4] M= ((1x2)) 1 2 3 4 >> b=x b=x*M M b= 23 34 (1x2) ((2x2)) Note that this b vector is different from b f before even though th h th the x and dMh have the same elements as. If we want to get the same result for b as before but now in the form of a row vector we need to multiply p y x by y the transpose p of M,, MT, where if M 12 ⎤ ⎡M M = ⎢ 11 ⎥ ⎣ M 21 M 22 ⎦ M 21 ⎤ ⎡M M T = ⎢ 11 ⎥ ⎣ M 12 M 22 ⎦ (interchange rows and columns) This works since for any two matrices or vectors A, B, where the product is defined we have (AB)T =BTAT so if we take the transpose of ⎡ M 11 ⎢M ⎣ 21 M 12 ⎤ ⎡ x1 ⎤ ⎡ b1 ⎤ =⎢ ⎥ ⎥ ⎢ ⎥ M 22 ⎦ ⎣ x2 ⎦ ⎣b2 ⎦ we get [ x1 ⎡ M 11 x2 ] ⎢ ⎣ M 12 M 21 ⎤ = [b1 b2 ] M 22 ⎥⎦ In MATLAB, the transpose is MT =M' >> b=x*M' b= 17 39 same b as obtained originally but now in terms of a row vector For vector analysis, MATLAB has built-in dot and cross product functions: >> v1 = [ 1 2 6]; >> v2 = [ 3 5 -2]; 2]; >> d =dot(v1, v2) d= 1 >> c =cross(v1, v2) c= -34 20 here the vectors must be 3-D vectors i (1 i.e. (1x3) 3) or (3 (3x1) 1) iin di dimensions i -1 we saw before we could also get the dot product by calculating v1*v2T >> v1 v1*v2' v2 ans = 1 In solving Statics equilibrium problems, we will typically have to solve a set of linear equations for a set of unknown forces or moments. We can do this easily in MATLAB: Example Consider the following system of two equations for the two unknowns x1 and x2 ⎡ C11 C12 ⎤ ⎡ x1 ⎤ ⎡ b1 ⎤ =⎢ ⎥ ⎢C ⎥ ⎢ ⎥ ⎣ 21 C22 ⎦ ⎣ x2 ⎦ ⎣b2 ⎦ If we define the C matrix of coefficients and the vector b in MATLAB then the solution is given by using a backslash operator: >> C = [ 1 2; 3 7]; >> b=[1;1]; >> x =C\b x= 5.0000 -2.0000