Expression, Index Expression, Reshape Elements (reshape), Is Element an Index (isindex) Arithmetic Operators Addition & Subtraction, Multiplication, Division Power, Unary Operations Left Division (ldivide) Matrix Left Division (mldivide), Subtraction (minus), Matrix Power (mpower), Matrix Right Division (mrdivide) Recursive Product (mtimes), Element-wise Recursive Product (times) Element-wise Right Division (rdivide), Addition of Elements (plus), Power (power) Unaryn or Equal (le), Less Than (lt) Not Equals (ne), Evaluation, Arithmetic, Absolute Value (abs), Ceiling (ceil), Truncate Fraction (fix) Geometry, Cartesian to Polar Conversion (cartpol), Polar to Cartesian Conversion (polcart), Spherical to Cartesian Conversion (sphcart), Cartesian to Spherical Conversion (cartsph), Logarithm, Natural Logarithm (log), Logarithm Base Ten (log), Unit Increment Logarithm (logp), Binary Base Logarithm (log) Exponential Base (e), Matrix, Transpose of Matrix (transpose) Complex Conjugate Transpose of Matrix (ctranspose) Dot Product (dot) Cross Product (cross) Determinant (det) Identity Matrix (eye), Eigenvalues (eig), Eigens (eigs), Inverse of Matrix (inv) Linear Equation Solver (linsolve) Type of Matrix (matrix type) Normalized Matrix (norm), Null Space Matrix (null), Orthogonal Basis (orth), Rank of Matrix (rank) Trace of Matrix (trace), Cholesky Matrix (chol), Inverse Cholesky Matrix (cholinv), Matrix Exponential (expm), Logarithmic Matrix (logm), Square Root of Matrix (sqrtm), Kronecker Product (kron) Diagonal Matrix (diag), Single Value Decomposition (svd) Lower Upper Decomposition (lu) Lower Upper Composition (qr), Length of Matrix (length) Special Functions, Bessel Function of First Kind (besselj), Bessel Function of Second Kind (bessely), Hyperbolic Bessel Function of First Kind (besseli) Hyperbolic Bessel Function of Second Kind (besselk), Bessel Function as Hankel Function (besselh), Beta Function (beta), Gamma Function (gamma), Error Function (erf), Complementary Error Function (erfc), Inverse Error Function (erfinv), Legendre Function (legendre) Differentiation Derivative (diff), Linear ODE Solver (lsode) Options For Linear ODE Solver (lsode options) Differential Algebraic System Solver (dassl) Differential Algebraic System Solver Options (dassl options) Differential Algebraic Equations (daspk) Differential Algebraic Equations Options (daspk options) Differential Algebraic System Root Solver (dasrt), Differential Algebraic System Root Solver Options (dasrt options) Integration Quadratic Integration (quad) Vectorized Quadratic Integration (quadv), Quadratic Lobatto’s Integration (quadl) Quadratic Gauss-Kronrod Integration (quadgk) Quadratic Clenshaw-Curtis Integration
15. 1.1. EXPRESSION 15
https://sites.google.com/view/arunumrao
1Mathematics
In this section, those functions would be discussed which are used in math-
ematical operation.
1.1 Expression
An expression is a group of statements or codes which accept input values
either from function’s parameters or from local variables, executes instructions
according to set rules and returns a value. While writing code, it should be
efficient, short, optimized and consolidated. for loop is slow than the vector
range. Therefor range is used instead of for loop. For example,
✞
1 >>> for(i=1:10) # loop method
>>> a(i) = sqrt (i);
3 >>> endfor
✌
✆
is less efficient than
✞
1 >>> sqrt (1:10) # vector method
✌
✆
1.1.1 Index Expression
An index expression allows to scale, select or replace an element from a vector
or matrix. Indices may be scalars, vectors, ranges, or the special operator ‘:’,
which may be used to select entire rows or columns. Row and column indices,
i.e. two parameters, are separated by comma.
✞
1 >>> A(<row indices >, <column indices >)
✌
✆
When only one parameter is passed to matrix variable to extract the sub matrix,
then at first, matrix is arranged in vector form in sequence of column vectors.
For example,
✞
1 >>> A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
>>> A(:)
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
16. 16 Mathematics
2
5
8
4
3
3
6
5
1
1
7
1
4
8
2
3
✌
✆
Now, only one parameter is supplied to vector variable, it is treated as indices
for elements and it returns a vector of elements present at that indice. Note
that, here index counting started from 1 rather than 0.
✞
>>> A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
2 >>> A(2) // element at index 2
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
5
✌
✆
Similarly, a range of indices can be given to matrix variable to extract a vector
of elements present at that index. In the following example, sub vectors are
returned by constructing elements present at indices from index 1 to index 2.
There are two different way to pass same indices. Here, indices are counted in
columnwise.
✞
1 >>> A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
>>> A(1:2) // 1st index to 2nd index
3 >>> A([1;2]) // 1st index and 2nd index
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
17. 1.1. EXPRESSION 17
https://sites.google.com/view/arunumrao
ans =
2
5
ans =
2
5
✌
✆
When both parameters are passed to matrix variable, then first parameter is
indices for rows and second parameter is for columns. Rows or columns may be
discrete indices or range of indices but should not beyond the dimension of the
matrix. In this case, matrix is not arranged in sequences of elements of column
vectors. See the below example.
✞
1 >>> A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
>>> A(2:4 ,3:4) // Row index from 2nd row to 4th row
3 // col index from 3rd col to 4th col
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
1 8
7 2
1 3
✌
✆
If both parameters are discrete indice, then row index are recurred for all col-
umn indices. See the following example, in which elements at second and third
column of first row, and elements at second and third column of fourth row
forms a sub matrix.
✞
1 >>> A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
>>> A([1,4], [2 ,3]) // 1st row - 2nd & 3rd columns
3 // 4th row - 2nd & 3rd columns
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
3 1
5 1
✌
✆
If same index number is used twice or more in row parameter, i.e. as [1,1], then
output sub matrix has two rows as part of row index ‘1’ of the given matrix.
18. 18 Mathematics
✞
>>> A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
2 >>> A([1,1], [2 ,3]) // 1st row - 2nd & 3rd columns
// 1th row - 2nd & 3rd columns
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
3 1
3 1
✌
✆
end operator is used to denote last index of the vector. For example
✞
>>> A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
2 >>> A(end)// last element of vector
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
3
✌
✆
The parameter of matrix variable may be controlled by range indices. For
example, odd elements of matrix and even elements of matrix can be extracted
by
✞
1 >>> A = [2 3 1 4; 5 3 1 8; 8 6 7 2; 4 5 1 3]
>>> A(1:2: end) // Odd elements vector
3 >>> A(2:2: end) // Even elements vector
✌
✆
✞
A =
2 3 1 4
5 3 1 8
8 6 7 2
4 5 1 3
ans =
2 8 3 6 1 7 4 2
ans =
5 4 3 5 1 1 8 3
✌
✆
19. 1.2. ARITHMETIC OPERATORS 19
https://sites.google.com/view/arunumrao
1.1.2 Reshape Elements (reshape)
It is used to scale the elements into new matrix form. The syntax of this function
is
✞
1 >>> reshape(<elements >, <n_x >, <n_y >, <n_z >)
✌
✆
The number of elements should be in same shape as required to put in all
dimensions. In other words if l, m and n are the dimensions then elements
number must be equal to l × m × n. Example is
✞
1 >>> reshape (1:8, 2, 2, 2)
✌
✆
✞
ans(:,:,1) =
1 3
2 4
ans(:,:,2) =
5 6
7 8
✌
✆
1.1.3 Is Element an Index (isindex)
isindex returns ‘true’ if supplied argument is an index element of current vector
otherwise returns ‘false’. The syntax of the function is
✞
>>> isindex(<vector >)
✌
✆
1.2 Arithmetic Operators
Numerical programming languages use double type floating point arithmetics.
Therefore each number is denoted as float point, i.e. 1.0 as 1.000000, 0.5 as
0.500000. Sometime, even zero is represented as 2.5 × 10−18
or as 13.2 × 10−19
etc. where these values are very very small and are close to zero. Here zero is
taken as comparative value rather than absolute value. For example, sometimes,
in numerical calculations, identity matrix is represented as
34. 20 Mathematics
1.2.1 Addition & Subtraction
Addition is denoted by ‘+’ operator. If operands are matrices then shape and
size of matrices should be same or broadcastable to the same shape. When
dot is prefixed to this operator (.+), then it converts into element-by-element
addition. Matrix addition and element-by-element wise addition is same.
✞
1 >>> A=[1 ,2 ,5;5 ,4 ,7;5 ,9 ,8]
>>> B=[5 ,7 ,4;6 ,9 ,5;8 ,5 ,9]
3 >>> A+B
>>> A.+B
✌
✆
✞
A =
1 2 5
5 4 7
5 9 8
B =
5 7 4
6 9 5
8 5 9
ans =
6 9 9
11 13 12
13 14 17
ans =
6 9 9
11 13 12
13 14 17
✌
✆
Similarly, ‘-’ and ‘.-’ represent to normal and elementwise subtraction respec-
tively.
1.2.2 Multiplication
Multiplication is denoted by ‘*’ operator. If operands are matrices of size m×n
and p× q respectively then n must equal to p. If dot is prefixed to this operator
(.*), then it converts into element-by-element multiplication.
✞
>>> A=[1 ,2 ,5;5 ,4 ,7;5 ,9 ,8]
2 >>> B=[5 ,7 ,4;6 ,9 ,5;8 ,5 ,9]
>>> A*B
4 >>> A.*B
✌
✆
✞
A =
1 2 5
5 4 7
35. 1.2. ARITHMETIC OPERATORS 21
https://sites.google.com/view/arunumrao
5 9 8
B =
5 7 4
6 9 5
8 5 9
ans =
57 50 59
105 106 103
143 156 137
ans =
5 14 20
30 36 35
40 45 72
✌
✆
Note that vector multiplication of matrices (A × B) is purely different from
the elementwise multiplication (Aij × Bij). Mathematically, vector multiplica-
tion and elementwise multiplication are given by
Cij =
X
AijBji; Cij = AijBij
Again, note that A ∗ B may or may not be equal to B ∗ A.
✞
>>> A*B
2 >>> B*A
✌
✆
✞
ans =
57 50 59
105 106 103
143 156 137
ans =
60 74 106
76 93 133
78 117 147
✌
✆
1.2.3 Division
There is two type of divisions. Right division (/) and left division (). Element-
wise right division is performed by ‘./’ and elementwise left division is performed
by ‘.’. The division of matrix is performed as
A/B = A × B−1
; AB = A−1
× B
In right division, left operand is divided by right operand. In left division, right
operand is divided by left operand.
✞
>>> A=[1 ,2 ,5;5 ,4 ,7;5 ,9 ,8]
2 >>> B=[5 ,7 ,4;6 ,9 ,5;8 ,5 ,9]
36. 22 Mathematics
>>> A/B
4 >>> AB
>>> A./B
✌
✆
✞
A =
1 2 5
5 4 7
5 9 8
B =
5 7 4
6 9 5
8 5 9
ans =
-13.00000 9.85714 0.85714
-5.00000 3.85714 0.85714
-13.00000 10.71429 0.71429
ans =
-0.34524 0.16667 -0.39286
0.20238 -1.16667 0.67857
0.98810 1.83333 0.60714
ans =
0.20000 0.28571 1.25000
0.83333 0.44444 1.40000
0.62500 1.80000 0.88889
✌
✆
1.2.4 Power
ˆ or ‘**’ are called exponent operator. If n is an index or a degree, or power
of base x then exponents is represented by xn
. If |n| ≥ 1, then exponent
means “base is multiplied by itself n times”, and if |n| < 1, then it is called
nth
root of base x. To find the kth
root of base number x, we use ˆ operator
as xk
. Exponent or power operator ˆ or ‘**’ are used to find the solutions of
expressions like “base to the power” in both, elementwise operation and matrix
operation. Mathematically, matrix operation is
A4
= A × A × A × A
Where, A × A is vector product of a matrix to itself. The rightward operand
of this operator should be a number not a matrix for matrix operation. Ele-
mentwise power operator is performed by .ˆ or ‘.**’. In case of elementwise
power operation, both operands may be matrices. Elementwise power operator
is performed as
Cij = A
Bij
ij
✞
>>> A=[1 ,2 ,5;5 ,4 ,7;5 ,9 ,8]
2 >>> B=[5 ,7 ,4;6 ,9 ,5;8 ,5 ,9]
37. 1.2. ARITHMETIC OPERATORS 23
https://sites.google.com/view/arunumrao
>>> C=A ** 4 // matrix operation
4 >>> D=A .** B // elementwise operation
✌
✆
✞
A =
1 2 5
5 4 7
5 9 8
B =
5 7 4
6 9 5
8 5 9
C =
9906 13837 17087
17310 24083 29809
24000 33388 41276
D =
1 128 625
15625 262144 16807
390625 59049 134217728
✌
✆
For matrix operation, it uses following algorithm. Assume a matrix of order
2 × 2
A =
1 2
3 4
Now its eigenvalues are given by |A − λI| = 0 i.e.
53. = 0
Or
(1 − λ) × (4 − λ) − 6 = 0
On solving it
λ = −0.372281; 5.372281
Or eigenvalues in matrix form, when they are arranged in descending order is
d =
5.372281 0.000000
0.000000 −0.372281
For λ = 5.372281, eigenvector (v1) is (A − λI)v1 = 0. So,
1 − 5.372281 2
3 4 − 5.372281
x
y
= 0
54. 24 Mathematics
−4.372281 2
3 −1.372281
x
y
= 0
Or
−4.372281x + 2y = 0; 3x − 1.372281y = 0
To get solutions, put x = 1 in −4.372281x + 2y = 0, we get the value of
y = 2.186140. To get eigenvectors, we shall normalize these two values as
x =
1
√
12 + 2.1861402
; y =
2.186140
√
12 + 2.1861402
It gives, x = 0.415973 and y = 0.909376. As coefficients of above two eigenvector
equations are of opposite signs, hence values of x and y shall be either both
negative or both positive. Now, we shall submit x and y values in equation
g = 3x − 1.372281y to get minimum positive value.
g = 3 × 0.415973 − 1.372281 × 0.909376 = 0.000000407
Taking sign convention, the other possible set of solution be x = −0.415973 and
y = −0.909376.
g = 3 × −0.415973 − 1.372281 × −0.909376 = 0.000000407
When x = −0.415973 and y = −0.909376, we have positive f value. This gives
first eigenvector corresponding to λ = 5.372281.
v1 =
−0.415973
−0.909376
For λ = −0.372281, eigenvector (v2) is (A − λI)v2 = 0. So,
1 − (−0.372281) 2
3 4 − (−0.372281)
x
y
= 0
1.372281 2
3 4.372281
x
y
= 0
Or
1.372281x + 2y = 0; 3x + 4.372281y = 0
As coefficients of above two eigenvector equations are of same signs, hence values
of x and y shall be in opposite signs. On solving these two algebraic equations,
as explained for λ = 5.372281, we have x = −0.824564 and y = 0.565767 or
x = 0.824564 and y = −0.565767. This gives second eigenvector corresponding
to λ = −0.372281.
v2 =
−0.824564
0.565767
55. 1.2. ARITHMETIC OPERATORS 25
https://sites.google.com/view/arunumrao
The corresponding eigenvector matrix from above two eigenvectors (v1 and v2)
is
v =
−0.415973 −0.824564
−0.909376 0.565767
Note that, each column of eignevectors is arranged to the corresponding eigen-
values. Now, the matrix A can be written as A = v × d × v−1
. The square root
of the matrix is given by
A
1
2 = v × d
1
2 × v−1
1.2.5 Unary Operations
−A and +A are known as negation of an operand and unary plus to an operand
respectively. A′
is called complex conjugate transpose, which is equal to conj(x.’).
Again, A.′
is transpose of a Vector.
1.2.6 Left Division (ldivide)
ldivide performs element-by-element wise left division of two arguments. As-
sume two ordered elements as (a, b). In the left division, division is performed
as b ÷ a. It is equal to the operation x.y.
✞
ldivide(x, y)
✌
✆
For example
✞
1 ldivide (2, 4);
✌
✆
✞
ans =
2
✌
✆
1.2.7 Matrix Left Division (mldivide)
mldivide performs left division of between two matrices. It is equal to the
operation x=AB. If x is a non singular square matrices then A−1
× B is unique
solution of Ax = B. Here, matrix A must be either of a nonsingular square
matrix or a matrix, in which there are more rows than columns, such that
A′
× A is nonsingular or a matrix, in which there are more columns than rows,
such that A × A′
is nonsingular matrix.
✞
mldivide (x, y)
✌
✆
For example
✞
1 mldivide ([2,4], [3 ,7]);
✌
✆
56. 26 Mathematics
In this case we are trying to get the result A−1
× B where A = [2, 4] and B =
[3, 7]. As A and B are not nonsingular square matrices, as well as m n, i.e.
number of rows are lesser than the number of columns. In this case, computation
method used is
A′
× ((A × A′
) B)
If m n, i.e. number of rows are greater than the number of columns, then
computation method, that is used is
(A′
× A)−1
× (A′
× B)
For the given example, A × A′
= 2 and A′
× ((A × A′
) B) is
✞
ans =
0.30000 0.70000
0.60000 1.40000
✌
✆
1.2.8 Subtraction (minus)
minus performs subtraction of second matrix from the first matrix. It is equal
to the operation x-y. Its syntax is
✞
1 minus(x, y)
✌
✆
If two arguments are in matrix form, then second argument is subtracted from
first argument as element-by-element method, i.e. element-wise. See the exam-
ple given below:
✞
1 minus([2, 4], [3, 7]);
✌
✆
✞
ans =
-1 -7
✌
✆
1.2.9 Matrix Power (mpower)
mpower performs the power of second argument over the first argument. It is
equal to the operation xˆy. Mathematically, x to the power y is represented as
xy
. Syntax of this function is
✞
mpower(x, y)
✌
✆
For example
✞
1 mpower (3, 3);
✌
✆
57. 1.2. ARITHMETIC OPERATORS 27
https://sites.google.com/view/arunumrao
✞
ans =
27
✌
✆
1.2.10 Matrix Right Division (mrdivide)
mrdivide performs right division of two arguments. Its syntax is
✞
mrdivide (x, y)
✌
✆
It is equal to the operation x=A/B. If x is nonsingular square matrix then x
has a unit solution as A × B−1
for the relation Bx = A. If A and B are not
square matrices then used computation method is
(A ∗ B′
) × (B ∗ B′
)−1
For example
✞
1 mrdivide ([2, 4], [3, 7]);
✌
✆
has (A ∗ B′
) = 38 and (B ∗ B′
) = 58 which gives answer from the computation
method (A ∗ B′
) × (B ∗ B′
)−1
as
✞
ans =
0.58621
✌
✆
1.2.11 Recursive Product (mtimes)
mtimes performs recursive product of base to the multiplier. This function is
used as
✞
mtimes(x, n)
✌
✆
The mathematics behind this calculation is
xi = x × n
Where, xi is each element of the first argument of this function. n is value of
second argument. For example
✞
1 mtimes([2, 1], 4);
✌
✆
✞
ans =
8 4
✌
✆
In this function, multiplier (second argument) can not be a vector.
58. 28 Mathematics
1.2.12 Element-wise Recursive Product (times)
times performs recursive product of same element upto second argument times
in manner of element-by-element wise. Here base and multiplier may be vector
or matrix. This function is used as
✞
times(x, n)
✌
✆
For example
✞
1 times([2,1], [1, 4]);
✌
✆
✞
ans =
2 4
✌
✆
1.2.13 Element-wise Right Division (rdivide)
mrdivide performs element-by-element wise right division of first argument by
the second argument of this function. It is equal to the operation x./y.
✞
rdivide (x, y)
✌
✆
See the example below:
✞
1 A=[2 ,4];
B=[3 ,7];
3 A./B
✌
✆
✞
ans =
0.66667 0.57143
✌
✆
1.2.14 Addition of Elements (plus)
plus performs addition of all arguments. It is equal to the operation x + y + z
+ . . .. It gives cumulative sum of all elements.
✞
plus (x, y, z, ....)
✌
✆
For example
✞
1 plus (1, 2, 3, 4)
✌
✆
✞
ans =
10
✌
✆
59. 1.2. ARITHMETIC OPERATORS 29
https://sites.google.com/view/arunumrao
1.2.15 Power (power)
power performs the power of second argument over the first argument in manner
of element-by-element wise. This function is similar to xy
.
✞
power(x, y)
✌
✆
For example
✞
1 power([1, 2], [3, 4])
✌
✆
✞
ans =
1 16
✌
✆
1.2.16 Unary Subtraction (uminus)
Returns unary negative of supplied number as function’s argument. It is gener-
ally equals to −1×x, where x is any integer supplied to function as its argument.
✞
uminus(x)
✌
✆
For example
✞
1 uminus (1)
✌
✆
✞
ans =
-1
✌
✆
1.2.17 Unary Addition (uplus)
Returns unary plus of supplied number. It is generally equals to +1 × x, where
x is any integer supplied to the function as its argument.
✞
uplus(x)
✌
✆
For example
✞
1 uplus(-1)
✌
✆
✞
ans =
-1
✌
✆
60. 30 Mathematics
1.3 Comparison Operator
In following table, comparison operators are given :
Operation Explanation
x y True if x is less than y.
x ≤ y True if x is less than or equal to y.
x == y True if x is equal to y.
x ≥ y True if x is greater than or equal to y.
x y True if x is greater than y.
x 6= y Equals to x ∼= y and true if x is not equal to y.
For above operations, following functions can also be used for the similar
purposes.
1.3.1 Equals (eq)
Returns true if two input arguments or elements of vector arguments are equal.
Two variables are equal if and only if (iff) x == y. If first argument is equal
to second argument or corresponding elements are equal then, it returns ‘true’
otherwise ‘false’ value.
✞
eq(x, y)
✌
✆
For example
✞
1 eq(2, 3) #Returns false
eq(3, 3) #Returns true
✌
✆
✞
ans = 0
ans = 1
✌
✆
In case of vector argument, conditions are checked for corresponding elements.
See the example below
✞
eq([2,4], [2 ,7])
✌
✆
✞
ans =
1 0
✌
✆
Here, both vectors should be of equal size.
61. 1.3. COMPARISON OPERATOR 31
https://sites.google.com/view/arunumrao
1.3.2 Greater Than or Equal (ge)
It is acronym of ‘greater than or equals to’. It returns true if first input is
greater than or equals to second input otherwise ‘false’ value. If variable x is
greater than or equals to y then it is mathematically represented by x ≥ y. The
inputs may be positive or negative real or complex numbers.
✞
ge(x, y)
✌
✆
For real inputs
✞
1 ge(2, 3) #Returns false
ge(3, 3) #Returns true
3 ge(4, 3) #Returns true
✌
✆
✞
ans = 0
ans = 1
ans = 1
✌
✆
For complex arguments, real part of first argument is compared with real part
of second argument. In the meanwhile, imaginary part of first argument should
be qual to or greater than imaginary part of second argument.
✞
1 ge (3+5*i, 2 + 5*i) #Returns true
ge (3+4*i, 2 + 5*i) #Returns false
✌
✆
✞
ans = 1
ans = 0
✌
✆
1.3.3 Greater Than (gt)
It is acronym of ‘greater than’. Returns true if first input is greater than to
second input otherwise returns false. If variable x is greater than to y then it is
mathematically represented by x y.
✞
gt(x, y)
✌
✆
For example
✞
1 gt(2, 3) #Returns false
gt(3, 3) #Returns false
3 gt(4, 3) #Returns true
✌
✆
62. 32 Mathematics
✞
ans = 0
ans = 0
ans = 1
✌
✆
For complex inputs, real part of first argument is compared with real part of
second argument. In the meanwhile, imaginary part of first argument should
be qual to or greater than imaginary part of second argument.
✞
1 gt (3+5*i, 2 + 5*i) #Returns true
gt (3+4*i, 2 + 5*i) #Returns false
✌
✆
✞
ans = 1
ans = 0
✌
✆
1.3.4 Is Arguments are Equal (isequal)
Returns true if all inputs are equal. Inputs may be real or complex.
✞
isequal (x, y, z, ...)
✌
✆
For example
✞
1 isequal (2, 3, 4)
isequal (3, 3, 3)
✌
✆
✞
ans = 0
ans = 1
✌
✆
1.3.5 Less Than or Equal (le)
It is acronym of ‘less than or equal’. Returns ‘true’ if first argument is less than
or equal to argument input. Mathematically, it is represented as x ≤ y.
✞
le(x, y)
✌
✆
For example
✞
1 le(2, 3)
le(4, 3)
✌
✆
✞
ans = 1
ans = 0
✌
✆
For complex inputs
63. 1.3. COMPARISON OPERATOR 33
https://sites.google.com/view/arunumrao
✞
le (3+5*i, 2 + 5*i) #Returns true
2 le (3+4*i, 2 + 5*i) #Returns false
✌
✆
✞
ans = 0
ans = 1
✌
✆
1.3.6 Less Than (lt)
It is acronym of ‘less than’. Returns ‘true’ if first argument is less than to
argument input. Mathematically, it is represented as x y.
✞
lt(x, y)
✌
✆
For example
✞
1 lt(2, 3)
lt(3, 3)
✌
✆
✞
ans = 1
ans = 0
✌
✆
1.3.7 Not Equals (ne)
It is acronym of ‘not equal’. Returns ‘true’ if first argument is not equal to
second argument. Mathematically, it is represented as x 6= y.
✞
ne(x, y)
✌
✆
For example
✞
1 ne(2, 3)
ne(3, 2)
✌
✆
✞
ans = 1
ans = 1
✌
✆
64. 34 Mathematics
Increment Explanation
+ + x Increase the variable x by one and use new value.
− − x Decrease the variable x by one and use new value.
x + + Increase the variable x by one and use old value.
x − − Decrease the variable x by one and use old value.
Table 1.1: Increments decrements operators for variables.
1.4 Evaluation
The function eval is used to evaluate an expression. The syntax for this function
is
✞
eval (try body , catch body )
✌
✆
An example is
✞
1 eval (
ne(3, 2), //try body
3 sin (90) // catch body
)
✌
✆
✞
ans = 0.89400
✌
✆
1.5 Arithmetic
Mathematical operations those are used in purely arithmetic.
1.5.1 Absolute Value (abs)
It returns the positive value of any positive or negative real number.
✞
1 abs (0.112)
✌
✆
✞
ans = 0.112
✌
✆
✞
1 abs ( -0.112)
✌
✆
✞
ans = 0.112
✌
✆
65. 1.5. ARITHMETIC 35
https://sites.google.com/view/arunumrao
The decimal places of a real decimal number are controlled by format command.
Default decimal places in Octave are ‘5’.
1.5.2 Ceiling (ceil)
It returns the greatest integer that is not less than supplied value. Symbolic
representation of ceil of a number x is ⌈x⌉. If supplied value is a complex number
then computation used in this function is
✞
1 ceil (real (x)) + ceil (imag (x)) * i
✌
✆
An example of this function is
✞
1 ceil ([-1.1, 2.22])
✌
✆
✞
ans =
-1 3
✌
✆
ceil function returns the right side integer to the given real number based on
number-line. In the following figure, ceiling of −2.25 is circled by cyan colour
circle. Here, ceiling value of −2.25 is −2.
0 1 2 3 4
0
−1
−2
−3
−4
b
ceil(-2.25)
1.5.3 Truncate Fraction (fix)
It truncates the fractional portion of a number. It is equivalent to rounding
towards zero. If supplied value is a complex then computation used is
✞
fix (real (x)) + fix (imag (x)) * i
✌
✆
Here an example is
✞
1 fix([1, 2.22])
✌
✆
✞
ans =
1 2
✌
✆
66. 36 Mathematics
1.5.4 floor
This function returns the maximum value but not greater than supplied value.
Symbolic representation of floor of a number x is ⌊x⌋. It is equivalent to round-
ing towards negative infinity. If supplied value is a complex number then com-
putation in result is
✞
floor (real (x)) + floor (imag (x)) * i
✌
✆
Example is
✞
1 floor([1, -2.22])
✌
✆
✞
ans =
1 -2
✌
✆
This function is similar to the Greatest Integer Function (gif). floor function
returns the left side integer of the given real number based on number-line. In
the following figure, floor of −2.25 is circled by cyan colour circle. Here, floor
value of −2.25 is −3. Floor value of 2.5 is 2.
0 1 2 3 4
0
−1
−2
−3
−4
b
gi
b
floor(2.5)
1.5.5 Round Value (round)
This function returns the nearest to the supplied value. If supplied value is a
complex number then computation in result is
✞
round (real (x)) + round (imag (x)) * i
✌
✆
Example is given below.
✞
1 round ([-2.7, 2.7])
✌
✆
✞
ans =
-3 3
✌
✆
1.5.6 Product Elements (prod)
It multiply all elements with each other of a vector or a matrix. Arguments to
this function may be a vector or matrix. Mathematically
P =
Y
aij
67. 1.5. ARITHMETIC 37
https://sites.google.com/view/arunumrao
✞
prod ([-2, 7, 9])
✌
✆
✞
ans = -126
✌
✆
1.5.7 Sum All Elements (sum)
This function returns the algebraic summation of all elements of a vector or a
matrix or a cell. Mathematically
S =
X
aij
✞
1 sum([-2, 7, 9])
✌
✆
✞
ans = 14
✌
✆
1.5.8 Sum of Square of Elements (sumsq)
It returns the sum of square of elements of a vector. If a set A is given by
A = {a1, a2, a3, . . .} then sumsq of this function will be
sumsq(A) = a2
1 + a2
2 + a2
3 + . . .
Example is given below.
✞
1 sumsq([-2, 7, 9])
✌
✆
✞
ans = 134
✌
✆
1.5.9 Maximum Value (max)
It returns the element having maximum value among all the elements in a vector.
✞
1 max([-2, 7, 9])
✌
✆
✞
ans = 9
✌
✆
68. 38 Mathematics
1.5.10 Minimum Value (min)
It returns the element which is minimum among all the elements in a vector.
Its argument is a vector or scalar quantity.
✞
1 min([-2, 7, 9])
✌
✆
✞
ans = -2
✌
✆
1.5.11 Cumulative Maximum (cummax)
In cummax, first element of a vector is placed at its place and it is compared
with next element of the vector. The maximum value of these two element is
placed at next place. This maximum value is again compare with next element
of the vector unitl all elements are not compared.
✞
1 cummax ([1, 2, 3, 4, 2, 5, 1, 8, 36])
✌
✆
✞
ans =
1 2 3 4 4 5 5 8 36
✌
✆
1.5.12 Cumulative Minimum (cummin)
In cummin, first element of a vector is placed at its place and it is compared
with next element of the vector. The minimum value of these two element is
placed at next place. This minimum value is again compare with next element
of the vector until all elements are not compared.
✞
cummin ([4,2,3,4,2,5,1,8,36])
✌
✆
✞
ans =
4 2 2 2 2 2 1 1 1
✌
✆
1.5.13 Cumulative Product (cumprod)
In cumprod, first element of a vector is placed at its place and it is multiplied
with next element of the vector. The product of these two element is placed
at next place. This product value is again multiplied with next element of the
vector until all elements are not multiplied. Mathematically
Y
Pn =
Y
Pn−1 × Pn
Where
Y
Pn−1 = 1 if n = 1.
69. 1.5. ARITHMETIC 39
https://sites.google.com/view/arunumrao
✞
cumprod ([4,2,3,4,2,5,1,8,36])
✌
✆
✞
ans =
4 8 24 96 192 960 960 7680 276480
✌
✆
1.5.14 Cumulative Summation (cumsum)
In cumsum, first element of a vector is placed at its place and it is added with
next element of the vector. The sum value of these two element is placed at
next place. This sum value is again added with next element of the vector until
all elements are not added. Mathematically
X
Pn =
X
Pn−1 × Pn
Where
X
Pn−1 = 0 if n = 1.
✞
cumsum ([4,2,3,4,2,5,1,8,36])
✌
✆
✞
ans =
4 6 9 13 15 20 21 29 65
✌
✆
1.5.15 Output Precisions (output precision)
It tells that how many decimal place precision is being used in fraction numbers.
Use this function like
✞
output_precision
✌
✆
or
✞
1 output_precision ()
✌
✆
1.5.16 Is Element Float Value (isfloat)
It checks whether elements of supplied vector or matrix are floating point values
or not. It returns ‘true’ if a number is float number.
1.5.17 Is Not A Number (isnan)
It checks whether elements of supplied vector or matrix are valid numbers or
not. It returns ‘true’ if a number is valid number.
70. 40 Mathematics
1.5.18 Is Null Value (isnull)
It checks whether elements of supplied vector or matrix are null values or not.
It returns ‘true’ if an element is a null.
1.5.19 Is Numeric Value (isnumeric)
It checks whether elements of supplied vector or matrix are numeric values or
not. It returns ‘true’ if an element is a valid numeric.
1.5.20 Is Prime Number (isprime)
It checks whether elements of supplied vector or matrix are prime or not. It
returns ‘true’ if an element is a prime.
1.5.21 Is Real Number (isreal)
It checks whether elements of supplied vector or matrix are real numbers or not.
It returns ‘true’ if an element is a real number.
1.5.22 Is Empty (isempty)
Returns true if argument is empty.
✞
1 isempty () # empty string
isempty ({}) # Here empty is an element of matrix.
3 isempty ([]) # Here vector is empty.
✌
✆
✞
ans = 1
ans = 0
ans = 1
✌
✆
1.5.23 Are Elements Equal (isequal)
If two values are equals then it returns ‘true’ value otherwise it returns ‘false’
value. If arguments are vector or matrix then whose vector or matrix is com-
pared rather than respective elements of the vector and matrix.
✞
1 isequal (a, b)
isequal (a, a)
3 isempty (1,1)
✌
✆
71. 1.5. ARITHMETIC 41
https://sites.google.com/view/arunumrao
✞
ans = 0
ans = 1
ans = 1
✌
✆
1.5.24 Is Argument A Matrix (ismatrix)
It returns ‘true’ if argument is a matrix otherwise it returns ‘false’.
✞
1 ismatrix ([1, 2, 3, 4]) # matrix of one row four
columns
ismatrix ([1, 2; 3, 4]) # matrix of two rows two
columns
✌
✆
✞
ans = 1
ans = 1
✌
✆
1.5.25 Is Element Infity (isinf)
It returns ‘true’ if an element of a vector or a matrix is an infinite value.
✞
isinf([1, Inf , NA , NaN])
✌
✆
✞
ans = 0 1 0 0
✌
✆
1.5.26 Is Vector Not A Number (isnan)
It returns ‘true’ if an element of a vector or a matrix is not a number (NaN).
✞
1 isnan([1, Inf , NA , NaN])
✌
✆
✞
ans = 0 0 0 1
✌
✆
1.5.27 Binary Power (pow2)
It returns 2n
. This function only accepts the exponents only. It may be a scalar
quantity or a vector.
✞
1 pow2 ([2 ,10])
✌
✆
✞
ans =
4 1024
✌
✆
72. 42 Mathematics
1.5.28 Get Real Part of Complex Number (real)
It returns real part of a complex number. If z = a + bi is a complex number
then real(z) shall return real part of the complex number i.e. value a.
✞
real (2+1 i)
✌
✆
✞
ans =
2
✌
✆
Note that in complex number, the symbol of imaginary part, i.e. ‘i’ here always
appended after numeric value without spaces. Complex number may be in scalar
or in vector form.
✞
real ([2+1i, 4+3i])
✌
✆
✞
ans =
2 4
✌
✆
1.5.29 Real Logarithm of Element (reallog)
This function returns the real-valued natural logarithm of each element of x. It
reports an error if any element results in a complex return value.
✞
reallog (1)
✌
✆
✞
ans = 0
✌
✆
1.5.30 Real Maximum Value (realmax)
It returns maximum real floating number in scalar or vector form of the supplied
argument. The maximum float value in Octave is 1.7977 × 10308
.
✞
1 realmax (1)
✌
✆
✞
ans =
1.7977e+308
✌
✆
73. 1.5. ARITHMETIC 43
https://sites.google.com/view/arunumrao
1.5.31 Real Minimum Value (realmin)
It returns the minimum floating number in scalar or vector form of the supplied
argument. The minimum float value in Octave is 1.7977 × 10−308
.
✞
realmin (1)
✌
✆
✞
ans =
1.7977e-308
✌
✆
1.5.32 Real Power (realpow)
It returns the real value of exponent over the base. For decimal number ‘m’ and
power ‘n’, realpow(m, n) returns the value mn
.
✞
realpow (10 ,2)
✌
✆
✞
ans =
100
✌
✆
1.5.33 Real Square Root Value (realsqrt)
This function computes the square root of a number and returns only the real
square root of the supplied number. If a value has complex or imaginary roots
then they are skipped by this function. The argument may be in vector or scalar
form.
✞
realsqrt (10)
✌
✆
✞
ans =
3.1623
✌
✆
1.5.34 Square Root (sqrt)
If x is a real number then its square root is given by y =
√
x. In other words, if y
is square root of x, then y2
= x. This function performs element wise operation.
This function returns square root of elements of a vector or matrix. Roots
computed by this function may be real or complex. Square root of negative
numbers returned in complex form. If argument is a vector then element wise
square root is computed.
74. 44 Mathematics
✞
sqrt (2)
✌
✆
✞
ans =
1.414
✌
✆
If argument is a vector, then square root of the vector is a vector of square root
of its elements. If argument is a matrix then square root of the matrix is a
matrix of square roots of its elements.
✞
a=[1 ,2;3 ,4];
2 sqrt (a)
✌
✆
✞
ans =
1. 1.4142136
1.7320508 2.
✌
✆
The algorithm used in computation of the square root of a real value, x, is
√
x = e
ln(x)
2
1.5.35 Nth
Root (nthroot)
It return the nth
root of a given number. If yn
= x is a n degree equation for
y, then y = n
√
x is called nth
root for y. Roots, hereby, computed may be real
or complex.
✞
1 nthroot (x,n)
nthroot (5,4)
✌
✆
✞
ans =
1.4953
✌
✆
If, there is a negative number or elements (x) in base, then power (n) must be an
odd value. nth
root of a number can be computed by using algebraic binomial
expansion series. For example, nth
root of x can be obtained by expanding
following reconstruct binomially.
x
1
n = [(1 + x) − 1]
1
n
1.5.36 Infinity Variable (Inf)
‘Inf’ represents to the infinity value. A variable x having assigned value ∞ is
called infinity variable.
75. 1.6. ALGEBRA 45
https://sites.google.com/view/arunumrao
1.5.37 Not A Number Variable (NaN)
‘NaN’ represents to the “Not a Number” value. Octave returns NaN when we
try to get the square root of negative number.
1.6 Algebra
An algebraic relation has terms of known and unknown values. In this section,
functions, useful for algebraic relations are discussed.
1.6.1 Polynomial (poly)
poly function generates coefficient of a polynomial whose roots are supplied to
the function as a vector. If a and b are roots of a polynomial then poly function
✞
poly ([’a’, ’b’])
✌
✆
returns coefficients of the polynomial ax2
− (a + b)x + ab = 0. For example
✞
1 poly ([1, 1])
✌
✆
✞
ans =
1 -2 1
✌
✆
The polynomial equation of these coefficient is generated by using relation
n
X
i=1
Pi × xi−1
Where n is length of coefficients of a polynomial and pi is the coefficient at index
i. Index of coefficient is measured from right to left.
1.6.2 Roots of Polynomial (roots)
A polynomial series in Octave is given by
P = c1xn−1
+ c2xn−2
+ c3xn−3
+ . . . + cn−1x + cn
In this polynomial, there are ‘n’ polynomial terms. In vector form, a polynomial
is written as
P = [c1, c2, c3, . . . , cn−1, cn]
Roots of this polynomial is
✞
roots(P)
✌
✆
76. 46 Mathematics
The roots of polynomial equation p = x2
+ x + 1 is
✞
1 p=[1 ,1 ,1];
roots(p)
✌
✆
✞
ans =
-0.5+0.86 i
-0.5 -0.86i
✌
✆
1.6.3 Convolution (conv)
Let two polynomials are
P = x2
+ x + 1
Q = x2
+ 2 ∗ x + 2
The vector equivalent of these two polynomial
P = [1, 1, 1]
Q = [1, 2, 2]
The convolution of two polynomials P and Q is P ∗ Q and result is
Z = x4
+ 3 ∗ x3
+ 5 ∗ x2
+ 4 ∗ x + 2
and vector equivalent of this polynomial is
Z = [1, 3, 5, 4, 2]
✞
1 p=[1 ,1 ,1];
q=[1 ,2 ,2];
3 conv (p,q)
✌
✆
✞
ans =
1 3 5 4 2
✌
✆
The number of terms in convolution are one less to the sum of length of terms
in the vectors. The modified convolution syntax is
✞
conv (p,q,shape )
✌
✆
The ‘shape’ options are “full” and “same”.
77. 1.6. ALGEBRA 47
https://sites.google.com/view/arunumrao
1.6.4 Residue Of a Polynomial Fraction (residue)
Let a polynomial fraction is
F =
M
X
m=1
rm
(s − pm)e
m
+
N
X
i=1
kisN−i
The residue of this fraction is obtained by using function
✞
1 [r, p, k, e] = residue(num vect , denom vect )
✌
✆
Here ‘num vect’ is the coefficient vector of numerator polynomial and ‘denom
vect’ is coefficient vector of denominator polynomial. The return value is a
vector consisting residue value, pole, direct polynomial and pole multiplicity
respectively. For example assume a fraction
F =
s2
+ s + 1
s3 − 5s2 + 8s − 4
The, numerator and denominator polynomial vectors are
a = [1, 1, 1]
b = [1, −5, 8, −4]
Now the residue is
✞
1 a = [1, 1, 1];
b = [1, -5, 8, -4];
3 [r, p, k, e] = residue(a,b)
✌
✆
✞
r =
-2 7 3
p =
2 2 1
k =
[](0 x0)
e =
1 2 1
✌
✆
The residue of the function is written in format of
R =
r
(s − p)e
k
for all corresponding outputs. Hence
s2
+ s + 1
s3 − 5s2 + 8s − 4
=
−2
(s − 2)1
+
7
(s − 2)2
+
3
(s − 1)1
78. 48 Mathematics
1.6.5 Area of Polygon (polyarea)
It determines area of a polygon by using triangle method. The vertex coor-
dinates are supplied in cyclic form. The pair of consecutive three coordinate
points form a triangle. First side lengths of triangle are measured by using
relation
a =
96. Now, the half perimeter is computed using relation
s =
a + b + c
2
Now, area of triangle is computed as
A =
p
s(s − a)(s − b)(s − c)
In octave, the vertices are represented by abscissa and ordinate vectors, say x
and y respectively. The variables x and y define the vertex pairs. Both, x and
y must be in same shape. The vertex pairs may be a vector or matrix array.
If they are arrays then the columns of x and y are treated separately and area
returned for each x, y pairs. The syntax for polyarea is
✞
A = polyarea (x,y)
✌
✆
Area of a rectangle of vertex (1, 0), (4, 0), (4, 3) and (1, 3) is given as
✞
1 x = [1,4,4,1];
y = [0,0,3,3];
3 a = polyarea (x,y)
✌
✆
✞
a = 9
✌
✆
1.6.6 Polynomial Derivatives (polyder)
It is acronym for “polynomial derivatives”. A polynomial is written as
y =
i=n
X
i=0
aixn−i
97. 1.6. ALGEBRA 49
https://sites.google.com/view/arunumrao
If a terms of degree k is missing then it is written as 0 × xk
. Let y = x2
+ 2 is
a polynomial of degree 2, then in Octave, it is written as
y = x2
+ 0 × x + 2
and its Octave equivalent is written as a vector of coefficients of the polynomial,
like
y = [1, 0, 2]
Note that in polynomial vector, first element is coefficient of highest degree of
the polynomial and next elements are the coefficients of decreasing degree terms.
When polynomial is derivated as dy/dx, then it becomes
y′
= 2x = 2x + 0
Its Octave equivalent representation is
y′
= [2, 0]
The syntax for polyder is
✞
1 polyder (p)
✌
✆
It returns the coefficients of the derivative of the polynomial whose coefficients
are given by the vector ‘p’. Assume a polynomial p = x + 1 whose vector
equivalent is p = [1, 1] and its derivative is
✞
1 p=[1 ,1];
[h]= polyder (p)
✌
✆
✞
h =
1
✌
✆
Here ‘h’ is handle for result of polyder. It is a vector. The equivalent polynomial
is h = 1. The syntax for derivative for product of two polynomials is given as
✞
[k]= polyder (p,q)
✌
✆
Assume two polynomial p = x + 1 and q = x whose vector equivalents are
p = [1, 1] and q = [1, 0]. Product of p and q is p ∗ q. Now the derivative of this
product is
✞
1 p=[1 ,1];
q=[1 ,0];
3 [k]= polyder (p,q)
y=polyout (k)
✌
✆
98. 50 Mathematics
✞
k =
2 1
y =
2*x^1 + 1
✌
✆
polyder can be used in polynomial quotient. For use of polyder in polynomial
quotients, output arguments must be in two elements vector form. The syntax
is
✞
[n,d]= polyder (p,q)
✌
✆
Here output is in polynomial quotient (n/d) form. Here ‘n’ and ‘d’ are the
coefficients of polynomials in numerator and denominator respectively. Assume
two polynomial p = x and q = x + 1 whose vector equivalents are p = [1, 0] and
q = [1, 1] respectively. These two polynomials form a polynomial quotient like
p/q. The derivative of this polynomial quotient is
✞
1 p=[1 ,0];
q=[1 ,1];
3 [n,d]= polyder (p,q)
✌
✆
✞
n =
1
d =
1 2 1
✌
✆
The polynomial quotient is equivalent to polynomial fraction like
1
1 ∗ x2 + 2 ∗ x1 + 1
1.6.7 Greatest Common Divisor of Polynomials (polygcd)
polygcd returns greatest common divisor (gcd) of two polynomials. The two
polynomials are supplied to the function in vector form. Let two polynomials
are
P = x2
+ x + 1
Q = x2
+ 2 ∗ x + 2
The vector equivalent of matrix are
P = [1, 1, 1]
Q = [1, 2, 2]
The greatest common divisor of two polynomials P and Q is constant 1.
99. 1.6. ALGEBRA 51
https://sites.google.com/view/arunumrao
✞
polygcd(p,q)
✌
✆
For example
✞
1 p=[1 ,1 ,1];
q=[1 ,2 ,2];
3 polygcd(p,q)
✌
✆
✞
ans =
1
✌
✆
✞
p=[1,-1,1,-1];
2 q=[1,-2,1];
polygcd(p,q)
✌
✆
✞
ans =
1 -1
✌
✆
The polynomial equivalent of this vector output is x − 1. Hence gcd of these
two polynomials is x − 1.
1.6.8 Polynomial Integral (polyint)
It is acronym for polynomial integral. The syntax for polyint is
✞
p=[1,-1,1,-1];
2 polyint (p, k)
✌
✆
It returns the coefficients of the integral of the polynomial whose coefficients
are represented by the vector ‘p’. The variable ‘k’ is the constant of integration,
which by default is set to zero. Assume a polynomial p = x + 1 whose vector
equivalent is p = [1, 1] and its integral is
✞
p=[1 ,1];
2 [h]= polyint (p)
y=polyout (h)
✌
✆
✞
h =
0.50000 1.00000 0.00000
y =
0.5* x^2 + 1*x^1 + 0
✌
✆
Here ‘h’ is handle for result of polyint and it is a vector.
100. 52 Mathematics
1.6.9 Polynomial Output (polyout)
It converts a vector into polynomial form. This function takes two arguments.
First is coefficient vector and second is polynomial variable. If a0, a1, a2, . . .,
an are coefficients of a vector A then polynomial output for the variable x is
y =
i=n
X
i=0
aixn−i
Note that in polynomial vector, first element is coefficient of highest degree of
the polynomial and next elements are the coefficients of decreasing degree terms.
The syntax used with this function is
✞
polyout (vector , poly string )
✌
✆
The default value of ‘poly string’ is “s”. We can change the polynomial string by
supplying second argument in the polyout() function as shown in the following
example.
✞
1 x=[1 ,4 ,5 ,8];
polyout (x,x)
✌
✆
✞
ans =
1*x^3 + 4*x^2 + 5*x^1 + 8
✌
✆
1.6.10 Reduce Polynomial (polyreduce)
Reduce a polynomial coefficient vector to a minimum number of terms by strip-
ping off any leading zeros. For example a polynomial vector [0, 0, 1, 1] is reduced
to [1, 1].
✞
polyreduce ([0 ,0 ,1 ,2])
✌
✆
✞
ans =
1 2
✌
✆
1.6.11 Evaluate Polynomial (polyval)
polyval evaluates a polynomial at a given point. The syntax of this function is
✞
polyval (p,a)
✌
✆
101. 1.6. ALGEBRA 53
https://sites.google.com/view/arunumrao
Where ‘p’ is a polynomial vector and ‘a’ is a variable value. Assume a polynomial
y = x3
+ 2x − 10
whose polynomial vector equivalent is
y = [1, 0, 2, −10]
Evaluation of this polynomial at a point x = 3 is
y = 33
+ 2 × 3 − 10 = 23
The Octave code for this function are
✞
1 y = [1,0,2,-10];
x = 3;
3 f3= polyval (y,x)
✌
✆
✞
f3 = 23
✌
✆
1.6.12 Matrix Polynomial Evaluation (polyvalm)
polyvalm evaluates a polynomial at a given point in matrix sense. The syntax
of this function is
✞
1 polyvalm (a, x)
✌
✆
Where ‘x’ must a square matrix and ‘a’ is a vector value.
✞
1 x = [1 ,0;2 , -10];
y = [1 ,1];
3 z = polyvalm (y,x)
✌
✆
✞
z =
2 0
2 -9
✌
✆
1.6.13 Factorial (factorial)
factorial returns the factorial of a number. Factorial of a number N is repre-
sented by N! and it is equal to
N! = N · (N − 1) · . . . · 3 · 2 · 1
✞
1 factorial ([10 ,5 ,7 ,4])
✌
✆
✞
ans = 3628800 120 5040 24
✌
✆
102. 54 Mathematics
1.6.14 Prime Factor of a Number (factor)
factor returns the prime factors of a given number. Prime factors of a number
does not divides perfectly to each other.
✞
1 factor (15)
factor (30)
✌
✆
✞
ans =
3 5
ans =
2 3 5
✌
✆
The supplied argument may be scalar or a vector.
1.6.15 Greatest Common Divisor of Numbers (gcd)
Compute the greatest common divisor of given arguments. If arguments are
vectors then corresponding element wise gcd is computed. Remember that the
vectors must of same length.
✞
gcd(15, 20)
2 gcd ([10, 30],[15, 60])
✌
✆
✞
ans =
5
ans =
5 30
✌
✆
1.6.16 Least Common Multiple (lcm)
Least common multiple of given numbers is that smallest number which is com-
pletely divided by given numbers. lcm compute the least common multiple of
the give numbers. If argument to this function are vectors, then all vectors must
be of same size. LCM of vector elements is computed element wise.
✞
lcm(15, 20)
2 lcm ([10, 30],[15, 60])
✌
✆
✞
ans =
60
ans =
30 60
✌
✆
103. 1.6. ALGEBRA 55
https://sites.google.com/view/arunumrao
1.6.17 Remainder of Division of Two Element (rem)
When dividend (y) is divided by divisor (x) then it gives quotient (q) and
remainder (r). The relation between them are
y = q × x + r
rem returns the remainder (r) of division of first element by second elements of
the function argument.
✞
rem(15, 20) # like C expression 15%20;
2 rem ([10, 30],[2, 8]) # like C expression 10%2; 30%8;
✌
✆
✞
ans =
15
ans =
0 6
✌
✆
1.6.18 Modulo of Two Number (mod)
mod returns the modulo of two numbers.
✞
mod(10, 4)
✌
✆
✞
ans =
2
✌
✆
1.6.19 Prime Numbers (primes)
primes returns all prime numbers upto the value supplied as argument to the
function. For example, if input argument to this function is decimal 10, then
this function returns all prime numbers falls between 1 to 10. Here, answer shall
be 2, 3, 5, 7. See the example given below:
✞
primes (10)
✌
✆
✞
ans =
2 3 5 7
✌
✆
104. 56 Mathematics
1.6.20 Evaluates Algebraic Equations (fsolve)
fsolve is used to evaluate algebraic equations. The syntax of this function is
✞
[x, fval , info ] = fsolve(function , initial guess )
✌
✆
Here, ‘x’ is solution of equations, ‘fval’ is function values and ‘info’ tells the
convergence or divergence of a function. If ‘info’ is ‘1’ then output is convergent
otherwise divergent. For fsolve, specific method of function generation is used.
For example, two equations are
2x2
+ 3xy − 6 = 0
x2
+ 2xy + 4 = 0
These two equations will be syntax like
✞
1 ## Initiate function with output y and function f
function y=f(x)
3 ## Initiate matrix format of equations . Here
## two equations as rows and one column.
5 y = zeros (2, 1);
## First equation named as y(1) and variable
7 ## ‘x’ as x(1) and ‘y’ as x(2).
y(1) = 2*x(1) ^2 + 3*x(1)*x(2) - 6 ;
9 ## second equation
y(2) = x(1) ^2 + 2*x(1)*x(2) + 4;
11 ## Close the function
endfunction
✌
✆
Now call the function like
✞
[x, fval , info ] = fsolve(@f , [1 ,2])
✌
✆
A working example is
✞
1 function y=f(x)
y = zeros (2, 1);
3 y(1) = 2*x(1) ^2 + 3*x(1)*x(2) - 6 ;
y(2) = x(1) ^2 + 2*x(1)*x(2) + 4;
5 endfunction
[x, fval , info ] = fsolve(@f , [1 ,2])
✌
✆
✞
x =
4.8890 -2.8577
fval =
-1.5923 e-006
-9.2964 e-007
105. 1.6. ALGEBRA 57
https://sites.google.com/view/arunumrao
info =
2
✌
✆
1.6.21 Find Zeros (fzero)
This function finds a zero of a uni-variate function. It returns a point where
function value is zero and function value at that point. Its synopsis is
✞
1 [x, fval , info , output] = fzero (func , x0 ,
options )
✌
✆
Here ‘func’ is a function handle. ‘x0’ is initial value. ‘options’ are used to
extend the scope of the computation. ‘x0’ should be two-element vector or sin-
gle vector so that there is a zero crossing (change in sign of function value) of
function within the given range of ‘x0’. “FunValCheck”, “OutputFcn”, “TolX”,
“MaxIter”, “MaxFunEvals” are used as options. The function returns the ap-
proximate zero point as ‘x’ and ‘fval’ as function value. ‘info’ is an exit flag which
is 1 if the algorithm converged to a solution, 0 if maximum number of iterations
or function evaluations has been reached, -1 if the algorithm has been termi-
nated from user output function and -5 if the algorithm may have converged to
a singular point. ‘output’ is a structure containing runtime information about
the ‘fzero’ algorithm.
✞
1 function y=f(x)
y = 2*x^2 + 3*x - 6 ;
3 endfunction
[x, fval , info ] = fzero(@f ,1)
✌
✆
✞
x = 1.1375
fval = 0
info = 1
✌
✆
1.6.22 Evaluate Function Name (feval)
It evaluate the named primitive function. The function name is either function
name within double quotes or function object. All arguments after the first one
are passed as inputs to the named function. For example,
✞
1 feval(sin ,45)
✌
✆
computes the sin(45◦
) value which is 0.85090.
✞
ans =
0.85090
✌
✆
106. 58 Mathematics
1.7 Complex Numbers
A complex number is made of two parts. First is real part and second is imagi-
nary part. Imaginary part is suffixed with symbol i. The coefficient of imaginary
part is real number. For example, in Octave, a complex number is represented
by z = a + bi. Where a and bi are real and imaginary parts of the complex
number respectively. a and b are real numbers. In numerical form, it is similar
to z = 2 + 3i (say).
1.7.1 Absolute Number (abs)
abs returns the absolute value of a number. All positive or negative numbers
becomes positive one.
✞
abs ([1.25 , 0.022145 , -1, 40])
✌
✆
✞
ans =
1.250000 0.022145 1.000000 40.000000
✌
✆
1.7.2 Argument of Complex Number (arg)
arg returns the argument of a complex number. Argument of a complex number,
z = x + yi is calculated by using the relation
arg = tan−1
y
x
This arg is in radian.
✞
arg (1+2 i)
✌
✆
✞
ans =
1.1071
✌
✆
1.7.3 Argument of Complex Number (angle)
angle returns the argument of a complex number. It is similar to the arg func-
tion. The return value is in radian.
✞
angle(1 + 2i)
✌
✆
✞
ans =
1.1071
✌
✆
107. 1.8. GEOMETRY 59
https://sites.google.com/view/arunumrao
Mathematically, argument of a complex number a + ib is given by
α = tan−1
b
a
1.7.4 Conjugate of Complex Number (conj)
conj converts a complex number into conjugate form. If a + ib is a complex
number then its complex conjugate is given by a − ib.
✞
conj (1 + 2i)
✌
✆
✞
ans =
1 - 2i
✌
✆
1.7.5 Imaginary Part of Complex Number (imag)
imag returns imaginary part of a complex number. If z = a + bi then imag(z)
will return value b.
✞
imag (1 + 2i)
✌
✆
✞
ans =
2
✌
✆
1.7.6 Real Part of Complex Number (real)
real returns real part of a complex number. If z = a+bi then real(z) will return
value a.
✞
real (1+2 i)
✌
✆
✞
ans =
1
✌
✆
1.8 Geometry
In geometry, coordinates play a major role in calculations. Sometimes there is
need of conversion of coordinate system mutually among Cartesian, Polar and
Spherical coordinate system.
108. 60 Mathematics
1.8.1 Cartesian to Polar Conversion (cart2pol)
cart2pol returns the Cartesian coordinates into Polar form. The syntax of this
function is
✞
[theta , rho , z]= cart2pol (x, y, z)
✌
✆
x
y
b
z
r
b
ρ
b
z
x
y
b
ρ
θ
x
y
If all three axes are used, Cartesian coordinates are converted into cylindrical
coordinates. Here ‘theta’ is given by
θ = tan−1
y
x
‘rho’ is radius and it is given by
ρ =
p
x2 + y2
and elevation is z is given by z itself, i.e.
z = z
A Cartesian to polar conversion example for three dimensional relation is
✞
1 [th , rh , z]= cart2pol (1, 2, 3)
✌
✆
✞
th = 1.1071
rh = 2.2361
z = 3
✌
✆
109. 1.8. GEOMETRY 61
https://sites.google.com/view/arunumrao
x
y
b
r
θ
x
y
If there are only two arguments in the function then θ and radius r are given
by respectively
θ = tan−1
y
x
‘r’ is radius and it is given by
r =
p
x2 + y2
✞
1 [theta , rho ]= cart2pol (x, y)
✌
✆
A two dimensional format
✞
1 [th , r]= cart2pol (1,2)
✌
✆
✞
th = 1.1071
r = 2.2361
✌
✆
1.8.2 Polar to Cartesian Conversion (pol2cart)
It is reversal of cart2pol function. It converts polar coordinates into Cartesian
form. The syntax of this function is
✞
[z, y, z]= pol2cart (theta , rho , z)
✌
✆
110. 62 Mathematics
x
y
b
ρ
θ
x
y
x
y
b
z
r
b
ρ
b
z
Relation between Cartesian and polar coordinates are given by
x = ρ cosθ
y = ρ sin θ
and
z = z
Example is
✞
1 [z, y, z]= pol2cart (1.1071 , 2.2361 , 3)
✌
✆
✞
x = 1
y = 2
z = 3
✌
✆
1.8.3 Spherical to Cartesian Conversion (sph2cart)
sph2cart converts spherical coordinates into Cartesian form. It accepts three
parameters. First is azimuth, i.e. angular displacement (azimuth) of projection
of the coordinate vector from x-axis. Second is angular displacement (elevation)
of the coordinate vector from the xy-plain. Third argument is the radius of the
sphere where spherical coordinate exists. Syntax of this function is
✞
1 [x,y,z]= sph2cart (azimuth , elevation , r)
✌
✆
Spherical to Cartesian coordinate conversion takes place according to the follow-
ing relations. First we compute the projection of radius vector of the spherical
coordinates along the z-axis and in the xy-plain. So,
z = r sin ϕ
111. 1.8. GEOMETRY 63
https://sites.google.com/view/arunumrao
and projection of this vector r in xy-plain is
z′
= r cos ϕ
This z′
acts as the projection vector of r in the xy-plane. We can again resolute
this vector along the x and y-axes respectively. Now, x = z′
cos θ and y =
z′
sin θ. So,
x = r cos ϕ cos θ
y = r cos ϕ sin θ
x
z
y
z
z′
ϕ
r
b
x
z
y
z
z′
x
y
θ
r
b
Example is
✞
1 [x, y, z]= sph2cart (1.1071 , 0.93027 , 3.7417)
✌
✆
✞
x = 1
y = 2
z = 3
✌
✆
1.8.4 Cartesian to Spherical Conversion (cart2sph)
cart2sph converts Cartesian coordinates into spherical coordinates. It is inverse
of the sph2cart function. The syntax of this function is
✞
1 [azimuth , elevation , r]= cart2sph (x, y, z)
✌
✆
112. 64 Mathematics
x
z
y
x
y
θ
r
b
x
z
y
ϕ
r
b
Here azimuth (θ), elevation (ϕ) and radius (r) of the spherical coordinate are
computed respectively
az(θ) = tan−1
y
x
el(ϕ) = sin−1 z
p
x2 + y2 + z2
!
and
r =
p
x2 + y2 + z2
Example is
✞
1 [az , el , r]= cart2sph (1, 2, 3)
✌
✆
✞
az = 1.1071
el = 0.93027
rd = 3.7417
✌
✆
1.9 Logarithm
Logarithm and antilogarithm is a method to perform mathematical operations
of long numbers very easily. Mathematical operations of exponential terms (xy
)
is best usability of logarithm. Properties are
Property Description
log(a × b) log a + log b
log
a
b
log a − log b
log(ab
) b log a
loge x = y x = ey
loge e 1
113. 1.9. LOGARITHM 65
https://sites.google.com/view/arunumrao
1.9.1 Natural Logarithm (log)
It is logarithm of any numerical value with natural base. If x is numerical value
then its natural logarithm is given by
y = loge(x)
This natural logarithm has a relation with logarithm having base ‘10’ as
y = 2.303 × log10(x)
✞
1 log (2)
✌
✆
✞
ans = 0.69315
✌
✆
1.9.2 Logarithm Base Ten (log10)
It gives logarithm of any numerical value with respect to base ‘10’. If x is
numerical value then its logarithm is given by
y = log10(x)
✞
1 log10(2)
✌
✆
✞
ans = 0.30103
✌
✆
1.9.3 Unit Increment Logarithm (log1p)
y = log1p(x) is equivalent to y = log(1 + x).
✞
1 log1p(2)
✌
✆
✞
ans = 1.0986
✌
✆
This value can be verified by using log function and supplying it value (1+2) as
its argument.
✞
1 log (1+2)
✌
✆
✞
ans = 1.0986
✌
✆
114. 66 Mathematics
1.9.4 Binary Base Logarithm (log2)
It is logarithm with binary base ‘2’. If x is numerical argument, then its loga-
rithm on base 2 is given by
y = log2(x)
In Octave, binary base logarithm is used as given below:
✞
1 log2 (2)
✌
✆
✞
ans = 1
✌
✆
1.9.5 Exponential Base (e)
ex
returns the element-wise exponential value of an argument. Mathematically,
exponent of x to base ‘e’ is given by, ex
. Its series expansion is
ex
= 1 + x +
x2
2!
+
x3
3!
+ . . .
Similarly, if expression is e−x
then
e−x
= 1 − x +
x2
2!
−
x3
3!
+ . . .
In complex form, exponent is written as
eix
= cos(x) + i sin(x)
This is important relation and it is used to get the trigonometric function in
series expansion form as
cos x =
eix
+ e−ix
2
, sin x =
eix
− e−ix
2i
Natural base with unit exponent lies between ‘2’ and ‘3’ and it is irrational
number.
✞
1 e
✌
✆
✞
ans = 2.7183
✌
✆
115. 1.10. TRIGONOMETRIC FUNCTIONS 67
https://sites.google.com/view/arunumrao
1.10 Trigonometric Functions
1.10.1 Inverse Trigonometric Functions
If y = sin(x) is a trigonometric function then x = sin−1
(y) is called inverse
trigonometric function. Inverse sine function is also written as x = asin(y). The
domain of x for sine function is [−π/2, π/2] and its range is [−1, 1], i.e. domain
of y.
Inverse Sine in Radian (asin)
asin computes inverse sine of the argument. The result is in radian.
✞
1 A=[1 ,2;3 ,4];
asin (A)
✌
✆
✞
ans =
1.5707963 1.5707963 + 1.3169579 i
1.5707963 + 1.7627472 i 1.5707963 + 2.0634371 i
✌
✆
Inverse Sine in Degree (asind)
asin computes inverse sine of the argument. The result is in degree.
✞
1 asind(-1)
✌
✆
✞
ans =
- 90.
✌
✆
Inverse Hyperbolic Sine (asinh)
asinh computes the element-wise inverse hyperbolic sine of the argument. Math-
ematically, it is given as logarithm function as
asinh(x) = ln
x +
p
x2 + 1
The domain of this function is (−1, 1). In terms of series expansion:
asinh(x) =
∞
X
n=0
(−1)n
(2n)!
22n(n!)2
x2n+1
2n + 1
; |x| 1
✞
asinh(-1)
✌
✆
116. 68 Mathematics
✞
ans =
- 0.8813736
✌
✆
Inverse Cosine in Radian (acos)
acos returns inverse cosine value of an argument as a real value. Its result is in
degree. Mathematically, if y = cos θ is written as
θ = cos−1
(y)
Here, θ is in radian.
✞
x=0
2 acos (cos(x))
✌
✆
✞
ans =
0.
✌
✆
Inverse Cosine In Degree (acosd)
acosd returns inverse cosine value of supplied arguments as a real value. Its
result is in degree. The domain of the argument is [−1, 1]. The result of this
function is in degree. Mathematically, if y = cos θ is written as
θ = cos−1
(y)
If θ is in degree.
✞
x=[0 ,1 ,1];
2 acosd(x)
✌
✆
✞
ans =
90. 0. 0.
✌
✆
Inverse Hyperbolic Cosine (acosh)
acosh returns inverse hyperbolic cosine value of supplied arguments. It returns
value in radian. Mathematically, if y = cosh θ is written as
θ = cosh−1
(y)
Here, θ is in radian. Mathematically
acosh(x) = ln
x +
p
x2 − 1