Skip to main content
Engineering LibreTexts

4.5: Arithmetic Operations

  • Page ID
    55647
  • \( \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}}\)

    We now turn to arithmetic operations, typically performed in Matlab on floating point numbers (though the operators also work on integers and indeed logicals through coercion, as discussed above).

    As already indicated, an operator is a function, syntactically represented by a symbol, which takes one or two input arguments, or parameters, or operands, and returns a result. The arithmetic operators are ^ (exponentiation), / (division), * (multiplication), + (addition), and - (subtraction). These operations do the obvious thing, applied either to data or variables.

    We consider addition: for data,

    >> 2 + 3
    ans =
        5
    

    or for variables

    >> x = 2; y = 3;
    >> x + y
    ans =
        5
    >>
    

    (Note the semi-colons suppress display of x and y; if we wish to confirm the value, we need only do (say) >> x without a semi-colon to evaluate x.) In more detail, the + operator takes the two values to the left and right and outputs as the answer (ans) the sum. The other arithmetic operators ^, /, *, and -, perform in a similar obvious fashion.

    We note that in the above \(\mathrm{x}+\mathrm{y}\) is an expression - here a single function, but more generally a composition of many operations - which is evaluated to yield the result ans. Note when we do an evaluation of an expression expr, MATLAB "finishes" the statement for us as ans \(=\operatorname{expr}\) - in other words, MATLAB assigns the result of the evaluation to a variable ans. We can in fact use this variable ans subsequently, but this is highly frowned upon since, as you can imagine, it is quite easy for the generic ans to be changed in unanticipated or unnoticed ways.

    Rather, if we wish to subsequently use the result of an evaluation, we should explicitly assign the output to a variable, say z: we need only do

    >> z = x + y
        z = 5
    >>
    

    which is a composition of the addition ( +) operator with the assignment \((=)\) operator: we evaluate the expression (or operation, or function) \(\mathrm{x}+\mathrm{y}\) and then assign the result of this evaluation to a variable \(z\).

    We repeatedly above refer to the addition "function." In fact, for most operators there is an "output = function_name (inputs)" syntax which is equivalent to the operator syntax. For instance, we may compute the sum of \(x\) and \(y\) as plus \((x, y)\). Obviously the operator form is much easier and more readable, however the plus contains the actual code which implements addition and furthermore provides a mechanism by which to change what we mean by addition for different data types (and in the object-oriented context, different classes). We do not recommend that you change the definition of plus for double data types - but in fact, it is quite easy to do, and could prove an effective form of industrial numerical sabotage.

    It is instructive to consider the statement

    >> z = z + 1
        z = 6
    >>
    

    which in fact serves quite often in iterative procedures (typically for \(z\) an integer, even if represented in MATLAB by a double). What does this statement do? First MATLAB evaluates the expression \(z+1\) to obtain the value 6 ; then operator \(=\) assigns this value 6 to the address (variable) \(z\). Although mathematically \(z=z+1\) appears nonsensical, it is important to remember that in \(z=z+1\) the \(=\) is the assignment operator and not an equal sign. (Of course, MATLAB contributes to the confusion in very next line, \(z=6\), by using the \(=\) sign in the convention mathematical sense of equality.)

    Up to this point we have composed operators for which we did not need to worry about the order in which we performed the operations. For example, \(3+4+5\) can be evaluated either as \((3+4)+5\) (i.e., \(3+4\) first, then \(+5\) ). (In fact, this is not quite true in finite precision, as we discuss in the next section.) On the other hand, \(3 * 2 \wedge 4\) gives quite different results if we perform either the \(*\) first or the first. MATLAB thus provides rules of precedence for the order in which operators are evaluated: first the items in parentheses, from the inside out; then \(;\); then \(/\) and \(*\); then \(+\) and \(-\). The mnemonic is PEDMAS (not very mnemonic, in fact). If the rules of precedence do not not dictate a unique order then MATLAB operates from left to right.

    The easiest way to avoid misinterpretation of your intentions is to liberally use parentheses, and for readability to always include ample spaces within and between your parentheses. The evaluation of \(3 * 2^{\wedge} 4\) gives us opportunity to introduce the notion of a bug. If you intended ( \(3 * 2)^{\wedge} 4\) but simply wrote \(3 * 2 ^{\wedge} 4\) MATLAB would do something different from your intentions. This is not MATLAB ’s fault, but your fault. There are many kinds of bugs: a statement may in fact not correspond to valid MATLAB syntax and the statement will not even be interpreted; a statement may correspond to valid MATLAB syntax but may do something other than intended by the user - as in our \(3 * 2^{\wedge} 4\) example; the statement may do what the users intends but in fact to achieve the desired end the user’s intentions are not correct - this occurs quite frequently in the context of numerical computation.


    This page titled 4.5: Arithmetic Operations is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Masayuki Yano, James Douglass Penn, George Konidaris, & Anthony T Patera (MIT OpenCourseWare) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.