1 ME313 Homework #1 Matlab- Command Window Operations, Scalars and Arrays Last Updated August 17 2012. Assignment: Read and complete the suggested commands. After completing the exercise, copy the contents of the command window into Word or Adobe, and save as a pdf. Turn in a hardcopy of the pdf. Organization of Matlab software: Upon opening Matlab, multiple windows will appear. Depending on which version of Matlab you are using, the screen will look like this The window at the left, with the command prompt >>, is the command window. One uses the command window by typing commands after the prompt. Scalar variables and expressions: Matlab is a programming language. A programming language functions by executing commands in sequence. The information flow for each command is right-to-left, meaning that the = symbols is referred to as the assignment operator. For example, at the command line, type x=5. The command line and response after the command line will be >> x=5 5
2 The meaning of this action is that the number 5 has been placed in a storage location named x, i.e., the information 5 was assigned by the = symbol to be placed in the variable x. This is the meaning of the assignment operator terminology. The storage location x, known as a variable, has been created by the assignment operator. When Matlab is initially opened, no variables exist. They are created by commands. Assignment operator and computational expressions: One can use the assignment operator to perform computations. For example, now type the command y=x^2. The command line and response should look like >> y=x^2 y = 25 The meaning of the command is take the value stored in the variable x, square it, and then store it in the new variable y. The response is to display the value of the variable y. When down and left-to-right information flow is violated, Matlab will stop and display an error message. Type the command y=z. The command and response will be >> y=z??? Undefined function or variable 'z'. An error message, which should appear in red font, was encountered because the numerical value of z has not yet been defined. Error messages are very useful for debugging code (take my word for it). Listing existing variables: An extremely useful command is whos. This command lists the variables that have been created in a session. Type the command whos. The response should be x 1x1 8 double y 1x1 8 double The result is a table of the variables that exist in the current session. The first column contains a list of the names of the variables that have been created. We see that x and y exist, but z does not. The second column gives the dimension each variable. At this point, the variables have been scalars, indicated by the 1x1 dimension. Later, we will consider arrays and other array abstractions, which may be thought of as generalized
3 matrices. The third and fourth columns list the number of bytes that each variable uses to store the numerical information, and the variable type, i.e., whether it is a floating point number (double), or integer, among some of the options. Clearing variables in a session: Sometimes it is necessary to clear (erase, delete) the defined variables in a session. This is peformed with the clear command. Type the command clear, followed by the command whos. The output should look like >> clear >> Nothing appears after the whos command because the variables x and y have been deleted or erased from the session. Assigning values to vector and square matrix arrays: In the previous examples, the variables x and y were scalars. That is because they stored only one number in each variable. A variable with a single name may store many numbers. Variables can be arrays, in particular column vectors, row vectors, and square matrices are the most elemental examples. Consider the row vector { 1 2.3-5}. The dimensions of this vector are 1x3, i.e., one row and three columns. A variable can be defined to store this row vector. At the command prompt, type x(1,1)=, followed by whos. The output should look like >> x(1,1)=1 1 x 1x1 8 double The response is, as before, a scalar variable named x, that has a value of one, and the whos command shows that x is a scalar. Now type x(1,2)=2.3, followed by whos. The response should be >> x(1,2)=2.3 1.0000 2.3000 Now the response shows two values for x, 1.0000 and 2.3000, and the whos command lists the dimensions of the variable x as 1x2. The syntax x(1,1) and
4 x(1,2) refers to specific elements of the variable named x. Each element of an array is identified by its row and column location. The first number in the parenthesis refers to the row, and the second number in the parenthesis refers to the column location of an individual element in the array. The command x(1,1)=1 assigns the value of one to the element of x located in the first row and first column. The command x(1,2)=2.3 assigns the value of 2.3 to the element of x located in the first row and second column. An aspect of the procedure just described is that Matlab increases the size of the variable to encompass the information that is assigned. Note that the whos command after the assignment x(1,1)=1 command indicates that the dimension of x is 1x1. After the assignment x(1,2)=2.3, the whos command reveals that the dimension of x is 1x2. Matlab automatically expands the size of the array to store increasing amounts of data. As an exercise, define a matrix z and z1 to have the values 3 5.2 10 8 z 12 1 0, z1 2. 6 8.3 0.06 1 Your output should look like z = -3.0000 5.2000 10.0000 12.0000-1.0000 0 6.0000 8.3000-0.0600 and z1 = 8 2-1 Type the whos command. Your output should now look like Alternate method to assign values of arrays: There is another way to assign values to array elements. Type the following command x1=[ 1 2.3], followed by whos. You should obtain the following result
5 >> x1=[1 2.3] x1 = 1.0000 2.3000 x1 1x2 16 double A new array x1 has been created. The array x1 has same dimensions as x, and has identical elements. The syntax of the statement x1=[ 1 2.3] is to enclose the array in square brackets, and separate each column element by a space. Unless otherwise indicated, the elements will be placed in the first row by default. Now consider the statement q=[-3 5.2 10 ; 12-1 0 ; 6 8.3-0.06], followed by the whos command. The response at the command window should be >> q=[-3 5.2 10 ; 12-1 0 ; 6 8.3-0.06] q = -3.0000 5.2000 10.0000 12.0000-1.0000 0 6.0000 8.3000-0.0600 q 3x3 72 double x1 1x2 16 double A new variable q has been created, which is a 3x3 matrix that has elements identical to those of z created earlier. The syntax of the statement q=[-3 5.2 10 ; 12-1 0 ; 6 8.3-0.06] is again to enclose the array in square brackets, separate column elements with a space, but now indicate a change to a new row with the semicolon ; character. Thus, to assign array elements in the manner, enclose the numerical values with square brackets, separate column entries with a space, and indicate movement to a new row with the semi-colon ;.