3.1: Basic Mathematical Operations
Operations and Expressions
An mfile environment has all of the standard arithmetic operations (addition, subtraction, etc.) and functions (sine, cosine, logarithm, etc.). Table 1 lists the most commonly used operations; in this table,
Operation  mfile 


x  y 

x + y 

x*y 

x/y 

x^y 

exp(x) 

log10(x) 

log(x) 

log2(x) 

cos(x) 

sin(x) 

sqrt(x) 
Expressions are formed from numbers, variables, and these operations. The operations have different precedences. The
^
operation has the highest precedence;
^
operations are evaluated before any other operations. Multiplication and division have the next highest precedence, and addition and subtraction have the lowest precedence. Precedence is altered by parentheses; expressions within parentheses are evaluated before expressions outside parentheses.
Example 1
Table 2 below shows several mathematical formulas, the corresponding expressions, and the values that are computed for the expressions.
Example Expressions formula MATLAB Expression Computed Value 52+42 5^2+4^2 41(5+4)2 (5+4)^2 812+34−5 (2 + 3)/(4  5)log10(100) log10(100) 2ln(4(2+3)) log(4*(2 + 3)) 2.9957
Useful Tricks
These tricks are occasionally useful, especially when you begin programming with mfiles.
 A semicolon added at the end of a line suppresses the output.
 Often it is useful to split input over multiple lines. To split a statement across multiple lines, enter three periods ... at the end of the line to indicate it continues on the next line.
Example 2

You could split the expression
2+34−5 over multiple lines as follows: 
(2+3)...

/(45)
Notes/Highlights Having trouble? Report an issue.
Color  Highlighted Text  Notes  

Show More 
Image Attributions
To add resources, you must be the owner of the section. Click Customize to make your own copy.