Skip to main content
Engineering LibreTexts

1.6: 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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    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.6: Variables and Assignment Statements is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform.