MATLAB Expression Evaluation

When you type a line such as 3 + 4*2^2/8 - 1 at the MATLAB prompt and press Enter, MATLAB does not simply read it left to right like a sentence. It applies fixed rules to decide which operation happens first, reduces the line to a single number and displays it. Once you know those rules you can translate a formula from paper into one line of code, predict what MATLAB will return, and recognize a line that runs without error but gives the wrong number.

Operators and precedence

MATLAB's scalar arithmetic operators are +, -, *, /, \ and ^. Operator precedence is the ranking that decides which operator is applied first when several appear in one expression. Spaces do not change it. For scalar arithmetic the ranking is:


Level

Operators

Example

Result

1 (first)

parentheses ( )

(2 + 3)*4

20

2

power ^

2*3^2

18

3

unary minus -

-3^2

-9

4

multiplication *, right division /, left division \

2\10

5

5 (last)

addition +, subtraction -

1 + 2*3

7


Left division a\b means b/ab/a for scalars, so 2\10 is 10/2=510/2 = 5. The figure follows one expression through these rules, one operation per step.


Six stacked boxes show the expression 3 + 4 times 2 squared over 8 minus 1 being reduced one operation at a time to 4.



Left-to-right evaluation

Left-to-right evaluation resolves ties: operators on the same level are applied in the order they appear, reading from the left. Two results regularly surprise students:

  • 12/3*2 returns 8, because it is (12/3)*2. It is not 12/(3⋅2)=212/(3 \cdot 2) = 2.
  • 2^3^2 returns 64, because it is (2^3)^2. Handwritten 2322^{3^2} means 29=5122^9 = 512, so type 2^(3^2) for that.

The practical consequence is that every numerator, denominator or exponent containing more than one term needs its own parentheses. To evaluate


x=5+32⋅4x = \frac{5 + 3}{2 \cdot 4}


type (5 + 3)/(2*4), which returns 1. Typing 5 + 3/2*4 returns 11, because MATLAB computes 3/2=1.53/2 = 1.5, then 1.5×4=61.5 \times 4 = 6, then 5+6=115 + 6 = 11.



Built-in functions, constants and the displayed result

A function call is evaluated to a single value before the operators around it act on it, and its argument is a full expression evaluated first. So 2*sqrt(9) + 1 is 2×3+1=72 \times 3 + 1 = 7. A few conventions decide whether a function gives the number you expect:


Typed

Meaning

Displayed (format short)

sqrt(2)

2\sqrt{2}

1.4142

exp(1)

e1e^1

2.7183

log(100)

natural logarithm ln⁡100\ln 100

4.6052

log10(100)

base-10 logarithm

2

sin(90)

sine of 90 radians

0.8940

sind(90)

sine of 90 degrees

1

pi

π\pi

3.1416


  • log is the natural logarithm; use log10 for base 10.
  • sin, cos and tan expect radians. The versions ending in d (sind, cosd, tand) expect degrees.
  • pi is built in, but e is not: typing e produces an unrecognized-variable error, so write exp(1).
  • E-notation is part of a single number, not an operation: 2.2e3 is 2200, so 1/2.2e3 returns 4.5455e-04.

If you do not assign the result to a name, MATLAB stores it in ans and shows ans = .... Ending the line with a semicolon suppresses the display, but the expression is still evaluated. The command format short (the default) shows four digits after the decimal point, and format long shows fifteen, so pi appears as 3.1416 or 3.141592653589793. The format changes only what you see; the stored value keeps full double precision either way.

What to remember

  • MATLAB applies parentheses first, then ^, then unary minus, then *, /, \, then +, -.
  • Ties are broken left to right, including for ^: 2^3^2 is 64.
  • Put parentheses around every multi-term numerator, denominator and exponent.
  • log is the natural log, trig functions use radians unless they end in d, and pi exists while e does not.
  • The display format changes the digits you see, not the value MATLAB stores.

Example: Evaluating a pendulum formula in MATLAB

The period of a simple pendulum swinging through small angles is


T=2πLgT = 2\pi\sqrt{\frac{L}{g}}


A lab group measures a length L=1.5 mL = 1.5\ \text{m} and uses g=9.81 m/s2g = 9.81\ \text{m/s}^2. MATLAB stores only numbers, so the group tracks units themselves: with LL in metres and gg in m/s2\text{m/s}^2, the result is in seconds. Three group members each type one line at the prompt.


a) The first member types 2*pi*sqrt(1.5/9.81). What does MATLAB display in format short, and is it the period?


The argument of sqrt is evaluated first, because it sits inside the function's parentheses:


1.59.81=0.152905\frac{1.5}{9.81} = 0.152905


The function call then reduces to one value: 0.152905=0.391031\sqrt{0.152905} = 0.391031. What remains is a chain of multiplications, evaluated ________:


2×π=6.2831856.283185×0.391031=2.456920\begin{aligned} 2 \times \pi &= 6.283185 \\ 6.283185 \times 0.391031 &= 2.456920 \end{aligned}


MATLAB displays ans = 2.4569. This line matches the formula exactly, and the units work out: m/(m/s2)=s\sqrt{\text{m}/(\text{m/s}^2)} = \text{s}. The period is T=2.457 sT = 2.457\ \text{s}.


b) The second member types 2*pi*1.5/9.81^0.5. What does MATLAB display, and what formula did this line actually compute?


Power has higher precedence than ________, so MATLAB first computes


9.810.5=3.1320929.81^{0.5} = 3.132092


The remaining * and / share a level and are applied left to right:


2×π=6.2831856.283185×1.5=9.4247789.424778/3.132092=3.009100\begin{aligned} 2 \times \pi &= 6.283185 \\ 6.283185 \times 1.5 &= 9.424778 \\ 9.424778 / 3.132092 &= 3.009100 \end{aligned}


MATLAB displays ans = 3.0091. The line computed 2πL/g2\pi L/\sqrt{g}: only gg was square-rooted, not the ratio L/gL/g. The units, m/m/s2=m1/2 s\text{m}/\sqrt{\text{m/s}^2} = \text{m}^{1/2}\,\text{s}, are not a time. As a check, the wrong result divided by the correct one is 3.0091/2.4569=1.2247=1.53.0091/2.4569 = 1.2247 = \sqrt{1.5}, exactly the extra factor L\sqrt{L} that appears because LL was left outside the square root instead of being rooted along with gg.


c) The third member types 2*pi*(1.5/9.81)^1/2, intending the exponent one half. What does MATLAB display, and how should the line be fixed?


Parentheses come first: (1.5/9.81) is 0.1529050.152905. Next is the power, and the exponent is only the single number after ^, which is 1:


0.1529051=0.1529050.152905^{1} = 0.152905


The /2 is a separate division on the same level as *. With multiplication and division left to right:


2×π=6.2831856.283185×0.152905=0.9607320.960732/2=0.480366\begin{aligned} 2 \times \pi &= 6.283185 \\ 6.283185 \times 0.152905 &= 0.960732 \\ 0.960732 / 2 &= 0.480366 \end{aligned}


MATLAB displays ans = 0.4804, which is πL/g\pi L/g rather than the period. Fix it by grouping the exponent, 2*pi*(1.5/9.81)^(1/2), or by using 2*pi*sqrt(1.5/9.81) as in part a). Both return 2.4569, so T=2.457 sT = 2.457\ \text{s}.

At the MATLAB prompt you type


MATLAB
-5^2 + 10


What value does MATLAB display?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

When MATLAB evaluates an arithmetic expression typed at the command line, it applies operators in a fixed order of precedence. Arrange the following operator groups from the one MATLAB applies first (highest precedence) to the one it applies last (lowest precedence).

  1. 1.

    Multiplication and division * and /

  2. 2.

    Exponentiation ^

  3. 3.

    Parentheses ( )

  4. 4.

    Addition and subtraction + and -

  5. 5.

    Unary minus, as in -x

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

You type the following line at the MATLAB prompt to compute a length in metres:


MATLAB
d = 20 - 9/4*2^3 + 1.5


What value, in metres, does MATLAB assign to d? Give your answer to one decimal place.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice