PowerPoint PresentationSlide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 321-2What 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 331) 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 functions1-4MATLAB is interactive, if you type (without ; ) >> 2 + 3The system responds withans =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 =91-5To 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 =251-6All 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,>> xx =1.5000Variable names must begin with a character and are case sensitive. 1.5xRAM1-7The variable to be “assigned” a value must be on theleft hand side of the assignment statement…>> 1.5 = x; (illegal “assignment statement”)The above is illegal because “1.5” does not specify a legaladdress 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.0x =3.00001-8The 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 xFor 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 (we call these scalars)x 1x1 8 double (double refers to double precision)9(see p. 12 in Pratap for a list of these Matlab commands)… (extend a command that doesn’t fit on one line)>> save lab3 (saves all the workspace variables to the file lab3.mat)>> load lab3 (loads the lab3.mat file from the Unix pwd)>> save (uses default file matlab.mat)>> load (uses the default file matlab.m at from the Unix pwd)1-10(see p. 12 in Pratap for a list of these Matlab commands)>> clc % clears the command window>> clf % clears the figure window>> clear var1 var2 % clears only the variables var1 var2>> clear % clears(deletes) all variables in the workspace>> exit % exit Matlab>> quit % same as exit>> what % lists Matlab files1-11format options (controls how numeric output is displayed)(see p. 9 in Pratap for a list of these Matlab commands)Examples-- two of the options are shown below:>> x = 12.34567890123456789 x = 12.34567890123457 >> format short>> xx = 12.3457>> format long e>> xx = 1.234567890123457e+0112In Matlab (for CS101) all data will be stored in arrays. An array can represent a: vector - size 1 x n (row vector with 1 row and n columns) or n x 1 (column vector with n rows and 1 column) matrix - any size m x n ( a table of m rows and n columns). Conventions Two arrays are defined to be equal if they are of the same size and at each position, they have the same value.The values in an array are called components or elements.1-13Some examples of vector quantities that occur frequently in science and engineering problems are • velocity• acceleration • force• electric, magnetic, gravitational fields• surface normalFor example, the three-dimensional components of a force vector can be represented in Cartesian coordinates as P = (x, y) The xy coordinates in the plane correspond to a two-dimensional "vector" F = (Fx, Fy, Fz)1-14We can represent a vector either as a row vector or as a column vector, depending on the application. E.g., 321321 vvvorvvv VV1-15To create vector arrays in MATLAB type the following: >> F1 = [1 2.5 3.5];>> F2 = [-1.75, 2.75, 1.0e-1];>> F3 = [1 2 ];>> whos F1 F2 F3Name Size Bytes ClassF1 1x3 24 double F2 1x3 24 double F3 1x2 16 double F1 and F2 are 1 row x 3 column arrays and F3 is a 1 row x 2 column array.To display their values type the variable name:>> F1F1 =1.0000 2.5000 3.5000(The use of the “ , ” operator is optional)1-16To create column-vector arrays in MATLAB either use the semi-colon operator ; or use the transpose operator ‘>> F1 = [1 ; 2.5; 3.5]; ( ; means ... append to next row)>> F2 = [-1.75, 2.75, 1.0e-1] ‘ ; (transpose changes row vector to column vector and vice-versa )>> F3 = [ ]; (empty vector)>> whos F1 F2 F3Name Size Bytes ClassF1 3x1 24 double F2 3x1 24 double F3 0x0 0 double >> F1F1 =1.00002.5000 3.50001-17The “ : ” (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 21-18Use (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 =341-19Use 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 =451-20Use 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 =3671-21The
View Full Document