Skip to main content
Library homepage
 
Engineering LibreTexts

1.3: Variables and Assignment Statements

  • Page ID
    85848
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Of course, MATLAB is good for more than just evaluating expressions. One of the features that makes MATLAB more powerful than a calculator is the ability to give a name to a value. A named value is called a variable.

    MATLAB comes with a few predefined variables. For example, the name pi refers to the mathematical quantity \(\pi\), which is approximately this:

    >> pi
    ans = 3.1416

    And if you do anything with complex numbers, you might find it convenient that both i and j are predefined as the square root of \(-1\).

    You can use a variable name anywhere you can use a number—for example, as an operand in an expression,

    >> pi * 3^2
    ans = 28.2743

    or as an argument to a function:

    >> sin(pi/2)
    ans = 1

    Whenever you evaluate an expression, MATLAB assigns the result to a variable named ans. You can use ans in a subsequent calculation as shorthand for “the value of the previous expression.”

    >> 3^2 + 4^2
    ans = 25
    
    >> sqrt(ans)
    ans = 5

    But keep in mind that the value of ans changes every time you evaluate an expression.

    Assignment Statements

    You can create your own variables, and give them values, with an assignment statement. The assignment operator is the equals sign (=), used like so:

    >> x = 6 * 7
    x = 42

    This example creates a new variable named x and assigns it the value of the expression 6 * 7. MATLAB responds with the variable name and the computed value.

    There are a few rules when assigning variables a value. In every assignment statement, the left side has to be a legal variable name. The right side can be any expression, including function calls. Almost any sequence of lower- and uppercase letters is a legal variable name. Some punctuation is also legal, but the underscore (_) is the only commonly used non-letter. Numbers are fine, but not at the beginning. Spaces are not allowed. Variable names are case sensitive, so x and X are different variables.

    Let’s look at some examples of assignment statements.

    >> fibonacci0 = 1;
    
    >> LENGTH = 10;
    
    >> first_name = 'bob'
    first_name = 'bob'

    The first two examples demonstrate the use of the semicolon, which suppresses the output from a command. In this case MATLAB creates the variables and assigns them values but displays nothing.

    The third example demonstrates that not everything in MATLAB is a number. A sequence of characters in single quotes is a string.

    Although i, j, and pi are predefined, you are free to reassign them. It’s common to use i and j for other purposes, but it’s rare to assign a different value to pi.

    Variables in the Workspace

    When you create a new variable, it appears in the Workspace window and is added to the workspace, which is a set of variables and their values.

    The who command prints the names of the variables in the workspace:

    >> x = 5;
    >> y = 7;
    >> z = 9;
    >> who
    
    Your variables are:
    
    x  y  z

    The clear command removes specified variables from the workspace:

    >> clear x
    >> who
    
    Your variables are:
    
    y z

    But be careful: if you don’t specify any variables, clear removes them all.

    To display the value of a variable, you can use the disp function:

    >> disp(z)
         9

    but it’s easier to just type the variable name:

    >> z
    z = 9

    Now that you’ve seen how to use them, let’s take a step back and think about why we’d use variables.

    Why Variables?

    There are a number of reasons to use variables. A big one is to avoid recomputing a value you use repeatedly. For example, if your computation uses \(e\) frequently, you might want to compute it once and save the result.

    >> e = exp(1)
    e = 2.7183

    Variables also make the connection between the code and the underlying mathematics more apparent. If you’re computing the area of a circle, you might want to use a variable named r:

    >> r = 3
    r = 3
    
    >> area = pi * r^2
    area = 28.2743

    That way, your code resembles the familiar formula \(a = \pi r^2\).

    You might also use a variable to break a long computation into a sequence of steps. Suppose you’re evaluating a big, hairy expression like this:

    p = ((x - theta) * sqrt(2 * pi) * sigma)^-1 * ...
    exp(-1/2 * (log(x - theta) - zeta)^2 / sigma^2)

    You can use an ellipsis to break the expression into multiple lines. Just enter ... at the end of the first line and continue on to the next. But often it’s better to break the computation into a sequence of steps and assign intermediate results to variables:

    shiftx = x - theta
    denom = shiftx * sqrt(2 * pi) * sigma
    temp = (log(shiftx) - zeta) / sigma
    exponent = -1/2 * temp^2
    p = exp(exponent) / denom

    The names of the intermediate variables explain their role in the computation: shiftx is the value of x shifted by theta, it should be no surprise that exponent is the argument of exp, and denom ends up in the denominator. Choosing informative names makes the code easier to read and understand, which makes it easier to debug.


    This page titled 1.3: Variables and Assignment Statements is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.