Introduction to MATLAB Software
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/a for scalars, so 2\10 is 10/2=5. The figure follows one expression through these rules, one operation per step.
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)=2.
- 2^3^2 returns 64, because it is (2^3)^2. Handwritten 232 means 29=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
type (5 + 3)/(2*4), which returns 1. Typing 5 + 3/2*4 returns 11, because MATLAB computes 3/2=1.5, then 1.5×4=6, then 5+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=7. A few conventions decide whether a function gives the number you expect:
Typed | Meaning | Displayed (format short) |
|---|---|---|
sqrt(2) | 2 | 1.4142 |
exp(1) | e1 | 2.7183 |
log(100) | natural logarithm ln100 | 4.6052 |
log10(100) | base-10 logarithm | 2 |
sin(90) | sine of 90 radians | 0.8940 |
sind(90) | sine of 90 degrees | 1 |
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
A lab group measures a length L=1.5 m and uses g=9.81 m/s2. MATLAB stores only numbers, so the group tracks units themselves: with L in metres and g in m/s2, 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:
The function call then reduces to one value: 0.152905=0.391031. What remains is a chain of multiplications, evaluated ________:
MATLAB displays ans = 2.4569. This line matches the formula exactly, and the units work out: m/(m/s2)=s. The period is T=2.457 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
The remaining * and / share a level and are applied left to right:
MATLAB displays ans = 3.0091. The line computed 2πL/g: only g was square-rooted, not the ratio L/g. The units, m/m/s2=m1/2s, are not a time. As a check, the wrong result divided by the correct one is 3.0091/2.4569=1.2247=1.5, exactly the extra factor L that appears because L was left outside the square root instead of being rooted along with g.
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.152905. Next is the power, and the exponent is only the single number after ^, which is 1:
The /2 is a separate division on the same level as *. With multiplication and division left to right:
MATLAB displays ans = 0.4804, which is π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 s.
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.
Multiplication and division * and /
2.
Exponentiation ^
3.
Parentheses ( )
4.
Addition and subtraction + and -
5.
Unary minus, as in -x
You type the following line at the MATLAB prompt to compute a length in metres:
d = 20 - 9/4*2^3 + 1.5What value, in metres, does MATLAB assign to d? Give your answer to one decimal place.