GEOSC 444
Matlab Application for Geoscience

Lesson 1: Help, arithmetic, precedence

PrintPrint

Syntax introduced:

helplookfor-*/\^( )

Time to introduce my two favorite MATLAB commands: help and lookfor. If you type help commandName MATLAB will print to the screen the manual page for that command together with examples. This is a great resource, but it only works perfectly well if you already know the name of the command and you are merely wanting to double check its usage. Sometimes you can't remember the exact name of the command but you have a guess, or at least you have an idea that there is a command that does something similar to what you want. In this case lookfor is your new best friend because it will find and list all the commands that have part of the word you typed. Try it out for yourself!

Arithmetic

MATLAB works like a calculator and supports the obvious math operations commonly found on your keyboard such as + (addition) -(subtraction) *(multiplication) / \ (left and right division). Use ^ to raise a number to a power. For example, if you type a=3;b=4;c=b^a MATLAB reports back to you that the value of c is 64. That's because you set a equal to 3 and you set b equal to 4 and then you set c equal to b raised to the power of a. 4 cubed is 64, so we're good.

Other algebraic expressions work as expected. For example, raising a number to a negative power is equivalent to writing 1 over that number raised to the same power. For example, following on our previous work in which a = 3 and b = 4:

>>a^(-b)
ans =
    0.0123
>> 1/(a^b)
ans =
    0.0123

Operation precedence

Precedence refers to the order in which mathematical operations are executed. If you can recall back to any algebra classes you took before college, you probably remember the rules of precedence. MATLAB follows the standard rules, which are

  • expressions are evaluated left to right
  • exponentiation is first
  • multiplication and division are next
  • addition and subtraction are last

If you want to change the order, then use parentheses. Expressions are evaluated from the innermost set of parentheses outwards, with the rules listed above applying within each nested set of parentheses.

Check yourself!