<< Chapter < Page Chapter >> Page >
This module covers basic mathematical operations in an m-file environment.

Operations and expressions

An m-file environment has all of the standard arithmetic operations (addition, subtraction, etc.) and functions (sine, cosine, logarithm, etc.). The table lists the most commonly used operations; in this table, x and y are scalars . (A scalar is a single value, as opposed to a vector or matrix which consists of many values.)

Some common scalar mathematical operations
Operation m-file
x y x-y
x y x+y
x y x*y
x y x/y
x y x^y
e x exp(x)
log10 x log10(x)
ln x log(x)
log2 x log2(x)
x cos(x)
x sin(x)
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.

The Table below shows several mathematical formulas, the corresponding expressions, and the values that are computed for the expressions.

Example expressions
formula MATLAB Expression Computed Value
5 2 4 2 5^2+4^2 41
5 4 2 (5+4)^2 81
2 3 4 5 (2 + 3)/(4 - 5) -5
log10 100 log10(100) 2
ln 4 2 3 log(4*(2+3)) 2.9957

Got questions? Get instant answers now!

Useful tricks

These tricks are occasionally useful, especially when you begin programming with m-files.

  • 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.

Splitting the expression 2 3 4 5 over multiple lines. (2+3)... /(4-5)

Got questions? Get instant answers now!

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Freshman engineering problem solving with matlab. OpenStax CNX. Apr 23, 2007 Download for free at http://cnx.org/content/col10325/1.18
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Freshman engineering problem solving with matlab' conversation and receive update notifications?

Ask