Scalar mathematics involves operations on
single-valued variables. Matlab provides support for scalar mathematics similar to that provided by a calculator.
Arithmetic Operation with Scalars
The symbols of arithmetic operations are:
Table: Arithmetic Operators
| Operation | MATLAB Symbol | Algebraic
form | Matlab
form | Example |
| Addition | + | a + b | a + b | 7+3=10 |
| Subtraction | - | a − b | a – b | 7-3=4 |
| Multiplication | * | a × b | a * b | 7*3=21 |
| Left-division | \ | b ÷ a | a \ b | 7\3=0.4286 |
| Right-division | / | a ÷ b | a / b | 7/3=2.33 |
| Exponentiation | ^ | ab | a ^ b | 7^3 (means 7^3= 343) |
Order of Precedence
MATLAB executes the calculations according to the order of precedence displayed below. This order is the same as used in most calculators.
Table: Order of Precedence
| Precedence | Mathematical Operation |
| First | Parenthess. For nested parenthess, the innermost are executed first. |
| Second | Exponentiation. |
| Third | Multiplication, division (Equal precedence). |
| Fourth | Addition and subtraction |
Numeric Display Formats
MATLAB by default displays only 4 decimals in the result of the calculations, However, MATLAB does numerical calculations in
double precision, which is 15 digits.
Table: Display Format
| Command | Description | Example |
format short
| Fixed-point with 4 decimal digits for:
0.001≤number≤1000 Otherwise display format short e | >> x=250/7;
>> format short
>> x
x =
35.7143
|
format long
| Fixed-point with 15 decimal digits for:
0.001≤number≤100
Otherwise display format long e
| >> x=250/7;
>> format long
>> x
x =
35.714285714285715
|
format short e
| Scientific notation with 4 decimal digits. | >> x=250/7;
>> format short e
>> x
x =
3.5714e+001
|
format long e
| Scientific notation with 15 decimal digits. | >> x=250/7;
>> format long e
>> x
x =
3.571428571428572e+001
|
format short g
| Best of 5-digit fixed or floating point. | >> x=250/7;
>> format short g
>> x
x =
35.714
|
format long g
| Best of 15-digit fixed or floating point. | >> x=250/7;
>> format long g
>> x
x = 35.7142857142857
|
format bank
| Two decimal digits. | >> x=250/7;
>> format bank
>> x
x =
35.71
|
| format compact | Suppresses many of the blank lines that appear in the output. |
| format loose | Adds empty lines (opposite of compact) |
Note: If the largest element of a matrix is larger than 10
3 or smaller than 10
-3, MATLAB applies a common scale factor for the short and long formats.
Elementary Math Built-in Function
Lists of some commonly used elementary MATLAB mathematical built-in functions are given in TABLES.
Table: Elementary Math Functions
| Function | Description | Example |
| sqrt(x) | Square root; x. | >> sqrt(81)
ans =
9
|
| exp(x) | Exponential; ex. | >> exp(3)
ans =
20.0855
|
| log(x) | Natural logarithm; ln(x). | >> log(200)
ans =
5.2983
|
| log10(x) | Common (base 10) logarithm; log(x)= log10(x). | >> log10(400)
ans =
2.6021
|
| abs(x) | Absolute value. | >> abs(-27)
ans =
27
|
| factorial (x) | The factorial function x!
(x must be a positive integer.) | >> factorial(4)
ans =
24
|
Trigonometric Math Functions:
Table : Trigonometric Math Functions
| Function | Description | Example |
| sin(x) | Sine; sin(x).
(x in radians) | >> sin(pi/6)
ans =
0.5000
|
| cos(x) | Cosine; cos(x).
(x in radians) | >> cos(pi/6)
ans =
0.8660
|
| tan(x) | Tangent; tan(x).
(x in radians) | >> tan(pi/6)
ans =
0.5774
|
| cot(x) | Cotangent; cot(x).
(x in radians) | >> cot(pi/6)
ans =
1.7321
|
| sec(x) | Secant; sec(x).
(x in radians) | >> sec(pi/6)
ans =
1.1547
|
| csc(x) | Cosecant; csc(x).
(x in radians) | >> csc(pi/6)
ans =
2.0000
|
| asin(x) | Inverse sine; sin–1 (x). | >> asin(1)
ans =
1.5708
|
| acos(x) | Inverse cosine; cos–1 (x). | >> acos(.5)
ans =
1.0472
|
| atan(x) | Inverse tangent; tan –1 (x). | >> atan(.5)
ans =
0.4636
|
| acot(x) | Inverse cotangent; cot –1(x). | >> acot(.5)
ans =
1.1071
|
| asec(x) | Inverse secant; sec –1 (x). | >> asec(.5)
ans =
0 + 1.3170i
|
| acsc(x) | Inverse cosecant; csc –1 (x). | >> acsc(.2)
ans =
1.5708 - 2.2924i
|
NOTE: Trigonometric functions are sind(x), cosd(x), tand(x) and others, in which
x is in
degree.
Table: Hyperbolic Functions
| Function | Description |
| cosh(x) | Hyperbolic cosine; cosh(x). |
| coth(x) | Hyperbolic cotangent; cosh(x)/sinh(x). |
| csch(x) | Hyperbolic cosecant; 1/sinh(x). |
| sech(x) | Hyperbolic secant; 1/cosh(x). |
| sinh(x) | Hyperbolic sine; sinh(x). |
| tanh(x) | Hyperbolic tangent; sinh(x)/cosh(x). |
Rounding Functions
Table: Rounding Functions
| Function | Description | Example |
| Round(x) | Rounds towards the nearest integer. | >> round(17/3)
ans =
6
|
| fix(x) | Rounds to the nearest integer toward zero. | >> fix(13/5)
ans =
2
|
| ceil(x) | Rounds to the nearest integer toward +∞. | >> ceil(9/5)
ans =
2
|
| floor(x) | Rounds to the nearest integer toward - ∞. | >> floor(-9/5)
ans =
-2
|
| sign(x) | Signum function. Returns 1 if x>0, -1 if x<0, and 0 if x=0. | >> sign(7)
ans =
1
|
| rem(x,y) | Returns the remainder after x is divided by y. | >> rem(9,5)
ans =
4
|