MATLAB Lesson 1 - Arithmetic

Arithmetic operations

The most common arithmetic operations in MATLAB are

Operation Symbol
Addition +
Subtraction -
Multiplication *
Division /
Powers ^

This is just like on a calculator, in spreadsheets (Excel) and most programming languages.

Order of Operations

In MATLAB, and many other programming languages, operations are performed in the following order:   

  1. expressions in brackets: (   ) ;
  2. powers: ^ ;
  3. multiplication and division: * , / ;
  4. addition and subtraction: + , - .
Operations of the same precedence, for example multiplication and division, are evaluated from left to right.

In ordinary written mathematics we sometimes leave out brackets and rely on the fact that an intelligent human will understand where they ought to go. In contrast, computers obey exactly the rules for evaluating expressions. If you are unsure, adding some extra brackets (parentheses) will not hurt.

Examples

To see how MATLAB will apply these rules, try typing the following commands in a MATLAB command window.  The same arithmetic operators are used in each example, but the result may be different.

You do not need to type the MATLAB prompt

>> 
which just indicates that MATLAB is waiting for you to type a command.

 An example with addition and multiplication. Which has higher precedence?

In the first case the multiplication has higher precedence than the addition so 2 * 3 = 6 is calculated first, then 1 is added to give 7.

In the second case the brackets force the 1 and 2 to be added first giving 3, which is then multiplied by 3 to give 9.

>>  1+2*3


>>  (1+2)*3

 

An example with addition and division

In the first case the division has higher precedence so 4 / 2 is evaluated first to give 2, then 1 is added to give 3.

The result is the same as the first case. Why?

The brackets force the addition to be evaluated first to give 3, then 4 is divided by 3 to give 1.3333

>>  4/2+1

>>  1+4/2

>>  4/(2+1)

 

An example with powers and division

In the first case 8 is squared first to give 64, which is then divided by 3 to give 21.3333  

In the second case, 8 is raised to the power 2/3 giving 4.

>>  8^2/3

>>  8^(2/3)

 

Remember that operations of the same precedence are evaluated left to right. For example, try
In the first case, 12 is divided by 2 first to give 6, then 6 is divided by 3 to give 2.

In the second case the brackets force 2 * 3 = 6 to be evaluated first, then 12 / 6 = 2. This version is usually clearer.

>>  12/2/3

>>  12/(2*3)

 

For another example, try

In the first case 43 = 64 is evaluated first, which is then squared to give 4096.

In the second case 32=9 is evaluated first and then 49=262144.

This is the same as the first case, but clearer.

>>  4^3^2

>>  4^(3^2)

>>  (4^3)^2

 

One potentially confusing operation is when minus - is used with just one argument to indicate a change in sign. For example, try
The -3 is treated as a number, so you get -6, but this is unclear.
>>  2*-3

So use brackets to make it clear what you mean.  Instead type
>>  2*(-3)

 

One more example with minus used to indicate a negative number.
Again -3 is treated as a number, so 2-3 = 1/8 = 0.125
>>  2^-3

Use brackets to make it clear what you mean.  Instead type
>>  2^(-3)

 

Self-test Exercise

Write a MATLAB command to enter a fraction with 17.1 + 20.3 in the numerator and 36.5 + 41.8 in the denominator.

Note that the answer is NOT "17.1 + 20.3/36.5 + 41.8". Why not?

Answer: (17.1 + 20.3) / (36.5 + 41.8)
You must use brackets, otherwise the division is done before either addition.

Use the mouse to select the text between the word "Answer" and here to see the answer.

Summary

MATLAB evaluates arithmetic expressions precisely by the rules for order of precedence.

Use brackets as needed to get the correct expression or make the expression clear!