Download Vector and Matrix Operations with MATLAB: Part II and more Exercises Mathematics in PDF only on Docsity! Introduction to MATLAB: Part II Vector and Matrices Operations with MATLAB In the previous introduction section, how to input vectors and matrices has been explained briefly. In this section, vector and matrix operations will be discussed within the limited scope of physic laboratory experiments. Vectors Operation In MATLAB a vector is a matrix with either one row or one column. In two dimensional system, a vector is usually represented by 1 × 2 matrix. For example, a vector, B in Figure 1 is 6i + 3j, where i and j are unit vectors in the positive direction for x and y axes, respectively in the Cartesian coordinate system. For three dimensional system, the unit vectors in x, y, and z axes are labeled i, j, and k, respectively. Figure 1. Two dimensional vectors, A, B, C and D plotted in the Cartesian coordinate system As mentioned earlier in the section of inputting vectors and matrices, MATLAB uses square brackets, [ ] to create a vector. For example, to create the vectors A = -2i + 6j, B = 6i + 3j, C = 4i – 3j, and D = -2i – 4j shown in Figure 10, type in the MATLAB command window. >> A = [-2, 6] >> B = [6, 3] >> C = [4, -3] >> D = [-2, -4] To create a vector v = 3i + 2j + 5k, type v = [3, 2, 5]. The basic operations that can be performed with vectors are addition, subtraction, and scalar multiplication. For example, to add A and B vectors (-2i + 6j) + (6i + 3j), >> [-2, 6] + [6, 3] ans = 4 9 To perform the scalar multiplication 2⋅(3i + 2j +5k), type 2*[3, 2, 5] or [3, 2, 5]*2 : >> [3, 2, 5]*2 ans = 6 4 10 To find the magnitude of a vector in MATLAB, you can use a command, norm. In mathematics, the magnitude of a vector (xi + yj) is defined as . For example, the magnitude of the vector C (= 4i – 3j) is 5 (= )). In MATLAB, >> norm([4, -3]) ans = 5 or, since you already defined the vector C (C = [4, -3]), >> norm(C) ans = 5 To find the angle with x axis, use the atan2(y, x) command, which is the four quadrant arctangent of the real parts of the elements of x and y. Note that the y value must be entered before x. This command will return an angle between π and -π. To get a value in degrees, multiply the answer by 180/π. For example, to find the angles in degree for the vectors A = -2i + 6j, B = 6i + 3j, C = 4i – 3j, and D = -2i – 4j: >> atan2(6, -2)*180/pi ans = 108.4349 >> atan2(3, 6)*180/pi ans = 26.5651 >> atan2(-3, 4)*180/pi ans = -36.8699 >> atan2(-4, -2)*180/pi ans = -116.5651 There are other inverse tangent functions atand( ) and atan( ), the first giving a degree answer and the second giving a radian answer. For the vector of D = -2i – 4j, examples are: >> atand(-4/-2) ans = 63.4349 >> atan(-4/-2) ans = 1.1071 However, they do not return the angle in the proper quadrant. As shown in the above examples, atand(-4/-2) returns an answer of 63.4 degrees instead of the third quadrant angle of 243.4 (= 360 + (-116.6)) degrees. Matrix Operation The following table shows the basic matrix operations in MATLAB for addition, substraction, multiplying all elements of matrix by a scalar, and dividing all elements of a matrix by a scalar. Matrix A Matrix B >> A = [2, 4; 6, 8] A = 2 4 6 8 >> B = [1, 2; 3, 4] B = 1 2 3 4 Addition, A + B Subtraction, A – B >> A + B ans = 3 6 9 12 >> A – B ans = 1 2 3 4 Multiplication of all elements of matrix A by 2 Division of all elements of matrix A by 2 >> Z = 2*A Z = 4 8 12 16 >> X = A/2 X = 1 2 3 4 Multiplication Element-by-element multiplication. Note that this is different from multiplication of two matrices. >> A*B ans = 14 20 30 44 >> A.*B ans = 2 8 18 32 Element-by-element power >> A.^B ans = 2 16 216 4096 In addition to the matrix operations mentioned in the above table, there are matrix divisions including left and right matrix division. Here, only left matrix division will be discussed. The MATLAB command for left matrix division is mldivide or “\” (back slash). For example, X = A \ B in MATLAB command means dividing A into B. This is equivalent to inv(A)*B, where inv(A) is the inversion of matrix A. Basically, the resultant value, X is the solution to A*X = B, which is expressed as inv(A)*A*X = inv(A)*B. The product of inv(A) and A gives you identity matrix. For example, >> A = [1, 2; 3, 4] A = 1 2 3 4 >> inv(A) ans = -2.0000 1.0000 1.5000 -0.5000 >> inv(A)*A ans = 1.0000 0 0.0000 1.0000 Let’s try to solve the following linear equations using left matrix division. First thing is to create matrix form. 3x + 4y + 5z = 2 2x – 3y + 7z = -1 x – 6y + z =3 >> A = [3, 4, 5;2, -3, 7;1, -6, 1] A = 3 4 5 2 -3 7 1 -6 1 >> B = [2; -1; 3] B = 2 -1 3 >> X = mldivide(A,B) X = 2.6196 -0.2283 -0.9891 >> format rat, X X = 241/92 -21/92 -91/92 Therefore the answer is x = 241/92, y = -21/92, and z = -91/92. 3 2 1 4 3− 6− 5 7 1 ⎛ ⎜ ⎜ ⎝ ⎞ ⎟ ⎟ ⎠ x y z ⎛ ⎜ ⎜ ⎝ ⎞ ⎟ ⎟ ⎠ ⋅ 2 1− 3 ⎛ ⎜ ⎜ ⎝ ⎞ ⎟ ⎟ ⎠ :=