Wichita State University Logo

Math 451: Computational Mathematics

1.3 Elements of MATLAB Commands


1.3.1 MATLAB Commands

A MATLAB command is a string of ASCII characters typed into the MATLAB Command Window or occupying a line in a text file with the ".m" file extension.

A MATLAB statement is a string of ASCII characters typed into the MATLAB Command Windows or occupying a line of text in a text file with the ".m" file extension that contains one or more valid MATLAB commands and/or comments.

The American Standard Code for Information Interchange is the most common encoding scheme used for storing plain text . Other standards that encompass more languages then just English such as Unicode are more common in e-mail and documents, however MATLAB and many languages do not attempt to localize the keywords in the programming language.

1.3.2 Simple MATLAB Expressions

Simple MATLAB expressions evaluate an algebraic expression such as

>> 3 + 10 / 2 - 9 * 2

We have already discussed these types of MATLAB statements. A MATLAB expression may resolve to a value that may be represented by one of the built-in MATLAB data types or one of the built-in Fundamental MATLAB Classes .

>> 3 > 0

A MATLAB logical expression will resolve to true or false . However we can construct MATLAB expression that contains both logical and numeric values.

>> (x > 3)*(x + 2) + (x <= 3)*(2*x - 5)

The value of this expression will depend on the value stored in the workspace associated with the label x . If the value stored in the memory location labeled x is 5 , then the MATLAB expression above becomes

>> (5 > 3)*(5 + 2) + (5 <= 3)*(2*5 - 5)
  1. Since the expression 5 > 3 is in parentheses and farthest to the left it is evaluated first and the result is true .
  2. Since the expression 5 + 2 is in parentheses and next the expression is resolved to 7 .
  3. Since the expression 5 <= 3 is in parentheses and next the expression is resolved to false .
  4. The last expression in parentheses 2*5 - 5 resolves to 5 .

As an intermediate result we have

>> true * 7 + false * 5

In this case the MATLAB interpreter is faced with an expression containing multiple data types. Therefore the boolean value true is converted to the numeric value 1 , and the boolean value false is converted to the numeric value zero.

>> 1*7 + 0*5

This expression resolves to the numeric value of 7 .

In this way we can use and expression like

>> (x > 3)*(x + 2) + (x <= 3)*(2*x - 5)

to describe a piecewise defined function .

Exercise 1

Use a single MATLAB expression that evaluates

$$ \begin{align*} x - 3 &\qquad \text{if $x \lt 0$} \\ 0 &\qquad \text{if $0 \le x\lt 5$} \\ x + 3 &\qquad \text{if $x \ge 5$} \end{align*} $$


Check your work

```>> (x < 0)*(x - 3) + (x >= 5)*(x + 3)```



Debugging Challenge!

1.3.3 Calling Functions

An elementary way to create more complex MATLAB expressions is to invoke one of the most common built-in MATLAB functions . If you look at the most common MATLAB tools you can see a wide variety of computational mathematics.

MATLAB supplies a large number of functions just for numeric data types. For example

>> z = complex(2,-3)
>>
>> z =
>>
>>      2     -   3i

This MATLAB statement consists of two MATLAB expressions connected by the assignment operator . The assignment operator expects a MATLAB expression that resolves to a location in memory in the current workspace on the left-hand side of the equal operator = , and an expression that resolves to a built-in or user defined data type on the right-hand side of the equal operator. The equal operator is one of several MATLAB operators. An operator is a special character that represents a function call in a MATLAB expression or statement.

In the statement above the equal operator is given two arguments, the memory location for workspace variable z called an lvalue , and the value that results from a MATLAB expression such as calling complex(2,3) called an rvalue . Keep in mind that when you use the assignment operator, the expression on the left must resolve to a variable , that is a element of a workspace that is in scope (We will talk more about this later). The purpose of the assignment operator is to store the rvalue of the expression on the right of the operator into the location specified by the lvalue. The rvalue must resolve to a value of a valid built-in or user-defined data type.

Keep in mind also that an rvalue is usually an array of a data type. By default the MATLAB interpreter is designed to consider all rvalues, arrays. A single rvalue is an $1\times 1$ array of rvalues.

In our example the built-in MATLAB function complex is passed the literal values 2 and 3 . The function complex returns a $1\times 1$ array of type complex

>> 2 + 3i

This value is assigned to the memory location associated with the label z in the current workspace.

1.3.4 The Global Workspace

The MATLAB environment creates and maintains all of the variables you create in command window in a single memory buffer called the global workspace . The global workspace is a heap . You can configure your MATLAB integrated development environment to display the global workspace in a docked or undocked window. Keep the global workspace open and docked until you become more familiar with the IDE and MATLAB.

In most languages creating and assigning a value to a variable requires three steps:

  1. Definition
  2. Declaration
  3. Initialization

In most cases you will perform all three tasks in a single MATLAB statement. However later in the course we will define our own data types. This will require us to perform these steps in different MATLAB commands and even in different MATLAB files. We need to understand these three steps now for simple data types so that we have a clear idea of these steps when we perform them separately.

A MATLAB statement such as

x = 5

performs all three steps in one statement.

  1. Definition: Because the rvalue in the statement resolves to a double precision floating point number, the data type of x is defined to be of type "double precision floating point". This occurs even if the lable x was already in the workspace and referred to a value of a different data type before this statement is executed!

  2. Declaration: Because the rvalue in the statement resolves to a value of type "double precision floating point", the MATLAB integrated environment makes sure that the label x refers to a memory location with enough memory to store a double precision floating point number. If not, it releases the old memory location and allocates a new one from the heap large enough to store our data.

  3. Initialization: the double precision floating point value of five is stored in the memory location referred to by the label x .

Making these distinctions for a simple MATLAB statement like, x=5 seems excessive. Learn to this now so that you will understand later when we need to separate these tasks into separate files.

1.3.5 Creating Matrices and Arrays

Since MATLAB considers most rvalues to be arrays of values, we must become familiar with creating arrays and using them in expressions. A Matrix is simply a two dimensional array; it has rows and columns. Array is a more generic term that can have a single value or a single row or column ( vector ). Arrays can also have many more dimensions or subscripts necessary to access an element of the array. For example consider the three dimensional array that requires several statements to initialize

>> A(:,:,1) = [ 1 2 3; 4 5 6; 7 8 9]

A =

       1              2              3       
       4              5              6       
       7              8              9       

>> A(:,:,2) = [ 11 12 13; 14 15 16; 17 18 19]

A(:,:,1) =

       1              2              3       
       4              5              6       
       7              8              9       


A(:,:,2) =

      11             12             13       
      14             15             16       
      17             18             19       

>> A(:,:,3) = [ 21 22 23; 24 25 26; 27 28 29 ]

A(:,:,1) =

       1              2              3       
       4              5              6       
       7              8              9       


A(:,:,2) =

      11             12             13       
      14             15             16       
      17             18             19       


A(:,:,3) =

      21             22             23       
      24             25             26       
      27             28             29       

>>

You might want to preallocate your three dimensional array with zeros and then fill them in with a function call or a combination of built-in MATLAB functions. The MATLAB statement

>> A = zeros(3,3,3)

A(:,:,1) =

       0              0              0       
       0              0              0       
       0              0              0       


A(:,:,2) =

       0              0              0       
       0              0              0       
       0              0              0       


A(:,:,3) =

       0              0              0       
       0              0              0       
       0              0              0       

>>

creates a $3\times 3\times 3$ dimensional array filled with $27$ values of zero.

1.3.6 Indexing An Array

It is important to understand that the MATLAB integrated development environment and all current programming languages represent an array as a list of values. Our previous $3\times 3\times 3$ array of $27$ values is stored as:

>> A(:)

ans =

       1       
       4       
       7       
       2       
       5       
       8       
       3       
       6       
       9       
      11       
      14       
      17       
      12       
      15       
      18       
      13       
      16       
      19       
      21       
      24       
      27       
      22       
      25       
      28       
      23       
      26       
      29       

>>

This storage method is called column-wise order. The first page of our array is the matrix

>> A(:,:,1)

ans =

       1              2              3       
       4              5              6       
       7              8              9       

>>

Notice the use of the colon operator for the first two dimensions. Looking above at the list of numbers we see that the first nine elements of the list are the numbers in the first page in column-wise order . This means we can access the element of an array by using its index into the list or its subscript . For example, element with subscripts (1,2,3) is the element in row 1, column 2, and page 3. These labels row , column and page are human readable terms that are NOT part of the MATLAB language. The MATLAB language considers the expression

A(1,2,3)

to refer to the element of array A with first script 1 , second subscript 2 , and third subscript 3 . The MATLAB interpreter computes the index of the array

$$ index = (3-1)*9 + (2-1)*3 + 1 $$
because each page is a $3\times 3$ matrix of $9$ values, and each column is a $3\times 1$ vector of 3 rows of numbers. The arrangement is called column-wise because the interpreter works right-to-left. Every third index refers to a column of three $3\times 3$ matrices. Each second index refers to a column of $3$ values. The left-most subscript refers to the subscript of the column .

>> A(1,2,3)

ans =

      22       

>> A((3-1)*9 + (2-1)*3 + 1)

ans =

      22       

>>

Using built-in MATLAB functions, the colon operator, and arrays of indices one can access slices or sub-arrays. We used this in our previous exercise and homework.

1.3.7 Multiple Commands in a Single Statement

One may separate MATLAB commands in a single statement

>> x = 5, y = 3, z = 2

x =

       5       


y =

       3       


z =

       2       

>>

This single MATLAB statement executes three MATLAB commands by separating the commands with a comma. In general you do not want to obfuscate your MATLAB statements by executing several MATLAB commands in a single statement. A common example using a MATLAB structured statement .

>> z

z =

       2       

>> if (z > 0), z = -z, end

z =

      -2       

>>

In this sequence of statements we display the value of label z . Next we execute three MATLAB commands in a single statement. Let us parse the following statement:

>> if (z > 0), z = -z, end
  1. The MATLAB interpreter executes an if command. The if command must also have a logical expression; in this case (z > 0) .

If the logical expression is true execution continues to the statement immediately following the if command; the if block .

If the logical expression is false execution continues to the first statement after the associated end command.

  1. Notice that the if (z > 0) command is followed by a statement separator that indicates that the next MATLAB command is on the same line with the if command and follows the separator. This command is defined by z = -z . This assignment results in the display of the value of z in the command window. This statement is also followed a statement separator.

  2. Finally an end command is executed. The command declares the completion (end) of the if-block of code.

The result of this statement is that whenever the value contained in the $1\times 1$ array z is greater than zero, the value is assigned the new value -z , the negative of the value contained in the $1\times 1$ array.

We will see that this will be a useful programming structure to use when we write our own user-defined functions using the MATLAB language.

1.3.8 The Semi-colon Separator

When a MATLAB command is terminated by a semi-colon separator ; , any output to the command windows is suppressed. We can modify the statement in the previous section.

>> z

z =

       2       

>> if (z > 0), z = -z; end
>>

Notice that display of the new value stored in location z is suppressed. There is no output of the value to the command window. We can use the semi-colon as the statement separator for both MATLAB commands

>> z

z =

      -2       

>> if (z > 0); z = -z; end
>> z

z =

      -2       

>>

However since this if command does not result in any assignment and nothing was displayed to the command window, there does not appear to be a difference between using the semi-colon separator and the comma separator.

>> z

z =

      -2       

>> if (z > 0); z = -z; end
>> z

z =

      -2       

>> ans

ans =

      22       

>>

Notice that in addition the workspace variable ans is not updated as well.

Creative Commons Logo - White


Your use of this self-initiated mediated course material is subject to our Creative Commons License .


Creative Commons Attribution-NonCommercial-ShareAlike 4.0

Creative Commons Logo - Black
Attribution
You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

Creative Commons Logo - Black
Noncommercial
You may not use the material for commercial purposes.

Creative Commons Logo - Black
Share Alike
You are free to share, copy and redistribute the material in any medium or format. If you adapt, remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.