<img src="https://d5nxst8fruw4z.cloudfront.net/atrk.gif?account=iA1Pi1a8Dy00ym" style="display:none" height="1" width="1" alt="" />

# 3.1: Basic Mathematical Operations

Difficulty Level: At Grade Created by: CK-12

## Operations and Expressions

An m-file 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, x\begin{align*}x\end{align*} and y\begin{align*}y\end{align*} 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
xy\begin{align*}x - y\end{align*}
x - y

x+y\begin{align*}x + y\end{align*}
x + y

xy\begin{align*}xy\end{align*}
x*y

xy\begin{align*}\tfrac{x}{y}\end{align*}
x/y

xy\begin{align*}x^y\end{align*}
x^y

ex\begin{align*}e^x\end{align*}
exp(x)

log10(x)\begin{align*}\log_{10}(x)\end{align*}
log10(x)

ln(x)\begin{align*}\ln(x)\end{align*}
log(x)

log2(x)\begin{align*}\log_2(x)\end{align*}
log2(x)

cos(x)\begin{align*}\cos(x)\end{align*}
cos(x)

sin(x)\begin{align*}\sin(x)\end{align*}
sin(x)

x\begin{align*}\sqrt{x}\end{align*}
sqrt(x)


Expressions are formed from numbers, variables, and these perations. 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\begin{align*}5^2 + 4^2\end{align*}
5^2+4^2

41

(5+4)2\begin{align*}(5 + 4)^2\end{align*}
(5+4)^2

81

2+345\begin{align*}\tfrac{2+3}{4-5}\end{align*}
(2 + 3)/(4 - 5)

log10(100)\begin{align*}\log_{10}(100)\end{align*}
log10(100)

2

ln(4(2+3))\begin{align*}\ln(4(2 + 3))\end{align*}
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

You could split the expression 2+345\begin{align*}\tfrac{2+3}{4-5}\end{align*} over multiple lines as follows:
(2+3)...

/(4-5)


### Notes/Highlights Having trouble? Report an issue.

Color Highlighted Text Notes

Show Hide Details
Description
Tags:
Subjects: