Skip to main content

Registration is now open for this year's LibreFest! Join us virtually the week of July 13.

Register here
Engineering LibreTexts

1.10: Numeric Expressions and Operations

  • Page ID
    134907
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

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

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \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{\longvect}{\overrightarrow}\)

    \( \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}\)

    A numeric expression is a combination of variables, constants, numbers, and operators that MATLAB evaluates to produce a value.

    Operator

    Meaning

    Example

    +

    addition

    5 + 3

    -

    subtraction or negation

    5 - 3 or -4

    *

    multiplication

    5 * 3

    /

    right division

    10 / 5

    \

    left division

    5 \ 10

    ^

    exponentiation

    5^2

     

    Example \(\PageIndex{1}\)

    Evaluation an expression.

    a = 5 + 23
    b = a / 4
    Solution
    a =
        28
    
    
    b =
        7

     

    Operator Precedence

    When MATLAB evaluates a numeric expression, it does not simply move from left to right and perform every operation in the order it sees it. Instead, MATLAB follows an order of operations, also called operator precedence. This is the same idea you may have seen in algebra: multiplication happens before addition, and parentheses can be used to force a certain part of an expression to be evaluated first.

    This matters because MATLAB will always follow its own precedence rules, even if the expression is not written in the way you intended. A program may run without an error but still produce the wrong answer if the expression is grouped incorrectly. For this reason, operator precedence is not just a math topic; it is also a programming accuracy topic.

    Basic precedence order: parentheses first, then exponentiation, then unary minus or negation, then multiplication/division, and finally addition/subtraction. When two operators have the same precedence, MATLAB generally evaluates the expression from left to right.

    Example \(\PageIndex{2}\)

    Try these expressions in the Command Window and predict the answer before pressing Enter:

    exp_1 = 2 + 7 * 2 - 1
    exp_2 = 4 * 2 ^ 3
    exp_3 = (4 * 2) ^ 3
    exp_4 = 20 / 5 * 2
    exp_5 = 20 / (5 * 2)
    Solution
    
    exp_1 = 15
    
    exp_2 = 32
    
    exp_3 = 512
    
    exp_4 = 8
    
    exp_5 = 2

     

    In the first expression, MATLAB multiplies 7 by 2 before it performs the addition and subtraction. So 2 + 7 * 2 - 1 becomes 2 + 14 - 1, which equals 15. In the second expression, exponentiation happens before multiplication, so 4 * 2 ^ 3 becomes 4 * 8, which equals 32. In the third expression, the parentheses change the order: MATLAB calculates 4 * 2 first, then raises the result to the third power.

    Notice also the difference between 20 / 5 * 2 and 20 / (5 * 2). In the first expression, division and multiplication have the same precedence, so MATLAB evaluates from left to right: 20 / 5 is 4, and 4 * 2 is 8. In the second expression, the parentheses force MATLAB to calculate 5 * 2 first, so the result becomes 20 / 10, which is 2.

    Best practice: use parentheses when the expression is even slightly complicated. Parentheses do not make your code slower in any meaningful way, but they make your intention clear to both MATLAB and the person reading your code. This is especially important in engineering formulas, where a missing pair of parentheses can completely change the result.

    For example, suppose you want to calculate the wind chill factor using this expression:

    WCF = 35.7 + 0.6*T - 35.7*(V^0.16) + 0.43*T*(V^0.16)

    The parentheses around V^0.16 are not strictly required in every location because exponentiation already has high precedence, but they help the reader see that V^0.16 is an important repeated part of the formula. When you start writing longer formulas, readability becomes part of correctness.

    Common mistake: writing a formula the way it appears on paper without thinking about grouping. For example, the mathematical expression 1 divided by 2a should be written as 1/(2*a), not 1/2*a. MATLAB interprets 1/2*a as (1/2)*a.

    Precedence level

    Operation

    1

    Parentheses

    2

    Exponentiation

    3

    Negation

    4

    Multiplication, right division, and left division

    5

    Addition and subtraction


    1.10: Numeric Expressions and Operations is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?