Go to CCP Homepage Go to Materials Page Go to Engineering Mathematics Materials Go to Table of Contents
Go Back One Page Go Forward One Page

MATLAB Tutor

Part 3: Variables

Remember to press Enter or Return after each command.

  1. Enter

         x=3
  2. Enter

         y=2
  3. Enter

         2*x+y

Note that the variable x has been assigned the value 3, and the variable y has been assigned the value 2, so the algebraic expression you typed was evaluated using those values, and the result has been stored in the variable ans.

  1. Enter

         x
  2. Enter

         y

In general, to see what is stored in a variable, just type its name.

  1. Enter

         z=5;
  2. Enter

         z

Note the effect of the semicolon; use it when you do not want to see your input echoed. The computer still knows what is stored in the variable z.

  1. Enter

         2+x-y*z
  2. Enter

         ans
  3. Enter

         ans + 3
  4. Enter

         2*ans
  5. To see what variable names you have used in your current session, enter

         who
  6. Enter

         a = 3;
         b = 5;
         c = 1;
         discr = b^2 - 4*a*c;
  7. Enter

         discr
  8. Enter

         DISCR

    Is MATLAB case-sensitive?

There may in fact be times when you want to use the letter a to represent a real number and the letter A to represent a matrix, and MATLAB will let you do that. Remember, if you ever forget what variables are in current use, use the who command. (Or, for more information, use the whos command!)

  1. Enter

         sqrt(discr)
  2. Enter

         x = (-b + sqrt(discr)) / (2*a)

You will make use of vector variables on many ocassions in MATLAB. A vector is just a list of numbers. To enter a vector, enclose its components in square brackets and separate them by commas or blanks. This gives you a "row vector." which displays horizontally. You can make it into a "column vector" by putting a single quote mark after it.

  1. Enter

         x = [3, -1, 4, 2]
  2. Enter

         x'      % Transpose

You can do arithmetic operations on all components of a vector at once. This is especially important when you want to plot the graph of a function.

  1. Enter

         2*x
  2. Enter

         x/3

In order to of raise all components of a vector to a power or divide or multiply one vector by another, you must use the dot operator.

  1. Enter

         x^3     % Note error message
  2. Enter

         x.^3     % Note the use of the dot
  3. Enter

         3.^x
  4. Enter

         x/(2+x)
  5. Enter

         x./(2+x)     % Note the use of the dot

Make sure you understand what is going on before continuing to the next part of the tutorial.

Go to CCP Homepage Go to Materials Page Go to Engineering Mathematics Materials Go to Table of Contents
Go Back One Page Go Forward One Page


| CCP Home | Materials | Engineering Mathematics | Module Contents | Back | Forward |

modules at math.duke.edu Copyright CCP and the author(s), 2000