Skip to content Skip to navigation


You are here: Home » Content » Basic Mathematical Operations


Recently Viewed

This feature requires Javascript to be enabled.

Basic Mathematical Operations

Module by: Anders Gjendemsjø, Darryl Morrell. E-mail the authors

Based on: Using MATLAB by Anders Gjendemsjø

Summary: 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.)

Table 1: Some Common Scalar Mathematical Operations
Operation m-file
xyxy x-y
x+yxy x+y
xyxy x*y
xyxy x/y
xyxy x^y
exex exp(x)
log10xlog10x log10(x)
lnxlnx log(x)
log2xlog2x log2(x)
cosxx cos(x)
sinxx sin(x)
xx 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

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

Table 2: Example Expressions
formula MATLAB Expression Computed Value
52+42 52 42 5^2+4^2 41
5+42 54 2 (5+4)^2 81
2+345 23 45 (2 + 3)/(4 - 5) -5
log10100 log10 100 log10(100) 2
ln4×(2+3) ln 4 23 log(4*(2+3)) 2.9957

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.

Example 2

Splitting the expression 2+345 23 45 over multiple lines.


Content actions

Download module as:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens


A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks