Download Understanding Vectors and Matlab Environment and more Assignments Computer Science in PDF only on Docsity! 1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function differ from the use of the colon : operator? Readings: Matlab by Pratap Chapter 3. 3 1) engineering visualization and computation software • 2-D contour, histogram, line, mesh, polar, polygon and scatter plots. • 3-D line, mesh, polygon, scatter and wire frame plots. 2) The “interactive environment” is user friendly. 3) MATLAB provides its own high-level language in which users can extend the capabilities of MATLAB. • C-like programming language • Many user defined functions 1-4 MATLAB is interactive, if you type (without ; ) >> 2 + 3 The system responds with ans = 5 “ans” is a built-in variable that contains the results of the last evaluated expression that has not been assigned to another variable. e.g. if we type >> 3 * 3 ans = 9 1-5 To suppress the screen output use a semicolon: >> 5 ^ 2; (“ ^ ” is the power operator, 5 raised to the second power in this example) >> ( no response from matlab) To examine the current value of ans type >> ans ans = 25 1-6 All variables are created as array variables. For example, >> x = 1.5; (this is called an “assignment statement”) creates a (1 x 1) array of type real and assigns the value 1.5 to x. Variables only change values when you assign values to them using an assignment statement. To see the value of x type, >> x x = 1.5000 Variable names must begin with a character and are case sensitive. 1.5x RAM 1-7 The variable to be “assigned” a value must be on the left hand side of the assignment statement… >> 1.5 = x; (illegal “assignment statement”) The above is illegal because “1.5” does not specify a legal address in memory. An assignment statement is not a “math” equation. For example, the following sequence is legal in Matlab: >> x = 2.0; >> x = x + 1.0 x = 3.0000 1-8 The current environment for our computing session is called the workspace. We can have many variables and functions in our workspace. To see what variables we have, type >> who ans x For this session (up to now) the workspace contains two variables ans and x. To see the size (number of rows and columns) of these variables, type >> whos Name Size Bytes Class Both x and ans are 1 x 1 arrays, ans 1x1 8 double array we call these scalars. x 1x1 8 double array double refers to double precision) 1-17 The “ : ” (colon) operator is important in construction of arrays >> x = 1:100; (creates a 1x100 row vector with values 1,2,3,4, … 100) >> y = (-1 : 0.1 : 1)’; (creates a column vector with starting value -1 and increments by 0.1 until 1 is reached but not exceeded) >> z = 0 : 0.3 : 1 z = 0 0.3000 0.6000 0.9000 >> z = 5 : -1 : 2 z = 5 4 3 2 1-18 Use (parenthesis) to select a subset of the elements of an array. >> x = [10 9 8 7]; >> y = [3; 4; 5]; >> z = x(2) (note the use of parenthesis and not the brackets) z = 9 (subscripting x does not change x) >> z = x(1:3) (note the use the colon operator) z = 10 9 8 >> x = x(2:3) (now x has changed) x = 9 8 >> z = y(1:2) z = 3 4 1-19 Use the end function to obtain the last element in a vector. Example: >> x = [10 9 8 7]; >> y = [3; 4; 5]; >> z = x(end) z = 7 >> z = y(2:end) z = 4 5 1-20 Use subscripting to change the value of a vecor. Example: >> x = [10 9 8 7]; >> y = [3; 4; 5]; >> x([2, 3]) = 5 x = 10 5 5 7 >> y(2:end) = [ 6 7] % y(2:end) = [6 ; 7] works too y = 3 6 7 1-21 The built-in Matlab function linspace (see p. 57)works in a similar manner to the colon operator “ : ” . linspace(a,b,N) creates a row vector of N equally spaced values starting at a and ending at b. This is equivalent to a : (b-a)/(N-1) : b Example: Create a row vector named vec1 of 4 elements starting at -1 and ending at +1. >> vec1 = linspace(-1,1,4) vec1 = -1.0000 -0.3333 0.3333 1.0000 1-22 Example: Create a row vector named vec2 of 4 elements each equal to 7. >> vec2 = linspace(7,7,4) vec2 = 7 7 7 7 Example: Create a column vector named vec3 with the values 5,4,3,2 . >> vec3 = linspace(5,2,4) vec3 = 5 4 3 2 1-23 (dot form) Rules for the valid operations(see p 57,58). If A and B are vectors then it is not always true that >>A op B is defined when op is one of the operations in the table above. A and B are called operands. The size of an vector is its number of rows and columns. When multiple operators are used in an expression , use the rules of precedence. *, / , \ , ^ have “special” meaning as we will consider... add sub mult right div left div power + - * / \ ^ .* ./ .\ .^ 1-24 >> A = [2 3 4] ; >> B = 3; >> A + B ans = 5 6 7 A and B must be of the same size or a scalar. >> B = [1 2 3] ; >> A + B ans = 3 5 7 1-25 F1 F2 F = F1 + F2 = (Fx1 + Fx2, Fy1 + Fy2) In Matlab: F = resultant of two forces applied to an object. >> F1 = [1 , 2.5]; >> F2 = [-1.75, 1.0e-1] ; >> F = F1 + F2 F = -0.7500 2.6000 1-26 >> A = [2 3 4] ; >> B = 3; >> A - B ans = -1 0 1 A and B must be of the same size or a scalar. >> B = [1 2 3] ; >> A - B ans = 1 1 1 1-27 In Cartesian coordinates, we can express the dot or scalar product of two vectors as a matrix multiplication: [ ] 332211 3 2 1 321 sfsfsf s s s fff ++= ⎥ ⎥ ⎥ ⎦ ⎤ ⎢ ⎢ ⎢ ⎣ ⎡ •=• sF Physical Example: Work done by a force F acting through a displacement s F s sFW •=>> s = [1, 1, 1] ‘ ; >> F1 = [1 2.5 3.5]; >> F1 * s (dot or scalar product) ans = 7 surface 1-28 >> A = [2 3 4] ; >> B = [1 ; 2 ; 3] ; >> A * B ans = 20 In order for the scalar product of vectors A * B to be defined, the number of columns in the row vector A must equal the number of rows in the column vector B.