EGR 111 Introduction to MATLAB This lab introduces the MATLAB help facility, shows how MATLAB TM, which stands for MATrix LABoratory, can be used as an advanced calculator. This lab also introduces assignment ( = ) statements, entering vectors and matrices (the [ ] brackets, the multiple uses of the semi-colon ( ; ), and use of the colon operator ( : ). New MATLAB commands: help, lookfor, brackets [ ], colon, semicolon, linspace, and parentheses ( ) 1. Getting Started MATLAB is installed on the PC s in the computer classrooms and open lab in Shiley Hall. To start MATLAB in Windows, click on Start, All Programs, Courseware, General Engineering, MATLAB. 2. Getting Help To launch the MATLAB help facility, click on Help, and then Product help. A window should open that contains documentation for MATLAB. Click on the plus sign next to MATLAB and then the plus sign next to Getting Started link. You might want to explore the tutorials on your own time to become more familiar with MATLAB. The sections most relevant to this course are Introduction, Matrices and Arrays, Graphics, and Programming. If you know the name of the function or operator for which you want more information, you can go to MATLAB s command window and type the word help followed by the function name. EGR111 - p. 1 of 7 - Intro_to_MATLAB_rev2.docx
For example, type the following command into MATLAB s command window and then press the Enter key: help plot 3. Searching for a Keyword If you don t know the exact MATLAB command name, you can search for a keyword that might be included in the description of the command. For example, type the following command into MATLAB s command window: lookfor plot 4. Calculations MATLAB can do simple calculations, similar to your calculator. Type the following commands (each line followed by the Enter key): 4*5 3^2 The variable ans always stores the result of the most recent calculation. This feature is similar to Texas Instruments calculators. When MATLAB starts up, it assigns the value of π to the variable named pi, so it can be used in calculations such as the following: cos(pi) We can also store values into variables, so we can use the values in a later statement. Try typing the following: x = 5*5; y = sqrt(x); Notice that nothing is printed when the line ends in a semicolon, but MATLAB does execute the command. This feature is useful when you don t want to see the results of a calculation printed to the screen. The two commands above are examples of assignment commands. MATLAB evaluates the expression on the right hand side and stores the result in the variable name on the left hand side. To see the value stored in a variable, just type the variable names as follows: x y pi We can also use the variables in expressions. Try typing the following command: a = x + y EGR111 - p. 2 of 7 - Intro_to_MATLAB_rev2.docx
In MATLAB, anywhere you can put a constant number (like 4), you can also put a variable (like x) or even an expression (like 3*x+4). Type the following commands to demonstrate this feature: a = 2 b = sqrt(a+7) In MATLAB, several commands can be placed on a single line. If you separate the commands by a comma, the results of each command are printed to the Command Window. If you separate the commands by a semicolon, the commands are executed but nothing is printed to the Command Window. For example, in the command below, the values of the variables a and c are computed and stored, but they not printed because they are followed by a semicolon. >> a = 1; b = 2*4, c = 5+8; b = 8 In MATLAB, you can recall previous commands by pressing the up arrow on the keyboard. So if you wanted to run the above command again, you could just hit the up arrow, edit the command if needed, and then Enter. If you type the first few characters of a command and then hit the up arrow, MATLAB recalls the most recent command that starts with those characters. You can also click on a previous command in the command history window and drag it to the Command Window. Try these features out to see how they can save a lot of typing. Rules for variable names: In MATLAB, variable names must begin with a letter and can contain letters, digits, and underscores (but not spaces or operators like -, +, *, /). Variable names are case sensitive, so x is not the same variable as X. Examples of valid variable names are x, x2, and number_of_wheels. Examples of variable names that are NOT valid are 2x, yes!, and x*2. 5. Order of Operations The order in which operations are computed are determined by the following table. Order Operator Symbol 1 Parentheses ( ) 2 Power ^ 3 Unary Plus and + and - Minus 4 Multiplication * and / and Division 5 Addition and + and - Subtraction 6 Colon : EGR111 - p. 3 of 7 - Intro_to_MATLAB_rev2.docx
Parentheses are computed first, followed by the other operators in the order shown in the table. So in the expression 4*(3+2), the value of (3+2) is computed first, then the result is multiplied by 4. In the expression 3*2^2, the power operator (^) has higher precedence than multiply (*), so 2^2 is computed first, and the result is then multiplied by 3. So 3*2^2 = 3*(2^2) = 3*(4) = 12. Operators on the same level of precedence are computed left to right. So 6/3*2 = (6/3)*2 = (2)*2 = 4 because division and multiplication are on the same level. Unary minus refers to the case when a minus sign operates on a single value (as opposed to subtracting two values). For example, in the expression -2^2, the power has a higher precedence than the unary minus operator, so -2^2 = -(2^2) = -4. Determine the value of the following expressions and check your answers with MATLAB. 2*1+3 = 10/5*2 = 4/2+3 = -2^2+1 = 4+2/2+3 = 2^2*3 = -2*3^2+1 = 6. Vectors and Matrices Variables can store a single value (called a scalar) such as: a = 4 In MATLAB, variables can also store a row of values (called a row vector) by including the values in square brackets. Try typing the following command: b = [2 4 6 8 10] The values can be separated by spaces (as shown above) or by commas (as shown below). b = [2,4,6,8,10] We could store values in a column vector by separating the values with semicolons as follows: c = [2; 4; 6; 8; 10] EGR111 - p. 4 of 7 - Intro_to_MATLAB_rev2.docx
MATLAB can also store a table of values (called a matrix) by separating the rows of the matrix by semicolons. Try typing the following command: d = [1 2 3; 4 5 6; 7 8 9] Strings are vectors that contain characters. In MATLAB, strings are enclosed in single quotes as follows: str = 'This is a string' One of MATLAB s strengths is that it can easily store and process vectors and matrices, which can represent strings, audio signals, images, and any other large set of data. 7. Indexing Vectors and Matrices To access a particular element in a vector, type the element number in parentheses after the vector name. For example, assuming that you defined the vector c as shown above, you can access the third element of vector c by typing c(3) and replace it by 100, as follows: c(3) = 100 The command above overwrites the third element of the vector c with the new value of 100. Note that the old value (which was 6) is lost. Likewise, to access an element of a matrix, specify the row and column numbers. For example, assuming that you defined the matrix d as shown above, you can access the element in the second row and third column by typing d(2,3) and replace it by 100, as follows: d(2,3) = 100 The command above overwrites the element in the second row and third column of the matrix d with the new value of 100. Again, the old value (which was 6) is lost. If this old value will be needed in the future, you could store it as another variable, before using the command above. x = d(2,3) 8. The MATLAB Workspace Notice the Workspace in the upper right corner of the MATLAB window. Are the names inside of the Workspace familiar? The Workspace lists all variables that you have created and which are currently being stored in the computer s memory (RAM). Within MATLAB, you retrieve these stored value by using their variable names inside of the Command Window. EGR111 - p. 5 of 7 - Intro_to_MATLAB_rev2.docx
9. Colon Operator MATLAB has a way to easily create vectors called the colon operator. First, let's experiment with creating vectors. Type the following: x = 1:10 x = 1:2:10 x = 10:-1:1 t = 0:0.1:1 At this point, you have probably figured out that the notation start:inc:end generates a vector beginning with the value of start and incrementing by the value of inc up until it gets to the value of end. If the increment is not given (that is, if start:end is typed), MATLAB uses a default increment of 1. If we want to use a generated vector in an expression, we can enclose the colon expression in square brackets. For example, type the following to yield 2 times the row vector [0:0.1:1]: t = 2 * [0:0.1:1] Since the numeric operators have higher precedence than the colon operator (see the table in Section 5 of this document), the expression 0:1/10:2*pi would be equivalent to 0:(1/10):(2*pi) and would result in a vector that started at 0, incremented by 0.1, and ended just before 2*pi. 10. Linspace Function The command linspace can also be used to generate vectors of equally-spaced values. It is more convenient than the colon operator when you would rather specify the number of values to generate instead of the increment between the values. For example, to generate a vector that starts at 0, ends at 2, and has 10 elements, type the following: x = linspace(0,2,10) If you only give linspace the start and end values, it will default to 100 elements. So, x = linspace(0,2)would return a vector with 100 elements between 0 and 2. 9. Using the Colon Operator in Indexing Several elements of a vector can be selected using the colon operator. For example, let's replace the first four elements of the vector x by the value 100 as follows: w = 1:10 w(1:4) = 100 EGR111 - p. 6 of 7 - Intro_to_MATLAB_rev2.docx
In the command above, MATLAB first expands 1:4 to the vector [1 2 3 4], and then it accesses those elements and sets their value to 100. If we want to access several elements at the end of a vector, we can use the end statement: x = 1:10 x(6:end) = 100 We can access a subset of a matrix in a similar way. y = [1 2 3; 4 5 6; 7 8 9] y(2:3, 3) = 100 If we want to access a whole row or column, we can use just a colon as follows: z = [1 2 3; 4 5 6; 7 8 9] z(:, 3) = 100 The colon operator allows us to easily access any desired part of a vector or matrix. Exercise 1: Write the commands that will generate the vector x1 = [0.3 0.4 0.5 1.0] and use indexing to replace the even indexed elements (those at positions 2, 4, 6, etc.) with the value 100. Exercise 2: Write the commands that will produce a vector x2 that is the sin function of the following elements: 0, π/2, π, 3π/2, 2π. Note that you might want to use the help sin command to learn more about the sin function: specifically, whether the function uses radians or degrees. The help description also tells you that sin(x) is the sine of the elements of X. Note that X can be a scalar, a vector, or a matrix. Exercise 3: Create the matrix x3 = [1 2 3 4; 5 6 7 8; 9 10 11 12]. Then multiply this vector by 5 to create a new matrix y3. Then write the commands to change elements in the second row of y3 to the value 100. Checkpoint 1: Show the instructor your commands and the results for Exercises 1, 2, and 3. Your instructors will be especially appreciative if you use the semicolon ; to suppress output, except for your final results. EGR111 - p. 7 of 7 - Intro_to_MATLAB_rev2.docx