SlideShare une entreprise Scribd logo
1  sur  150
Télécharger pour lire hors ligne
1
GNU OCTAVE
AN INTRODUCTION
Arun Umrao
https://sites.google.com/view/arunumrao
DRAFT COPY - GPL LICENSING
2
Contents
1 Mathematics 3
1.1 Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.1 Index Expression . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.2 Reshape Elements (reshape) . . . . . . . . . . . . . . . . . 7
1.1.3 Is Element an Index (isindex) . . . . . . . . . . . . . . . . 7
1.2 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2.1 Addition & Subtraction . . . . . . . . . . . . . . . . . . . 8
1.2.2 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2.3 Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2.4 Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.5 Unary Operations . . . . . . . . . . . . . . . . . . . . . . 13
1.2.6 Left Division (ldivide) . . . . . . . . . . . . . . . . . . . . 13
1.2.7 Matrix Left Division (mldivide) . . . . . . . . . . . . . . . 13
1.2.8 Subtraction (minus) . . . . . . . . . . . . . . . . . . . . . 14
1.2.9 Matrix Power (mpower) . . . . . . . . . . . . . . . . . . . 14
1.2.10 Matrix Right Division (mrdivide) . . . . . . . . . . . . . . 15
1.2.11 Recursive Product (mtimes) . . . . . . . . . . . . . . . . . 15
1.2.12 Element-wise Recursive Product (times) . . . . . . . . . . 16
1.2.13 Element-wise Right Division (rdivide) . . . . . . . . . . . 16
1.2.14 Addition of Elements (plus) . . . . . . . . . . . . . . . . . 16
1.2.15 Power (power) . . . . . . . . . . . . . . . . . . . . . . . . 17
1.2.16 Unary Subtraction (uminus) . . . . . . . . . . . . . . . . . 17
1.2.17 Unary Addition (uplus) . . . . . . . . . . . . . . . . . . . 17
1.3 Comparison Operator . . . . . . . . . . . . . . . . . . . . . . . . 18
1.3.1 Equals (eq) . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.3.2 Greater Than or Equal (ge) . . . . . . . . . . . . . . . . . 19
1.3.3 Greater Than (gt) . . . . . . . . . . . . . . . . . . . . . . 19
1.3.4 Is Arguments are Equal (isequal) . . . . . . . . . . . . . . 20
1.3.5 Less Than or Equal (le) . . . . . . . . . . . . . . . . . . . 20
1.3.6 Less Than (lt) . . . . . . . . . . . . . . . . . . . . . . . . 21
1.3.7 Not Equals (ne) . . . . . . . . . . . . . . . . . . . . . . . 21
1.4 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.1 Absolute Value (abs) . . . . . . . . . . . . . . . . . . . . . 22
1.5.2 Ceiling (ceil) . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.5.3 Truncate Fraction (fix) . . . . . . . . . . . . . . . . . . . . 23
1.5.4 floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.5.5 Round Value (round) . . . . . . . . . . . . . . . . . . . . 24
1.5.6 Product Elements (prod) . . . . . . . . . . . . . . . . . . 24
1.5.7 Sum All Elements (sum) . . . . . . . . . . . . . . . . . . . 25
3
https://sites.google.com/view/arunumrao
1.5.8 Sum of Square of Elements (sumsq) . . . . . . . . . . . . 25
1.5.9 Maximum Value (max) . . . . . . . . . . . . . . . . . . . 25
1.5.10 Minimum Value (min) . . . . . . . . . . . . . . . . . . . . 26
1.5.11 Cumulative Maximum (cummax) . . . . . . . . . . . . . . 26
1.5.12 Cumulative Minimum (cummin) . . . . . . . . . . . . . . 26
1.5.13 Cumulative Product (cumprod) . . . . . . . . . . . . . . . 26
1.5.14 Cumulative Summation (cumsum) . . . . . . . . . . . . . 27
1.5.15 Output Precisions (output precision) . . . . . . . . . . . . 27
1.5.16 Is Element Float Value (isfloat) . . . . . . . . . . . . . . . 27
1.5.17 Is Not A Number (isnan) . . . . . . . . . . . . . . . . . . 27
1.5.18 Is Null Value (isnull) . . . . . . . . . . . . . . . . . . . . . 28
1.5.19 Is Numeric Value (isnumeric) . . . . . . . . . . . . . . . . 28
1.5.20 Is Prime Number (isprime) . . . . . . . . . . . . . . . . . 28
1.5.21 Is Real Number (isreal) . . . . . . . . . . . . . . . . . . . 28
1.5.22 Is Empty (isempty) . . . . . . . . . . . . . . . . . . . . . . 28
1.5.23 Are Elements Equal (isequal) . . . . . . . . . . . . . . . . 28
1.5.24 Is Argument A Matrix (ismatrix) . . . . . . . . . . . . . . 29
1.5.25 Is Element Infity (isinf) . . . . . . . . . . . . . . . . . . . 29
1.5.26 Is Vector Not A Number (isnan) . . . . . . . . . . . . . . 29
1.5.27 Binary Power (pow2) . . . . . . . . . . . . . . . . . . . . 29
1.5.28 Get Real Part of Complex Number (real) . . . . . . . . . 30
1.5.29 Real Logarithm of Element (reallog) . . . . . . . . . . . . 30
1.5.30 Real Maximum Value (realmax) . . . . . . . . . . . . . . 30
1.5.31 Real Minimum Value (realmin) . . . . . . . . . . . . . . . 31
1.5.32 Real Power (realpow) . . . . . . . . . . . . . . . . . . . . 31
1.5.33 Real Square Root Value (realsqrt) . . . . . . . . . . . . . 31
1.5.34 Square Root (sqrt) . . . . . . . . . . . . . . . . . . . . . . 31
1.5.35 Nth
Root (nthroot) . . . . . . . . . . . . . . . . . . . . . . 32
1.5.36 Infinity Variable (Inf) . . . . . . . . . . . . . . . . . . . . 32
1.5.37 Not A Number Variable (NaN) . . . . . . . . . . . . . . . 33
1.6 Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.6.1 Polynomial (poly) . . . . . . . . . . . . . . . . . . . . . . 33
1.6.2 Roots of Polynomial (roots) . . . . . . . . . . . . . . . . . 33
1.6.3 Convolution (conv) . . . . . . . . . . . . . . . . . . . . . . 34
1.6.4 Residue Of a Polynomial Fraction (residue) . . . . . . . . 35
1.6.5 Area of Polygon (polyarea) . . . . . . . . . . . . . . . . . 36
1.6.6 Polynomial Derivatives (polyder) . . . . . . . . . . . . . . 36
1.6.7 Greatest Common Divisor of Polynomials (polygcd) . . . 38
1.6.8 Polynomial Integral (polyint) . . . . . . . . . . . . . . . . 39
1.6.9 Polynomial Output (polyout) . . . . . . . . . . . . . . . . 40
1.6.10 Reduce Polynomial (polyreduce) . . . . . . . . . . . . . . 40
1.6.11 Evaluate Polynomial (polyval) . . . . . . . . . . . . . . . 40
1.6.12 Matrix Polynomial Evaluation (polyvalm) . . . . . . . . . 41
1.6.13 Factorial (factorial) . . . . . . . . . . . . . . . . . . . . . . 41
1.6.14 Prime Factor of a Number (factor) . . . . . . . . . . . . . 42
4
1.6.15 Greatest Common Divisor of Numbers (gcd) . . . . . . . 42
1.6.16 Least Common Multiple (lcm) . . . . . . . . . . . . . . . 42
1.6.17 Remainder of Division of Two Element (rem) . . . . . . . 43
1.6.18 Modulo of Two Number (mod) . . . . . . . . . . . . . . . 43
1.6.19 Prime Numbers (primes) . . . . . . . . . . . . . . . . . . 43
1.6.20 Evaluates Algebraic Equations (fsolve) . . . . . . . . . . . 44
1.6.21 Find Zeros (fzero) . . . . . . . . . . . . . . . . . . . . . . 45
1.6.22 Evaluate Function Name (feval) . . . . . . . . . . . . . . . 45
1.7 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . 46
1.7.1 Absolute Number (abs) . . . . . . . . . . . . . . . . . . . 46
1.7.2 Argument of Complex Number (arg) . . . . . . . . . . . . 46
1.7.3 Argument of Complex Number (angle) . . . . . . . . . . . 46
1.7.4 Conjugate of Complex Number (conj) . . . . . . . . . . . 47
1.7.5 Imaginary Part of Complex Number (imag) . . . . . . . . 47
1.7.6 Real Part of Complex Number (real) . . . . . . . . . . . . 47
1.8 Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
1.8.1 Cartesian to Polar Conversion (cart2pol) . . . . . . . . . 48
1.8.2 Polar to Cartesian Conversion (pol2cart) . . . . . . . . . 49
1.8.3 Spherical to Cartesian Conversion (sph2cart) . . . . . . . 50
1.8.4 Cartesian to Spherical Conversion (cart2sph) . . . . . . . 51
1.9 Logarithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
1.9.1 Natural Logarithm (log) . . . . . . . . . . . . . . . . . . . 53
1.9.2 Logarithm Base Ten (log10) . . . . . . . . . . . . . . . . . 53
1.9.3 Unit Increment Logarithm (log1p) . . . . . . . . . . . . . 53
1.9.4 Binary Base Logarithm (log2) . . . . . . . . . . . . . . . . 54
1.9.5 Exponential Base (e) . . . . . . . . . . . . . . . . . . . . . 54
1.10 Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . 55
1.10.1 Inverse Trigonometric Functions . . . . . . . . . . . . . . 55
Inverse Sine in Radian (asin) . . . . . . . . . . . . . . . . 55
Inverse Sine in Degree (asind) . . . . . . . . . . . . . . . . 55
Inverse Hyperbolic Sine (asinh) . . . . . . . . . . . . . . . 55
Inverse Cosine in Radian (acos) . . . . . . . . . . . . . . . 56
Inverse Cosine In Degree (acosd) . . . . . . . . . . . . . . 56
Inverse Hyperbolic Cosine (acosh) . . . . . . . . . . . . . 56
Inverse Tangent in Radian (atan) . . . . . . . . . . . . . . 57
Inverse Tangent in Degree (atand) . . . . . . . . . . . . . 57
Inverse Hyperbolic Tangent (atanh) . . . . . . . . . . . . 57
Inverse Cotangent in Radian(acot) . . . . . . . . . . . . . 58
Inverse Cotangent in Degree (acotd) . . . . . . . . . . . . 58
Inverse Hyperbolic Cotangent (acoth) . . . . . . . . . . . 58
Inverse Secant in Radian (asec) . . . . . . . . . . . . . . . 59
Inverse Secant in Degree (asecd) . . . . . . . . . . . . . . 59
Inverse Hyperbolic Secant (asech) . . . . . . . . . . . . . 60
Inverse Cosecant in Radian (acsc) . . . . . . . . . . . . . 60
Inverse Cosecant in Degree (acscd) . . . . . . . . . . . . . 61
5
https://sites.google.com/view/arunumrao
Inverse Hyperbolic Cosecant (acsch) . . . . . . . . . . . . 61
1.10.2 Trigonometric Functions . . . . . . . . . . . . . . . . . . . 61
Sine - Argument in Radian (sin) . . . . . . . . . . . . . . 61
Cardinal Sine (sinc) . . . . . . . . . . . . . . . . . . . . . 62
Sine - Argument in Degree (sind) . . . . . . . . . . . . . . 62
Hyperbolic Sine (sinh) . . . . . . . . . . . . . . . . . . . . 63
Cosine - Argument In Radian (cos) . . . . . . . . . . . . . 63
Cosine - Argument In Degree (cosd) . . . . . . . . . . . . 63
Hyperbolic Cosine (cosh) . . . . . . . . . . . . . . . . . . 63
Tangent - Argument in Radian (tan) . . . . . . . . . . . . 64
Tangent - Argument in Degree (tand) . . . . . . . . . . . 64
Hyperbolic Tangent (tanh) . . . . . . . . . . . . . . . . . 64
Slope Angle (atan2) . . . . . . . . . . . . . . . . . . . . . 65
Cotangent - Argument in Degree (cotd) . . . . . . . . . . 65
Hyperbolic Cotangent (coth) . . . . . . . . . . . . . . . . 65
Secant - Argument in Radian (sec) . . . . . . . . . . . . . 65
Secant - Argument in Degree (secd) . . . . . . . . . . . . 66
Hyperbolic Secant (sech) . . . . . . . . . . . . . . . . . . 66
Cosecant - Input in Radian (csc) . . . . . . . . . . . . . . 66
Cosecant - Input in Degree (cscd) . . . . . . . . . . . . . 66
Hyperbolic Cosecant (csch) . . . . . . . . . . . . . . . . . 67
1.11 Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
1.11.1 Transpose of Matrix (transpose) . . . . . . . . . . . . . . 72
1.11.2 Complex Conjugate Transpose of Matrix (ctranspose) . . 73
1.11.3 Dot Product (dot) . . . . . . . . . . . . . . . . . . . . . . 74
1.11.4 Cross Product (cross) . . . . . . . . . . . . . . . . . . . . 74
1.11.5 Determinant (det) . . . . . . . . . . . . . . . . . . . . . . 75
1.11.6 Identity Matrix (eye) . . . . . . . . . . . . . . . . . . . . . 76
1.11.7 Eigenvalues (eig) . . . . . . . . . . . . . . . . . . . . . . . 76
1.11.8 Eigens (eigs) . . . . . . . . . . . . . . . . . . . . . . . . . 77
1.11.9 Inverse of Matrix (inv) . . . . . . . . . . . . . . . . . . . . 79
1.11.10Linear Equation Solver (linsolve) . . . . . . . . . . . . . . 80
1.11.11Type of Matrix (matrix type) . . . . . . . . . . . . . . . . 82
1.11.12Normalized Matrix (norm) . . . . . . . . . . . . . . . . . 83
1.11.13Null Space Matrix (null) . . . . . . . . . . . . . . . . . . . 84
1.11.14Orthogonal Basis (orth) . . . . . . . . . . . . . . . . . . . 85
1.11.15Rank of Matrix (rank) . . . . . . . . . . . . . . . . . . . . 85
1.11.16Trace of Matrix (trace) . . . . . . . . . . . . . . . . . . . 85
1.11.17Cholesky Matrix (chol) . . . . . . . . . . . . . . . . . . . 86
1.11.18Inverse Cholesky Matrix (cholinv) . . . . . . . . . . . . . 87
1.11.19Matrix Exponential (expm) . . . . . . . . . . . . . . . . . 87
1.11.20Logarithmic Matrix (logm) . . . . . . . . . . . . . . . . . 88
1.11.21Square Root of Matrix (sqrtm) . . . . . . . . . . . . . . . 89
1.11.22Kronecker Product (kron) . . . . . . . . . . . . . . . . . . 90
1.11.23Diagonal Matrix (diag) . . . . . . . . . . . . . . . . . . . 90
6
1.11.24Single Value Decomposition (svd) . . . . . . . . . . . . . . 92
1.11.25Lower Upper Decomposition (lu) . . . . . . . . . . . . . . 95
1.11.26Lower Upper Composition (qr) . . . . . . . . . . . . . . . 96
1.11.27Length of Matrix (length) . . . . . . . . . . . . . . . . . . 96
1.12 Special Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
1.12.1 Bessel Function of First Kind (besselj) . . . . . . . . . . . 97
1.12.2 Bessel Function of Second Kind (bessely) . . . . . . . . . 97
1.12.3 Hyperbolic Bessel Function of First Kind (besseli) . . . . 98
1.12.4 Hyperbolic Bessel Function of Second Kind (besselk) . . . 98
1.12.5 Bessel Function as Hankel Function (besselh) . . . . . . . 98
1.12.6 Beta Function (beta) . . . . . . . . . . . . . . . . . . . . . 99
1.12.7 Gamma Function (gamma) . . . . . . . . . . . . . . . . . 99
1.12.8 Error Function (erf) . . . . . . . . . . . . . . . . . . . . . 100
1.12.9 Complementary Error Function (erfc) . . . . . . . . . . . 101
1.12.10Inverse Error Function (erfinv) . . . . . . . . . . . . . . . 102
1.12.11Legendre Function (legendre) . . . . . . . . . . . . . . . . 102
1.13 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
1.13.1 Derivative (diff) . . . . . . . . . . . . . . . . . . . . . . . 103
1.13.2 Linear ODE Solver (lsode) . . . . . . . . . . . . . . . . . . 104
1.13.3 Options For Linear ODE Solver (lsode options) . . . . . . 106
1.13.4 Differential Algebraic System Solver (dassl) . . . . . . . . 106
1.13.5 Differential Algebraic System Solver Options (dassl options)114
1.13.6 Differential Algebraic Equations (daspk) . . . . . . . . . . 115
1.13.7 Differential Algebraic Equations Options (daspk options) 115
1.13.8 Differential Algebraic System Root Solver (dasrt) . . . . . 116
1.13.9 Differential Algebraic System Root Solver Options (dasrt options)117
1.14 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
1.14.1 Quadratic Integration (quad) . . . . . . . . . . . . . . . . 117
1.14.2 Vectorized Quadratic Integration (quadv) . . . . . . . . . 118
1.14.3 Quadratic Lobatto’s Integration (quadl) . . . . . . . . . . 119
1.14.4 Quadratic Gauss-Kronrod Integration (quadgk) . . . . . . 120
1.14.5 Quadratic Clenshaw-Curtis Integration (quadcc) . . . . . 121
1.14.6 Trapezoidal Integration (trapz) . . . . . . . . . . . . . . . 121
1.14.7 Double Quadratic Integration (dblquad) . . . . . . . . . . 122
1.14.8 Triple Quadratic Integration (triplequad) . . . . . . . . . 123
1.15 Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
1.15.1 Statistical Mean (mean) . . . . . . . . . . . . . . . . . . . 124
1.15.2 Mean of Square (meansq) . . . . . . . . . . . . . . . . . . 124
1.15.3 Statistical Median (median) . . . . . . . . . . . . . . . . . 125
1.15.4 Statistical Mode (mode) . . . . . . . . . . . . . . . . . . . 125
1.15.5 Statistical Range (range) . . . . . . . . . . . . . . . . . . 126
1.15.6 Standard Deviation (std) . . . . . . . . . . . . . . . . . . 126
1.15.7 Variance (var) . . . . . . . . . . . . . . . . . . . . . . . . 127
1.15.8 Skewness of Data (skewness) . . . . . . . . . . . . . . . . 128
1.15.9 Kurtosis of Data (kurtosis) . . . . . . . . . . . . . . . . . 128
7
https://sites.google.com/view/arunumrao
1.15.10Data Moment (moment) . . . . . . . . . . . . . . . . . . . 129
1.15.11Percentile of Data (prctile) . . . . . . . . . . . . . . . . . 130
1.15.12Standard Score (zscore) . . . . . . . . . . . . . . . . . . . 130
1.15.13Bin Histogram (histc) . . . . . . . . . . . . . . . . . . . . 131
1.15.14Combination - Choose ‘k’ From ‘n’ (nchoosek) . . . . . . 132
1.15.15Permutations (perms) . . . . . . . . . . . . . . . . . . . . 132
1.15.16Binomial Coefficient (bincoeff) . . . . . . . . . . . . . . . 133
2 Variable 137
2.1 Assigning a Variable . . . . . . . . . . . . . . . . . . . . . . . . . 137
2.1.1 Is String A Valid Variable Name . . . . . . . . . . . . . . 137
2.1.2 Generate A Variable Name . . . . . . . . . . . . . . . . . 138
2.2 Global Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
2.2.1 Is Variable Global . . . . . . . . . . . . . . . . . . . . . . 140
2.3 Persistant Of a Variable . . . . . . . . . . . . . . . . . . . . . . . 140
2.3.1 Clear Symbol From Table (clear) . . . . . . . . . . . . . . 140
2.3.2 Show Files in Directory (what) . . . . . . . . . . . . . . . 141
2.3.3 Function Name Type (which) . . . . . . . . . . . . . . . . 141
2.3.4 Scope of Variable (who) . . . . . . . . . . . . . . . . . . . 141
2.3.5 Exit From Loop (exist) . . . . . . . . . . . . . . . . . . . 141
3 Functions 143
3.1 Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
3.1.1 Defining A Functions . . . . . . . . . . . . . . . . . . . . . 144
3.1.2 Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 147
3.1.3 Key Words . . . . . . . . . . . . . . . . . . . . . . . . . . 147
3.1.4 Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
3.1.5 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
3.1.6 Number Format . . . . . . . . . . . . . . . . . . . . . . . 148
3.1.7 Number of Input Arguments (nargin) . . . . . . . . . . . 149
3.1.8 Number of Output Arguments (nargout) . . . . . . . . . . 150
3.1.9 How to Use a Function (usage) . . . . . . . . . . . . . . . 150
3.1.10 Error (error) . . . . . . . . . . . . . . . . . . . . . . . . . 150
3.1.11 Error Number (errno) . . . . . . . . . . . . . . . . . . . . 151
3.1.12 List of Error Numbers (errno list) . . . . . . . . . . . . . 151
3.1.13 Last Error (lasterr) . . . . . . . . . . . . . . . . . . . . . . 151
3.1.14 Last Warning (lastwarn) . . . . . . . . . . . . . . . . . . . 151
3.1.15 Variable Input Arguments (varargin) . . . . . . . . . . . . 152
3.1.16 Variable Output Arguments (varargout) . . . . . . . . . . 152
3.1.17 Tilde ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
3.1.18 Whether Argument is Out Type (isargout) . . . . . . . . 153
3.1.19 Return Command (return) . . . . . . . . . . . . . . . . . 154
3.1.20 Add Environmental Path (addpath) . . . . . . . . . . . . 154
3.1.21 Remove Environmental Path (rmpath) . . . . . . . . . . . 154
3.1.22 Lock A Function (mlock) . . . . . . . . . . . . . . . . . . 155
8
3.1.23 Unlock A Function (munlock) . . . . . . . . . . . . . . . . 155
3.1.24 Whether A Function is Locked (mislocked) . . . . . . . . 155
3.2 Function Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
3.2.1 Whether Argument is Function Handle (is function handle)156
3.2.2 Anonymous Functions . . . . . . . . . . . . . . . . . . . . 157
3.2.3 Sub Functions . . . . . . . . . . . . . . . . . . . . . . . . 157
3.2.4 Nested Function . . . . . . . . . . . . . . . . . . . . . . . 157
4 Conditional Function 159
4.1 Loop Condition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
4.1.1 For Condition (for) . . . . . . . . . . . . . . . . . . . . . . 159
4.1.2 While Condition (while) . . . . . . . . . . . . . . . . . . . 161
4.1.3 Do-until Condition (do ... until) . . . . . . . . . . . . . . 162
4.2 Switch Condition (switch) . . . . . . . . . . . . . . . . . . . . . . 162
4.2.1 If Condition (if) . . . . . . . . . . . . . . . . . . . . . . . 162
4.2.2 Break Command (break) . . . . . . . . . . . . . . . . . . 163
4.2.3 Continue Command (continue) . . . . . . . . . . . . . . . 163
4.2.4 Try-Catch Statement . . . . . . . . . . . . . . . . . . . . . 164
5 File Operation 167
5.1 PC Info (info) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
5.1.1 System Architecture (computer) . . . . . . . . . . . . . . 167
5.1.2 Is Windows (ispc) . . . . . . . . . . . . . . . . . . . . . . 167
5.1.3 Is Linux (isunix) . . . . . . . . . . . . . . . . . . . . . . . 167
5.1.4 Parent Machine (isdeployed) . . . . . . . . . . . . . . . . 168
5.1.5 Version (version) . . . . . . . . . . . . . . . . . . . . . . . 168
5.1.6 License (license) . . . . . . . . . . . . . . . . . . . . . . . 168
5.2 Directory Management . . . . . . . . . . . . . . . . . . . . . . . . 168
5.2.1 Make a Dir (mkdir) . . . . . . . . . . . . . . . . . . . . . 168
5.2.2 List Directory (ls) . . . . . . . . . . . . . . . . . . . . . . 169
5.2.3 Change Directory (cd) . . . . . . . . . . . . . . . . . . . . 169
5.2.4 Directory (dir) . . . . . . . . . . . . . . . . . . . . . . . . 169
5.2.5 Is Directory (isdir) . . . . . . . . . . . . . . . . . . . . . . 169
5.2.6 Remove Directory (rmdir) . . . . . . . . . . . . . . . . . . 169
5.2.7 Read Directory (readdir) . . . . . . . . . . . . . . . . . . 170
5.2.8 Temporary Directory (tempdir) . . . . . . . . . . . . . . . 170
5.2.9 Temporary Name (tempname) . . . . . . . . . . . . . . . 170
5.2.10 Attribute of File (fileattrib) . . . . . . . . . . . . . . . . . 171
5.2.11 File Marker (filemarker) . . . . . . . . . . . . . . . . . . . 171
5.2.12 File Parts (fileparts) . . . . . . . . . . . . . . . . . . . . . 171
5.2.13 File Separator (filesep) . . . . . . . . . . . . . . . . . . . . 171
5.2.14 Copy File (copyfile) . . . . . . . . . . . . . . . . . . . . . 171
5.2.15 Move File (movefile) . . . . . . . . . . . . . . . . . . . . . 172
5.2.16 Rename a File (rename) . . . . . . . . . . . . . . . . . . . 172
5.2.17 Delete a File (unlink) . . . . . . . . . . . . . . . . . . . . 172
9
https://sites.google.com/view/arunumrao
5.3 I/O Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
5.3.1 End of File (feof) . . . . . . . . . . . . . . . . . . . . . . . 172
5.3.2 Clear a File Stream (fclear) . . . . . . . . . . . . . . . . . 173
5.3.3 Open/Create a File (fopen) . . . . . . . . . . . . . . . . . 173
5.3.4 Close a File (fclose) . . . . . . . . . . . . . . . . . . . . . 173
5.3.5 Print Output (sprintf) . . . . . . . . . . . . . . . . . . . . 174
5.3.6 Output at Console (fprintf) . . . . . . . . . . . . . . . . . 174
5.3.7 File Error (ferror) . . . . . . . . . . . . . . . . . . . . . . 174
5.3.8 File Report (freport) . . . . . . . . . . . . . . . . . . . . . 174
5.3.9 Delete a File (delete) . . . . . . . . . . . . . . . . . . . . . 174
5.3.10 Scan a Value (scanf) . . . . . . . . . . . . . . . . . . . . . 175
5.3.11 Formatted Scanning (sscanf) . . . . . . . . . . . . . . . . 175
5.3.12 Rewind a Pointer (frewind) . . . . . . . . . . . . . . . . . 175
5.3.13 Scan to Buffer (fscanf) . . . . . . . . . . . . . . . . . . . . 175
5.3.14 Get Pointer Position (fseek) . . . . . . . . . . . . . . . . . 176
5.3.15 Pointer Position (ftell) . . . . . . . . . . . . . . . . . . . . 176
5.4 Reading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
5.4.1 Read File by Lines (fgetl) . . . . . . . . . . . . . . . . . . 177
5.4.2 Read File by Characters (fgets) . . . . . . . . . . . . . . . 177
5.4.3 Skip a Line (fskipl) . . . . . . . . . . . . . . . . . . . . . . 177
5.4.4 Read File in Binary (fread) . . . . . . . . . . . . . . . . . 178
5.4.5 CSV File Read (csvread) . . . . . . . . . . . . . . . . . . 181
5.5 Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
5.5.1 Write Into File Unformatted (fputs) . . . . . . . . . . . . 182
5.5.2 Write Into a File (fwrite) . . . . . . . . . . . . . . . . . . 182
5.5.3 CSV Write (csvwrite) . . . . . . . . . . . . . . . . . . . . 185
5.6 GUI Building . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
5.6.1 Getting Directory . . . . . . . . . . . . . . . . . . . . . . 186
5.6.2 Getting a File . . . . . . . . . . . . . . . . . . . . . . . . . 186
5.6.3 Process Status . . . . . . . . . . . . . . . . . . . . . . . . 187
5.6.4 Creating a Panel . . . . . . . . . . . . . . . . . . . . . . . 188
5.6.5 User Interactive Menu . . . . . . . . . . . . . . . . . . . . 189
5.6.6 Create a Context Menu . . . . . . . . . . . . . . . . . . . 190
5.6.7 Put String with Mouse . . . . . . . . . . . . . . . . . . . . 190
5.6.8 Create Control . . . . . . . . . . . . . . . . . . . . . . . . 191
5.6.9 Figure Tool . . . . . . . . . . . . . . . . . . . . . . . . . . 197
5.6.10 Toggle Tool . . . . . . . . . . . . . . . . . . . . . . . . . . 197
5.6.11 Dialogue Box . . . . . . . . . . . . . . . . . . . . . . . . . 198
5.6.12 Tool Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
5.6.13 Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . 200
5.6.14 GUI Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
5.6.15 Handler GUI . . . . . . . . . . . . . . . . . . . . . . . . . 205
5.6.16 Wait For Time . . . . . . . . . . . . . . . . . . . . . . . . 205
5.6.17 Resume Control . . . . . . . . . . . . . . . . . . . . . . . 206
10
6 Binary & Digital Operation 207
6.1 Logic Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
6.1.1 AND Logic Operation (and) . . . . . . . . . . . . . . . . . 207
6.1.2 OR Logic Operation (or) . . . . . . . . . . . . . . . . . . 207
6.1.3 NOT Logic Operation (not) . . . . . . . . . . . . . . . . . 208
6.1.4 Bitwise AND Logic (bitand) . . . . . . . . . . . . . . . . . 208
6.1.5 Bitwise Comparison (bitcmp) . . . . . . . . . . . . . . . . 208
6.1.6 Get a Specific Bit (bitget) . . . . . . . . . . . . . . . . . . 209
6.1.7 Maximum Bit Length (bitmax) . . . . . . . . . . . . . . . 209
6.1.8 Bitwise OR Operation (bitor) . . . . . . . . . . . . . . . . 210
6.1.9 Set a Specific Bit (bitset) . . . . . . . . . . . . . . . . . . 210
6.1.10 Bit Shift (bitshift) . . . . . . . . . . . . . . . . . . . . . . 211
6.1.11 Bitwise Exclusive OR Operation (bitxor) . . . . . . . . . 211
6.2 Base Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
6.2.1 Decimal To Binary (dec2bin) . . . . . . . . . . . . . . . . 211
6.2.2 Binary To Decimal (bin2dec) . . . . . . . . . . . . . . . . 212
6.2.3 Decimal To Hexadecimal (dec2hex) . . . . . . . . . . . . . 212
6.2.4 Hexadecimal To Decimal (hex2dec) . . . . . . . . . . . . . 213
7 String 215
7.1 String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
7.1.1 Blank Spaces (blanks) . . . . . . . . . . . . . . . . . . . . 216
7.1.2 Code to Character (char) . . . . . . . . . . . . . . . . . . 216
7.1.3 Remove White Spaces (deblank) . . . . . . . . . . . . . . 216
7.1.4 Escape String (do string escapes) . . . . . . . . . . . . . . 216
7.1.5 Un Escape String (undo string escapes) . . . . . . . . . . 217
7.2 String Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
7.2.1 Check Character String (ischar) . . . . . . . . . . . . . . . 217
7.2.2 Whether Object is a Cell (iscell) . . . . . . . . . . . . . . 217
7.2.3 Whether Object is a Cell String (iscellstr) . . . . . . . . . 217
7.2.4 Whether Object is Alpha-Numeric (isalnum) . . . . . . . 218
7.2.5 Whether Object is Alphabatic (isalpha) . . . . . . . . . . 218
7.2.6 Whether Object is Letter (isletter) . . . . . . . . . . . . . 218
7.2.7 Whether Object is Integer (isinteger) . . . . . . . . . . . . 219
7.2.8 Whether Object is in Lower Case (islower) . . . . . . . . 219
7.2.9 Whether Object is in Upper Case (isupper) . . . . . . . . 219
7.2.10 Whether Object is Digit (isdigit) . . . . . . . . . . . . . . 219
7.2.11 Whether Object is Hexadecimal (isxdigit) . . . . . . . . . 220
7.2.12 Whether Object is Punctuation (ispunct) . . . . . . . . . 220
7.2.13 Whether Object is Space (isspace) . . . . . . . . . . . . . 220
7.2.14 Whether Object is Control Letter (iscntrl) . . . . . . . . . 220
7.2.15 Whether Object is Graph (isgraph) . . . . . . . . . . . . . 221
7.2.16 Whether Print Space (isprint) . . . . . . . . . . . . . . . . 221
7.2.17 Whether Object is ASCII (isascii) . . . . . . . . . . . . . 221
11
https://sites.google.com/view/arunumrao
7.2.18 Whether Object is Scalar (isscalar) . . . . . . . . . . . . . 221
7.2.19 Whether Object is Vector (isvector) . . . . . . . . . . . . 221
7.2.20 Whether Elements Are in Order (issorted) . . . . . . . . . 221
7.3 String Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
7.3.1 Part of String (substr) . . . . . . . . . . . . . . . . . . . . 222
7.3.2 String To Double (str2double) . . . . . . . . . . . . . . . 222
7.3.3 String To Function (str2func) . . . . . . . . . . . . . . . . 223
7.3.4 Function To String (func2str) . . . . . . . . . . . . . . . . 223
7.3.5 String To Number (str2num) . . . . . . . . . . . . . . . . 223
7.3.6 Joining of Strings (cstrcat) . . . . . . . . . . . . . . . . . 224
7.3.7 String Joining (strcat) . . . . . . . . . . . . . . . . . . . . 224
7.3.8 Matrix To String (mat2str) . . . . . . . . . . . . . . . . . 224
7.3.9 Number To String (num2str) . . . . . . . . . . . . . . . . 225
7.3.10 Integer To String (int2str) . . . . . . . . . . . . . . . . . . 225
7.3.11 String To Character (strchr) . . . . . . . . . . . . . . . . 225
7.3.12 String Comparison (strcmp) . . . . . . . . . . . . . . . . . 226
7.3.13 Case Insensitive Comparison (strcmpi) . . . . . . . . . . . 227
7.3.14 Search Into String (strfind) . . . . . . . . . . . . . . . . . 227
7.3.15 Cell String Joining (strjoin) . . . . . . . . . . . . . . . . . 227
7.3.16 Alignment of String (strjust) . . . . . . . . . . . . . . . . 227
7.3.17 Character Length Comparision (strncmp) . . . . . . . . . 228
7.3.18 Case Insensitive Character Length Comparision (strncmpi)228
7.3.19 Replace In String (strrep) . . . . . . . . . . . . . . . . . . 229
7.3.20 Split a String (strsplit) . . . . . . . . . . . . . . . . . . . . 229
7.3.21 String Tokenization (strtok) . . . . . . . . . . . . . . . . . 229
7.3.22 Trim String (strtrim) . . . . . . . . . . . . . . . . . . . . . 230
7.3.23 Truncate String (strtrunc) . . . . . . . . . . . . . . . . . . 230
7.3.24 Verticl Mode String Trucation (strvcat) . . . . . . . . . . 230
7.3.25 Convert to ASCII (toascii) . . . . . . . . . . . . . . . . . 231
7.3.26 Change To Lower Case (tolower) . . . . . . . . . . . . . . 231
7.3.27 Change To Upper Case (toupper) . . . . . . . . . . . . . . 231
7.3.28 Regular Expression Search (regexp) . . . . . . . . . . . . 231
7.3.29 Case Insensitive Regular Expression Search (regexpi) . . . 233
8 2D-Graphics 235
8.1 Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
8.1.1 Axis (axis) . . . . . . . . . . . . . . . . . . . . . . . . . . 235
8.2 Cartesian Plot (plot) . . . . . . . . . . . . . . . . . . . . . . . . . 236
8.2.1 Plot With Two Y Axes (plotyy) . . . . . . . . . . . . . . 241
8.2.2 Function Plot (fplot) . . . . . . . . . . . . . . . . . . . . . 242
8.2.3 Parametric Plot (ezplot) . . . . . . . . . . . . . . . . . . . 243
8.2.4 Three-D Parametric Plot (ezplot3) . . . . . . . . . . . . . 245
8.3 Bar Plot (bar) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
8.3.1 Horizontal Bar (barh) . . . . . . . . . . . . . . . . . . . . 247
8.3.2 Stair Like Bar Plot (stairs) . . . . . . . . . . . . . . . . . 247
12
8.3.3 Stem Plot (stem) . . . . . . . . . . . . . . . . . . . . . . . 249
8.3.4 Pareto Plot (pareto) . . . . . . . . . . . . . . . . . . . . . 249
8.4 Contour Plot (contour) . . . . . . . . . . . . . . . . . . . . . . . . 250
8.4.1 Function Contour Plot (ezcontour) . . . . . . . . . . . . . 254
8.4.2 Parametric Contour Plot (ezcontourf) . . . . . . . . . . . 255
8.4.3 Parametric Polar Plot (ezpolar) . . . . . . . . . . . . . . . 256
8.4.4 Scattered Data Plot (scatter) . . . . . . . . . . . . . . . . 258
8.5 Error Bar Plot (errorbar) . . . . . . . . . . . . . . . . . . . . . . 258
8.5.1 Semi X-axes Log Error (semilogxerr) . . . . . . . . . . . . 259
8.5.2 Semi Y-axes Log Error (semilogyerr) . . . . . . . . . . . . 259
8.5.3 Logarithmic Error (loglogerr) . . . . . . . . . . . . . . . . 259
8.6 Polar Plot (polar) . . . . . . . . . . . . . . . . . . . . . . . . . . 260
8.7 Pie Chart (pie) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
8.8 Quiver Plot (quiver) . . . . . . . . . . . . . . . . . . . . . . . . . 261
8.9 Comet Plot (comet) . . . . . . . . . . . . . . . . . . . . . . . . . 262
8.10 Area Plot (area) . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
8.11 Geometric Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
8.11.1 Cylindrical Plot (cylinder) . . . . . . . . . . . . . . . . . . 263
8.11.2 Sphere Plot (sphere) . . . . . . . . . . . . . . . . . . . . . 264
9 3D-Graphics 265
9.1 Mesh Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
9.1.1 Mesh Grid (meshgrid) . . . . . . . . . . . . . . . . . . . . 265
9.1.2 n-dimensional Grid (ngrid) . . . . . . . . . . . . . . . . . 265
9.1.3 View Point (view) . . . . . . . . . . . . . . . . . . . . . . 265
9.1.4 Mesh Plot (mesh) . . . . . . . . . . . . . . . . . . . . . . 266
9.1.5 Parametric Surface Plot (ezmesh) . . . . . . . . . . . . . . 267
9.2 Thee Dimensional Plot (plot3d) . . . . . . . . . . . . . . . . . . . 268
9.3 Surface Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
9.3.1 Parametric Surface Plot (ezsurf) . . . . . . . . . . . . . . 269
9.3.2 Surface & Contour (surfc) . . . . . . . . . . . . . . . . . . 270
9.3.3 Surface Light (surfl) . . . . . . . . . . . . . . . . . . . . . 270
9.4 Plot Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
9.4.1 Title (title) . . . . . . . . . . . . . . . . . . . . . . . . . . 271
9.4.2 X, Y & Z-axis Labels . . . . . . . . . . . . . . . . . . . . 271
9.4.3 Text (text) . . . . . . . . . . . . . . . . . . . . . . . . . . 271
9.4.4 Legends (legend) . . . . . . . . . . . . . . . . . . . . . . . 272
9.5 Graphics Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
9.5.1 Group of Plots . . . . . . . . . . . . . . . . . . . . . . . . 273
9.5.2 Multiple Figure Window . . . . . . . . . . . . . . . . . . . 273
9.5.3 Print or Save a Plot . . . . . . . . . . . . . . . . . . . . . 274
9.5.4 Graphics Handler . . . . . . . . . . . . . . . . . . . . . . . 275
9.5.5 Handling of Graphic’s Object . . . . . . . . . . . . . . . . 275
9.5.6 Is Handle is Figure (isfigure) . . . . . . . . . . . . . . . . 276
9.5.7 Get Current Figure Object (gcf) . . . . . . . . . . . . . . 276
13
https://sites.google.com/view/arunumrao
9.5.8 Get Current Axes Object (gca) . . . . . . . . . . . . . . . 278
9.5.9 Get Scalar Structure Object (get) . . . . . . . . . . . . . 280
10 Network 281
10.1 File Transfer Protocol (FTP) . . . . . . . . . . . . . . . . . . . . 281
10.1.1 Open File Transfer Protocol (ftp) . . . . . . . . . . . . . . 281
10.1.2 Close File Transfer Protocol (close) . . . . . . . . . . . . . 281
10.1.3 Read/Download a File (mget) . . . . . . . . . . . . . . . . 282
10.1.4 Write/Upload a File (mput) . . . . . . . . . . . . . . . . . 282
10.1.5 Search Directory (dir) . . . . . . . . . . . . . . . . . . . . 282
10.1.6 Delete A File (delete) . . . . . . . . . . . . . . . . . . . . 282
10.1.7 Rename A File (rename) . . . . . . . . . . . . . . . . . . . 282
10.1.8 Crete A Directory (mkdir) . . . . . . . . . . . . . . . . . . 283
10.1.9 Remove A Directory (rmdir) . . . . . . . . . . . . . . . . 283
11 Others 285
11.1 Hardware Access . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
11.1.1 CPU Bell (beep) . . . . . . . . . . . . . . . . . . . . . . . 285
11.1.2 Bell if Error (beep on error) . . . . . . . . . . . . . . . . . 285
11.1.3 Show All Variables (all) . . . . . . . . . . . . . . . . . . . 285
11.1.4 Date & Time (clock) . . . . . . . . . . . . . . . . . . . . . 285
11.1.5 Date In Number Format (datenum) . . . . . . . . . . . . 285
11.1.6 Date As String (datestr) . . . . . . . . . . . . . . . . . . . 286
11.1.7 Ticks As Date In Axes (datetick) . . . . . . . . . . . . . . 286
11.1.8 Date As Vector (datevec) . . . . . . . . . . . . . . . . . . 286
11.2 Octave Misc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
11.2.1 Close Octave Connection (exit) . . . . . . . . . . . . . . . 287
11.2.2 Quit The Session (quit) . . . . . . . . . . . . . . . . . . . 287
11.2.3 Display In Console (disp) . . . . . . . . . . . . . . . . . . 287
11.2.4 Compress Into GZIP (gzip) . . . . . . . . . . . . . . . . . 287
11.2.5 Decompress From GZIP (gunzip) . . . . . . . . . . . . . . 288
11.2.6 Get Octave Environmental Path (getenv) . . . . . . . . . 288
11.2.7 Add Octave Environmental Path (putenv) . . . . . . . . . 288
11.2.8 Set A Value to Environmental Variable (setenv) . . . . . 288
11.3 Writing Mex Macros . . . . . . . . . . . . . . . . . . . . . . . . . 288
11.3.1 Core Functions . . . . . . . . . . . . . . . . . . . . . . . . 288
mex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
mexext . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
11.3.2 Matrix & Array . . . . . . . . . . . . . . . . . . . . . . . . 291
11.3.3 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
11.3.4 Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
11.3.5 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
11.3.6 Calling Other Functions . . . . . . . . . . . . . . . . . . . 297
11.4 Standalone Programs . . . . . . . . . . . . . . . . . . . . . . . . . 297
14 Mathematics
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 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
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 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
✌
✆
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
1 3.5 × 10−12
35 × 10−12
1
Arithmetic operators are explained below:
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
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]
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]
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.
1 2
3 4

− λ

1 0
0 1
= 0
Or
1 − λ 2
3 4 − λ
= 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
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
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]);
✌
✆
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);
✌
✆
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.
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
✌
✆
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
✌
✆
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.
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
✌
✆
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
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
✌
✆
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
✌
✆
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
✌
✆
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
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
✌
✆
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.
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.
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)
✌
✆
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
✌
✆
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
✌
✆
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.
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.
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)
✌
✆
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”.
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
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 =
p
(x2 − x1)2 + (y2 − y1)2
b =
p
(x3 − x2)2 + (y3 − y2)2
c =
p
(x3 − x1)2 + (y3 − y1)2
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
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)
✌
✆
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.
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.
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)
✌
✆
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
✌
✆
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
✌
✆
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
✌
✆
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
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
✌
✆
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
✌
✆
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.
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
✌
✆
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)
✌
✆
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 ϕ
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)
✌
✆
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
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
✌
✆
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
✌
✆
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)
✌
✆
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
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02
Gnu octave help book 01 of 02

Contenu connexe

Tendances

Morton john canty image analysis and pattern recognition for remote sensing...
Morton john canty   image analysis and pattern recognition for remote sensing...Morton john canty   image analysis and pattern recognition for remote sensing...
Morton john canty image analysis and pattern recognition for remote sensing...
Kevin Peña Ramos
 
Shreve, Steven - Stochastic Calculus for Finance I: The Binomial Asset Pricin...
Shreve, Steven - Stochastic Calculus for Finance I: The Binomial Asset Pricin...Shreve, Steven - Stochastic Calculus for Finance I: The Binomial Asset Pricin...
Shreve, Steven - Stochastic Calculus for Finance I: The Binomial Asset Pricin...
raffamaiden
 

Tendances (16)

Assembly Codes in C Programmes - A Short Notes by Arun Umrao
Assembly Codes in C Programmes - A Short Notes by Arun UmraoAssembly Codes in C Programmes - A Short Notes by Arun Umrao
Assembly Codes in C Programmes - A Short Notes by Arun Umrao
 
Java Programming Notes for Beginners by Arun Umrao
Java Programming Notes for Beginners by Arun UmraoJava Programming Notes for Beginners by Arun Umrao
Java Programming Notes for Beginners by Arun Umrao
 
c programming
c programmingc programming
c programming
 
Principles of Linear Motion of Objects - Physics - Explained by Arun Umrao
Principles of Linear Motion of Objects - Physics - Explained by Arun UmraoPrinciples of Linear Motion of Objects - Physics - Explained by Arun Umrao
Principles of Linear Motion of Objects - Physics - Explained by Arun Umrao
 
Notes of 8085 micro processor Programming for BCA, MCA, MSC (CS), MSC (IT) &...
Notes of 8085 micro processor Programming  for BCA, MCA, MSC (CS), MSC (IT) &...Notes of 8085 micro processor Programming  for BCA, MCA, MSC (CS), MSC (IT) &...
Notes of 8085 micro processor Programming for BCA, MCA, MSC (CS), MSC (IT) &...
 
Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...
Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...
Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...
 
Advanced cardiovascular exercites
Advanced cardiovascular exercitesAdvanced cardiovascular exercites
Advanced cardiovascular exercites
 
Free high-school-science-texts-physics
Free high-school-science-texts-physicsFree high-school-science-texts-physics
Free high-school-science-texts-physics
 
genral physis
genral physisgenral physis
genral physis
 
Qmall20121005
Qmall20121005Qmall20121005
Qmall20121005
 
Morton john canty image analysis and pattern recognition for remote sensing...
Morton john canty   image analysis and pattern recognition for remote sensing...Morton john canty   image analysis and pattern recognition for remote sensing...
Morton john canty image analysis and pattern recognition for remote sensing...
 
Wavelet ug
Wavelet ugWavelet ug
Wavelet ug
 
Lower Bound methods for the Shakedown problem of WC-Co composites
Lower Bound methods for the Shakedown problem of WC-Co compositesLower Bound methods for the Shakedown problem of WC-Co composites
Lower Bound methods for the Shakedown problem of WC-Co composites
 
Principle of Angular Motion - Physics - An Introduction by Arun Umrao
Principle of Angular Motion - Physics - An Introduction by Arun UmraoPrinciple of Angular Motion - Physics - An Introduction by Arun Umrao
Principle of Angular Motion - Physics - An Introduction by Arun Umrao
 
Shreve, Steven - Stochastic Calculus for Finance I: The Binomial Asset Pricin...
Shreve, Steven - Stochastic Calculus for Finance I: The Binomial Asset Pricin...Shreve, Steven - Stochastic Calculus for Finance I: The Binomial Asset Pricin...
Shreve, Steven - Stochastic Calculus for Finance I: The Binomial Asset Pricin...
 
Signals and systems
Signals  and systemsSignals  and systems
Signals and systems
 

Similaire à Gnu octave help book 01 of 02

Scilab help book 1 of 2
Scilab help book 1 of 2Scilab help book 1 of 2
Scilab help book 1 of 2
Arun Umrao
 
Xcos simulation
Xcos simulationXcos simulation
Xcos simulation
Arun Umrao
 
Fortran programming help book
Fortran programming help bookFortran programming help book
Fortran programming help book
Arun Umrao
 
Notes of Java
Notes of JavaNotes of Java
Notes of Java
Arun Umrao
 
M.Sc_Maths Thesis Pradeep Mishra
M.Sc_Maths Thesis Pradeep MishraM.Sc_Maths Thesis Pradeep Mishra
M.Sc_Maths Thesis Pradeep Mishra
Pradeep Mishra
 
Modelica programming help book
Modelica programming help bookModelica programming help book
Modelica programming help book
Arun Umrao
 
Soal latihan algoritma
Soal latihan  algoritmaSoal latihan  algoritma
Soal latihan algoritma
Hayi Nukman
 

Similaire à Gnu octave help book 01 of 02 (20)

Scilab help book 1 of 2
Scilab help book 1 of 2Scilab help book 1 of 2
Scilab help book 1 of 2
 
Xcos simulation
Xcos simulationXcos simulation
Xcos simulation
 
Introduction to abstract algebra
Introduction to abstract algebraIntroduction to abstract algebra
Introduction to abstract algebra
 
Thats How We C
Thats How We CThats How We C
Thats How We C
 
Replect
ReplectReplect
Replect
 
Reading Materials for Operational Research
Reading Materials for Operational Research Reading Materials for Operational Research
Reading Materials for Operational Research
 
Fortran programming help book
Fortran programming help bookFortran programming help book
Fortran programming help book
 
Elementary Number Theory with Applications Koshy.pdf
Elementary Number Theory with Applications Koshy.pdfElementary Number Theory with Applications Koshy.pdf
Elementary Number Theory with Applications Koshy.pdf
 
Introduction to c++ (cpp)
Introduction to c++ (cpp)Introduction to c++ (cpp)
Introduction to c++ (cpp)
 
An Introduction to MATLAB for Geoscientists.pdf
An Introduction to MATLAB for Geoscientists.pdfAn Introduction to MATLAB for Geoscientists.pdf
An Introduction to MATLAB for Geoscientists.pdf
 
Notes of Java
Notes of JavaNotes of Java
Notes of Java
 
Ee3054 exercises
Ee3054 exercisesEe3054 exercises
Ee3054 exercises
 
M.Sc_Maths Thesis Pradeep Mishra
M.Sc_Maths Thesis Pradeep MishraM.Sc_Maths Thesis Pradeep Mishra
M.Sc_Maths Thesis Pradeep Mishra
 
main-moonmath.pdf
main-moonmath.pdfmain-moonmath.pdf
main-moonmath.pdf
 
I do like cfd vol 1 2ed_v2p2
I do like cfd vol 1 2ed_v2p2I do like cfd vol 1 2ed_v2p2
I do like cfd vol 1 2ed_v2p2
 
Algorithms for programers
Algorithms for programersAlgorithms for programers
Algorithms for programers
 
Partial differential equations, graduate level problems and solutions by igor...
Partial differential equations, graduate level problems and solutions by igor...Partial differential equations, graduate level problems and solutions by igor...
Partial differential equations, graduate level problems and solutions by igor...
 
Math for programmers
Math for programmersMath for programmers
Math for programmers
 
Modelica programming help book
Modelica programming help bookModelica programming help book
Modelica programming help book
 
Soal latihan algoritma
Soal latihan  algoritmaSoal latihan  algoritma
Soal latihan algoritma
 

Plus de Arun Umrao

Scilab help book 2 of 2
Scilab help book 2 of 2Scilab help book 2 of 2
Scilab help book 2 of 2
Arun Umrao
 
Linear motion for k12 students
Linear motion for k12 studentsLinear motion for k12 students
Linear motion for k12 students
Arun Umrao
 
Gnu octave help book 02 of 02
Gnu octave help book 02 of 02Gnu octave help book 02 of 02
Gnu octave help book 02 of 02
Arun Umrao
 

Plus de Arun Umrao (20)

maths_practice_sheet_28.12.23 (copy).pdf
maths_practice_sheet_28.12.23 (copy).pdfmaths_practice_sheet_28.12.23 (copy).pdf
maths_practice_sheet_28.12.23 (copy).pdf
 
Function Analysis v.2
Function Analysis v.2Function Analysis v.2
Function Analysis v.2
 
Average value by integral method
Average value by integral methodAverage value by integral method
Average value by integral method
 
Work and energy
Work and energyWork and energy
Work and energy
 
Units of physical quantities
Units of physical quantitiesUnits of physical quantities
Units of physical quantities
 
Scilab help book 2 of 2
Scilab help book 2 of 2Scilab help book 2 of 2
Scilab help book 2 of 2
 
Measurements and errors
Measurements and errorsMeasurements and errors
Measurements and errors
 
Linear motion for k12 students
Linear motion for k12 studentsLinear motion for k12 students
Linear motion for k12 students
 
Gnu octave help book 02 of 02
Gnu octave help book 02 of 02Gnu octave help book 02 of 02
Gnu octave help book 02 of 02
 
Force and its application for k12 students
Force and its application for k12 studentsForce and its application for k12 students
Force and its application for k12 students
 
Electric field for k12 student
Electric field for k12 studentElectric field for k12 student
Electric field for k12 student
 
Dictionary of physics
Dictionary of physicsDictionary of physics
Dictionary of physics
 
Decreasing and increasing function
Decreasing and increasing functionDecreasing and increasing function
Decreasing and increasing function
 
Circular motion
Circular motionCircular motion
Circular motion
 
Angular motion
Angular motionAngular motion
Angular motion
 
Who is successful
Who is successfulWho is successful
Who is successful
 
Energy and power source
Energy and power sourceEnergy and power source
Energy and power source
 
Definitions related to measurements
Definitions related to measurementsDefinitions related to measurements
Definitions related to measurements
 
Errors in Measurement
Errors in MeasurementErrors in Measurement
Errors in Measurement
 
Introduction to mechanical waves
Introduction to mechanical wavesIntroduction to mechanical waves
Introduction to mechanical waves
 

Dernier

CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 

Dernier (20)

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 

Gnu octave help book 01 of 02

  • 1. 1 GNU OCTAVE AN INTRODUCTION Arun Umrao https://sites.google.com/view/arunumrao DRAFT COPY - GPL LICENSING
  • 2. 2 Contents 1 Mathematics 3 1.1 Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1.1 Index Expression . . . . . . . . . . . . . . . . . . . . . . . 3 1.1.2 Reshape Elements (reshape) . . . . . . . . . . . . . . . . . 7 1.1.3 Is Element an Index (isindex) . . . . . . . . . . . . . . . . 7 1.2 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . 7 1.2.1 Addition & Subtraction . . . . . . . . . . . . . . . . . . . 8 1.2.2 Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.2.3 Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.2.4 Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.2.5 Unary Operations . . . . . . . . . . . . . . . . . . . . . . 13 1.2.6 Left Division (ldivide) . . . . . . . . . . . . . . . . . . . . 13 1.2.7 Matrix Left Division (mldivide) . . . . . . . . . . . . . . . 13 1.2.8 Subtraction (minus) . . . . . . . . . . . . . . . . . . . . . 14 1.2.9 Matrix Power (mpower) . . . . . . . . . . . . . . . . . . . 14 1.2.10 Matrix Right Division (mrdivide) . . . . . . . . . . . . . . 15 1.2.11 Recursive Product (mtimes) . . . . . . . . . . . . . . . . . 15 1.2.12 Element-wise Recursive Product (times) . . . . . . . . . . 16 1.2.13 Element-wise Right Division (rdivide) . . . . . . . . . . . 16 1.2.14 Addition of Elements (plus) . . . . . . . . . . . . . . . . . 16 1.2.15 Power (power) . . . . . . . . . . . . . . . . . . . . . . . . 17 1.2.16 Unary Subtraction (uminus) . . . . . . . . . . . . . . . . . 17 1.2.17 Unary Addition (uplus) . . . . . . . . . . . . . . . . . . . 17 1.3 Comparison Operator . . . . . . . . . . . . . . . . . . . . . . . . 18 1.3.1 Equals (eq) . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.3.2 Greater Than or Equal (ge) . . . . . . . . . . . . . . . . . 19 1.3.3 Greater Than (gt) . . . . . . . . . . . . . . . . . . . . . . 19 1.3.4 Is Arguments are Equal (isequal) . . . . . . . . . . . . . . 20 1.3.5 Less Than or Equal (le) . . . . . . . . . . . . . . . . . . . 20 1.3.6 Less Than (lt) . . . . . . . . . . . . . . . . . . . . . . . . 21 1.3.7 Not Equals (ne) . . . . . . . . . . . . . . . . . . . . . . . 21 1.4 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.5 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.5.1 Absolute Value (abs) . . . . . . . . . . . . . . . . . . . . . 22 1.5.2 Ceiling (ceil) . . . . . . . . . . . . . . . . . . . . . . . . . 23 1.5.3 Truncate Fraction (fix) . . . . . . . . . . . . . . . . . . . . 23 1.5.4 floor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.5.5 Round Value (round) . . . . . . . . . . . . . . . . . . . . 24 1.5.6 Product Elements (prod) . . . . . . . . . . . . . . . . . . 24 1.5.7 Sum All Elements (sum) . . . . . . . . . . . . . . . . . . . 25
  • 3. 3 https://sites.google.com/view/arunumrao 1.5.8 Sum of Square of Elements (sumsq) . . . . . . . . . . . . 25 1.5.9 Maximum Value (max) . . . . . . . . . . . . . . . . . . . 25 1.5.10 Minimum Value (min) . . . . . . . . . . . . . . . . . . . . 26 1.5.11 Cumulative Maximum (cummax) . . . . . . . . . . . . . . 26 1.5.12 Cumulative Minimum (cummin) . . . . . . . . . . . . . . 26 1.5.13 Cumulative Product (cumprod) . . . . . . . . . . . . . . . 26 1.5.14 Cumulative Summation (cumsum) . . . . . . . . . . . . . 27 1.5.15 Output Precisions (output precision) . . . . . . . . . . . . 27 1.5.16 Is Element Float Value (isfloat) . . . . . . . . . . . . . . . 27 1.5.17 Is Not A Number (isnan) . . . . . . . . . . . . . . . . . . 27 1.5.18 Is Null Value (isnull) . . . . . . . . . . . . . . . . . . . . . 28 1.5.19 Is Numeric Value (isnumeric) . . . . . . . . . . . . . . . . 28 1.5.20 Is Prime Number (isprime) . . . . . . . . . . . . . . . . . 28 1.5.21 Is Real Number (isreal) . . . . . . . . . . . . . . . . . . . 28 1.5.22 Is Empty (isempty) . . . . . . . . . . . . . . . . . . . . . . 28 1.5.23 Are Elements Equal (isequal) . . . . . . . . . . . . . . . . 28 1.5.24 Is Argument A Matrix (ismatrix) . . . . . . . . . . . . . . 29 1.5.25 Is Element Infity (isinf) . . . . . . . . . . . . . . . . . . . 29 1.5.26 Is Vector Not A Number (isnan) . . . . . . . . . . . . . . 29 1.5.27 Binary Power (pow2) . . . . . . . . . . . . . . . . . . . . 29 1.5.28 Get Real Part of Complex Number (real) . . . . . . . . . 30 1.5.29 Real Logarithm of Element (reallog) . . . . . . . . . . . . 30 1.5.30 Real Maximum Value (realmax) . . . . . . . . . . . . . . 30 1.5.31 Real Minimum Value (realmin) . . . . . . . . . . . . . . . 31 1.5.32 Real Power (realpow) . . . . . . . . . . . . . . . . . . . . 31 1.5.33 Real Square Root Value (realsqrt) . . . . . . . . . . . . . 31 1.5.34 Square Root (sqrt) . . . . . . . . . . . . . . . . . . . . . . 31 1.5.35 Nth Root (nthroot) . . . . . . . . . . . . . . . . . . . . . . 32 1.5.36 Infinity Variable (Inf) . . . . . . . . . . . . . . . . . . . . 32 1.5.37 Not A Number Variable (NaN) . . . . . . . . . . . . . . . 33 1.6 Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 1.6.1 Polynomial (poly) . . . . . . . . . . . . . . . . . . . . . . 33 1.6.2 Roots of Polynomial (roots) . . . . . . . . . . . . . . . . . 33 1.6.3 Convolution (conv) . . . . . . . . . . . . . . . . . . . . . . 34 1.6.4 Residue Of a Polynomial Fraction (residue) . . . . . . . . 35 1.6.5 Area of Polygon (polyarea) . . . . . . . . . . . . . . . . . 36 1.6.6 Polynomial Derivatives (polyder) . . . . . . . . . . . . . . 36 1.6.7 Greatest Common Divisor of Polynomials (polygcd) . . . 38 1.6.8 Polynomial Integral (polyint) . . . . . . . . . . . . . . . . 39 1.6.9 Polynomial Output (polyout) . . . . . . . . . . . . . . . . 40 1.6.10 Reduce Polynomial (polyreduce) . . . . . . . . . . . . . . 40 1.6.11 Evaluate Polynomial (polyval) . . . . . . . . . . . . . . . 40 1.6.12 Matrix Polynomial Evaluation (polyvalm) . . . . . . . . . 41 1.6.13 Factorial (factorial) . . . . . . . . . . . . . . . . . . . . . . 41 1.6.14 Prime Factor of a Number (factor) . . . . . . . . . . . . . 42
  • 4. 4 1.6.15 Greatest Common Divisor of Numbers (gcd) . . . . . . . 42 1.6.16 Least Common Multiple (lcm) . . . . . . . . . . . . . . . 42 1.6.17 Remainder of Division of Two Element (rem) . . . . . . . 43 1.6.18 Modulo of Two Number (mod) . . . . . . . . . . . . . . . 43 1.6.19 Prime Numbers (primes) . . . . . . . . . . . . . . . . . . 43 1.6.20 Evaluates Algebraic Equations (fsolve) . . . . . . . . . . . 44 1.6.21 Find Zeros (fzero) . . . . . . . . . . . . . . . . . . . . . . 45 1.6.22 Evaluate Function Name (feval) . . . . . . . . . . . . . . . 45 1.7 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . 46 1.7.1 Absolute Number (abs) . . . . . . . . . . . . . . . . . . . 46 1.7.2 Argument of Complex Number (arg) . . . . . . . . . . . . 46 1.7.3 Argument of Complex Number (angle) . . . . . . . . . . . 46 1.7.4 Conjugate of Complex Number (conj) . . . . . . . . . . . 47 1.7.5 Imaginary Part of Complex Number (imag) . . . . . . . . 47 1.7.6 Real Part of Complex Number (real) . . . . . . . . . . . . 47 1.8 Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 1.8.1 Cartesian to Polar Conversion (cart2pol) . . . . . . . . . 48 1.8.2 Polar to Cartesian Conversion (pol2cart) . . . . . . . . . 49 1.8.3 Spherical to Cartesian Conversion (sph2cart) . . . . . . . 50 1.8.4 Cartesian to Spherical Conversion (cart2sph) . . . . . . . 51 1.9 Logarithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 1.9.1 Natural Logarithm (log) . . . . . . . . . . . . . . . . . . . 53 1.9.2 Logarithm Base Ten (log10) . . . . . . . . . . . . . . . . . 53 1.9.3 Unit Increment Logarithm (log1p) . . . . . . . . . . . . . 53 1.9.4 Binary Base Logarithm (log2) . . . . . . . . . . . . . . . . 54 1.9.5 Exponential Base (e) . . . . . . . . . . . . . . . . . . . . . 54 1.10 Trigonometric Functions . . . . . . . . . . . . . . . . . . . . . . . 55 1.10.1 Inverse Trigonometric Functions . . . . . . . . . . . . . . 55 Inverse Sine in Radian (asin) . . . . . . . . . . . . . . . . 55 Inverse Sine in Degree (asind) . . . . . . . . . . . . . . . . 55 Inverse Hyperbolic Sine (asinh) . . . . . . . . . . . . . . . 55 Inverse Cosine in Radian (acos) . . . . . . . . . . . . . . . 56 Inverse Cosine In Degree (acosd) . . . . . . . . . . . . . . 56 Inverse Hyperbolic Cosine (acosh) . . . . . . . . . . . . . 56 Inverse Tangent in Radian (atan) . . . . . . . . . . . . . . 57 Inverse Tangent in Degree (atand) . . . . . . . . . . . . . 57 Inverse Hyperbolic Tangent (atanh) . . . . . . . . . . . . 57 Inverse Cotangent in Radian(acot) . . . . . . . . . . . . . 58 Inverse Cotangent in Degree (acotd) . . . . . . . . . . . . 58 Inverse Hyperbolic Cotangent (acoth) . . . . . . . . . . . 58 Inverse Secant in Radian (asec) . . . . . . . . . . . . . . . 59 Inverse Secant in Degree (asecd) . . . . . . . . . . . . . . 59 Inverse Hyperbolic Secant (asech) . . . . . . . . . . . . . 60 Inverse Cosecant in Radian (acsc) . . . . . . . . . . . . . 60 Inverse Cosecant in Degree (acscd) . . . . . . . . . . . . . 61
  • 5. 5 https://sites.google.com/view/arunumrao Inverse Hyperbolic Cosecant (acsch) . . . . . . . . . . . . 61 1.10.2 Trigonometric Functions . . . . . . . . . . . . . . . . . . . 61 Sine - Argument in Radian (sin) . . . . . . . . . . . . . . 61 Cardinal Sine (sinc) . . . . . . . . . . . . . . . . . . . . . 62 Sine - Argument in Degree (sind) . . . . . . . . . . . . . . 62 Hyperbolic Sine (sinh) . . . . . . . . . . . . . . . . . . . . 63 Cosine - Argument In Radian (cos) . . . . . . . . . . . . . 63 Cosine - Argument In Degree (cosd) . . . . . . . . . . . . 63 Hyperbolic Cosine (cosh) . . . . . . . . . . . . . . . . . . 63 Tangent - Argument in Radian (tan) . . . . . . . . . . . . 64 Tangent - Argument in Degree (tand) . . . . . . . . . . . 64 Hyperbolic Tangent (tanh) . . . . . . . . . . . . . . . . . 64 Slope Angle (atan2) . . . . . . . . . . . . . . . . . . . . . 65 Cotangent - Argument in Degree (cotd) . . . . . . . . . . 65 Hyperbolic Cotangent (coth) . . . . . . . . . . . . . . . . 65 Secant - Argument in Radian (sec) . . . . . . . . . . . . . 65 Secant - Argument in Degree (secd) . . . . . . . . . . . . 66 Hyperbolic Secant (sech) . . . . . . . . . . . . . . . . . . 66 Cosecant - Input in Radian (csc) . . . . . . . . . . . . . . 66 Cosecant - Input in Degree (cscd) . . . . . . . . . . . . . 66 Hyperbolic Cosecant (csch) . . . . . . . . . . . . . . . . . 67 1.11 Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 1.11.1 Transpose of Matrix (transpose) . . . . . . . . . . . . . . 72 1.11.2 Complex Conjugate Transpose of Matrix (ctranspose) . . 73 1.11.3 Dot Product (dot) . . . . . . . . . . . . . . . . . . . . . . 74 1.11.4 Cross Product (cross) . . . . . . . . . . . . . . . . . . . . 74 1.11.5 Determinant (det) . . . . . . . . . . . . . . . . . . . . . . 75 1.11.6 Identity Matrix (eye) . . . . . . . . . . . . . . . . . . . . . 76 1.11.7 Eigenvalues (eig) . . . . . . . . . . . . . . . . . . . . . . . 76 1.11.8 Eigens (eigs) . . . . . . . . . . . . . . . . . . . . . . . . . 77 1.11.9 Inverse of Matrix (inv) . . . . . . . . . . . . . . . . . . . . 79 1.11.10Linear Equation Solver (linsolve) . . . . . . . . . . . . . . 80 1.11.11Type of Matrix (matrix type) . . . . . . . . . . . . . . . . 82 1.11.12Normalized Matrix (norm) . . . . . . . . . . . . . . . . . 83 1.11.13Null Space Matrix (null) . . . . . . . . . . . . . . . . . . . 84 1.11.14Orthogonal Basis (orth) . . . . . . . . . . . . . . . . . . . 85 1.11.15Rank of Matrix (rank) . . . . . . . . . . . . . . . . . . . . 85 1.11.16Trace of Matrix (trace) . . . . . . . . . . . . . . . . . . . 85 1.11.17Cholesky Matrix (chol) . . . . . . . . . . . . . . . . . . . 86 1.11.18Inverse Cholesky Matrix (cholinv) . . . . . . . . . . . . . 87 1.11.19Matrix Exponential (expm) . . . . . . . . . . . . . . . . . 87 1.11.20Logarithmic Matrix (logm) . . . . . . . . . . . . . . . . . 88 1.11.21Square Root of Matrix (sqrtm) . . . . . . . . . . . . . . . 89 1.11.22Kronecker Product (kron) . . . . . . . . . . . . . . . . . . 90 1.11.23Diagonal Matrix (diag) . . . . . . . . . . . . . . . . . . . 90
  • 6. 6 1.11.24Single Value Decomposition (svd) . . . . . . . . . . . . . . 92 1.11.25Lower Upper Decomposition (lu) . . . . . . . . . . . . . . 95 1.11.26Lower Upper Composition (qr) . . . . . . . . . . . . . . . 96 1.11.27Length of Matrix (length) . . . . . . . . . . . . . . . . . . 96 1.12 Special Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 1.12.1 Bessel Function of First Kind (besselj) . . . . . . . . . . . 97 1.12.2 Bessel Function of Second Kind (bessely) . . . . . . . . . 97 1.12.3 Hyperbolic Bessel Function of First Kind (besseli) . . . . 98 1.12.4 Hyperbolic Bessel Function of Second Kind (besselk) . . . 98 1.12.5 Bessel Function as Hankel Function (besselh) . . . . . . . 98 1.12.6 Beta Function (beta) . . . . . . . . . . . . . . . . . . . . . 99 1.12.7 Gamma Function (gamma) . . . . . . . . . . . . . . . . . 99 1.12.8 Error Function (erf) . . . . . . . . . . . . . . . . . . . . . 100 1.12.9 Complementary Error Function (erfc) . . . . . . . . . . . 101 1.12.10Inverse Error Function (erfinv) . . . . . . . . . . . . . . . 102 1.12.11Legendre Function (legendre) . . . . . . . . . . . . . . . . 102 1.13 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 1.13.1 Derivative (diff) . . . . . . . . . . . . . . . . . . . . . . . 103 1.13.2 Linear ODE Solver (lsode) . . . . . . . . . . . . . . . . . . 104 1.13.3 Options For Linear ODE Solver (lsode options) . . . . . . 106 1.13.4 Differential Algebraic System Solver (dassl) . . . . . . . . 106 1.13.5 Differential Algebraic System Solver Options (dassl options)114 1.13.6 Differential Algebraic Equations (daspk) . . . . . . . . . . 115 1.13.7 Differential Algebraic Equations Options (daspk options) 115 1.13.8 Differential Algebraic System Root Solver (dasrt) . . . . . 116 1.13.9 Differential Algebraic System Root Solver Options (dasrt options)117 1.14 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 1.14.1 Quadratic Integration (quad) . . . . . . . . . . . . . . . . 117 1.14.2 Vectorized Quadratic Integration (quadv) . . . . . . . . . 118 1.14.3 Quadratic Lobatto’s Integration (quadl) . . . . . . . . . . 119 1.14.4 Quadratic Gauss-Kronrod Integration (quadgk) . . . . . . 120 1.14.5 Quadratic Clenshaw-Curtis Integration (quadcc) . . . . . 121 1.14.6 Trapezoidal Integration (trapz) . . . . . . . . . . . . . . . 121 1.14.7 Double Quadratic Integration (dblquad) . . . . . . . . . . 122 1.14.8 Triple Quadratic Integration (triplequad) . . . . . . . . . 123 1.15 Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 1.15.1 Statistical Mean (mean) . . . . . . . . . . . . . . . . . . . 124 1.15.2 Mean of Square (meansq) . . . . . . . . . . . . . . . . . . 124 1.15.3 Statistical Median (median) . . . . . . . . . . . . . . . . . 125 1.15.4 Statistical Mode (mode) . . . . . . . . . . . . . . . . . . . 125 1.15.5 Statistical Range (range) . . . . . . . . . . . . . . . . . . 126 1.15.6 Standard Deviation (std) . . . . . . . . . . . . . . . . . . 126 1.15.7 Variance (var) . . . . . . . . . . . . . . . . . . . . . . . . 127 1.15.8 Skewness of Data (skewness) . . . . . . . . . . . . . . . . 128 1.15.9 Kurtosis of Data (kurtosis) . . . . . . . . . . . . . . . . . 128
  • 7. 7 https://sites.google.com/view/arunumrao 1.15.10Data Moment (moment) . . . . . . . . . . . . . . . . . . . 129 1.15.11Percentile of Data (prctile) . . . . . . . . . . . . . . . . . 130 1.15.12Standard Score (zscore) . . . . . . . . . . . . . . . . . . . 130 1.15.13Bin Histogram (histc) . . . . . . . . . . . . . . . . . . . . 131 1.15.14Combination - Choose ‘k’ From ‘n’ (nchoosek) . . . . . . 132 1.15.15Permutations (perms) . . . . . . . . . . . . . . . . . . . . 132 1.15.16Binomial Coefficient (bincoeff) . . . . . . . . . . . . . . . 133 2 Variable 137 2.1 Assigning a Variable . . . . . . . . . . . . . . . . . . . . . . . . . 137 2.1.1 Is String A Valid Variable Name . . . . . . . . . . . . . . 137 2.1.2 Generate A Variable Name . . . . . . . . . . . . . . . . . 138 2.2 Global Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 2.2.1 Is Variable Global . . . . . . . . . . . . . . . . . . . . . . 140 2.3 Persistant Of a Variable . . . . . . . . . . . . . . . . . . . . . . . 140 2.3.1 Clear Symbol From Table (clear) . . . . . . . . . . . . . . 140 2.3.2 Show Files in Directory (what) . . . . . . . . . . . . . . . 141 2.3.3 Function Name Type (which) . . . . . . . . . . . . . . . . 141 2.3.4 Scope of Variable (who) . . . . . . . . . . . . . . . . . . . 141 2.3.5 Exit From Loop (exist) . . . . . . . . . . . . . . . . . . . 141 3 Functions 143 3.1 Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 3.1.1 Defining A Functions . . . . . . . . . . . . . . . . . . . . . 144 3.1.2 Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 147 3.1.3 Key Words . . . . . . . . . . . . . . . . . . . . . . . . . . 147 3.1.4 Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 3.1.5 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 3.1.6 Number Format . . . . . . . . . . . . . . . . . . . . . . . 148 3.1.7 Number of Input Arguments (nargin) . . . . . . . . . . . 149 3.1.8 Number of Output Arguments (nargout) . . . . . . . . . . 150 3.1.9 How to Use a Function (usage) . . . . . . . . . . . . . . . 150 3.1.10 Error (error) . . . . . . . . . . . . . . . . . . . . . . . . . 150 3.1.11 Error Number (errno) . . . . . . . . . . . . . . . . . . . . 151 3.1.12 List of Error Numbers (errno list) . . . . . . . . . . . . . 151 3.1.13 Last Error (lasterr) . . . . . . . . . . . . . . . . . . . . . . 151 3.1.14 Last Warning (lastwarn) . . . . . . . . . . . . . . . . . . . 151 3.1.15 Variable Input Arguments (varargin) . . . . . . . . . . . . 152 3.1.16 Variable Output Arguments (varargout) . . . . . . . . . . 152 3.1.17 Tilde ( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 3.1.18 Whether Argument is Out Type (isargout) . . . . . . . . 153 3.1.19 Return Command (return) . . . . . . . . . . . . . . . . . 154 3.1.20 Add Environmental Path (addpath) . . . . . . . . . . . . 154 3.1.21 Remove Environmental Path (rmpath) . . . . . . . . . . . 154 3.1.22 Lock A Function (mlock) . . . . . . . . . . . . . . . . . . 155
  • 8. 8 3.1.23 Unlock A Function (munlock) . . . . . . . . . . . . . . . . 155 3.1.24 Whether A Function is Locked (mislocked) . . . . . . . . 155 3.2 Function Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 3.2.1 Whether Argument is Function Handle (is function handle)156 3.2.2 Anonymous Functions . . . . . . . . . . . . . . . . . . . . 157 3.2.3 Sub Functions . . . . . . . . . . . . . . . . . . . . . . . . 157 3.2.4 Nested Function . . . . . . . . . . . . . . . . . . . . . . . 157 4 Conditional Function 159 4.1 Loop Condition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 4.1.1 For Condition (for) . . . . . . . . . . . . . . . . . . . . . . 159 4.1.2 While Condition (while) . . . . . . . . . . . . . . . . . . . 161 4.1.3 Do-until Condition (do ... until) . . . . . . . . . . . . . . 162 4.2 Switch Condition (switch) . . . . . . . . . . . . . . . . . . . . . . 162 4.2.1 If Condition (if) . . . . . . . . . . . . . . . . . . . . . . . 162 4.2.2 Break Command (break) . . . . . . . . . . . . . . . . . . 163 4.2.3 Continue Command (continue) . . . . . . . . . . . . . . . 163 4.2.4 Try-Catch Statement . . . . . . . . . . . . . . . . . . . . . 164 5 File Operation 167 5.1 PC Info (info) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 5.1.1 System Architecture (computer) . . . . . . . . . . . . . . 167 5.1.2 Is Windows (ispc) . . . . . . . . . . . . . . . . . . . . . . 167 5.1.3 Is Linux (isunix) . . . . . . . . . . . . . . . . . . . . . . . 167 5.1.4 Parent Machine (isdeployed) . . . . . . . . . . . . . . . . 168 5.1.5 Version (version) . . . . . . . . . . . . . . . . . . . . . . . 168 5.1.6 License (license) . . . . . . . . . . . . . . . . . . . . . . . 168 5.2 Directory Management . . . . . . . . . . . . . . . . . . . . . . . . 168 5.2.1 Make a Dir (mkdir) . . . . . . . . . . . . . . . . . . . . . 168 5.2.2 List Directory (ls) . . . . . . . . . . . . . . . . . . . . . . 169 5.2.3 Change Directory (cd) . . . . . . . . . . . . . . . . . . . . 169 5.2.4 Directory (dir) . . . . . . . . . . . . . . . . . . . . . . . . 169 5.2.5 Is Directory (isdir) . . . . . . . . . . . . . . . . . . . . . . 169 5.2.6 Remove Directory (rmdir) . . . . . . . . . . . . . . . . . . 169 5.2.7 Read Directory (readdir) . . . . . . . . . . . . . . . . . . 170 5.2.8 Temporary Directory (tempdir) . . . . . . . . . . . . . . . 170 5.2.9 Temporary Name (tempname) . . . . . . . . . . . . . . . 170 5.2.10 Attribute of File (fileattrib) . . . . . . . . . . . . . . . . . 171 5.2.11 File Marker (filemarker) . . . . . . . . . . . . . . . . . . . 171 5.2.12 File Parts (fileparts) . . . . . . . . . . . . . . . . . . . . . 171 5.2.13 File Separator (filesep) . . . . . . . . . . . . . . . . . . . . 171 5.2.14 Copy File (copyfile) . . . . . . . . . . . . . . . . . . . . . 171 5.2.15 Move File (movefile) . . . . . . . . . . . . . . . . . . . . . 172 5.2.16 Rename a File (rename) . . . . . . . . . . . . . . . . . . . 172 5.2.17 Delete a File (unlink) . . . . . . . . . . . . . . . . . . . . 172
  • 9. 9 https://sites.google.com/view/arunumrao 5.3 I/O Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 5.3.1 End of File (feof) . . . . . . . . . . . . . . . . . . . . . . . 172 5.3.2 Clear a File Stream (fclear) . . . . . . . . . . . . . . . . . 173 5.3.3 Open/Create a File (fopen) . . . . . . . . . . . . . . . . . 173 5.3.4 Close a File (fclose) . . . . . . . . . . . . . . . . . . . . . 173 5.3.5 Print Output (sprintf) . . . . . . . . . . . . . . . . . . . . 174 5.3.6 Output at Console (fprintf) . . . . . . . . . . . . . . . . . 174 5.3.7 File Error (ferror) . . . . . . . . . . . . . . . . . . . . . . 174 5.3.8 File Report (freport) . . . . . . . . . . . . . . . . . . . . . 174 5.3.9 Delete a File (delete) . . . . . . . . . . . . . . . . . . . . . 174 5.3.10 Scan a Value (scanf) . . . . . . . . . . . . . . . . . . . . . 175 5.3.11 Formatted Scanning (sscanf) . . . . . . . . . . . . . . . . 175 5.3.12 Rewind a Pointer (frewind) . . . . . . . . . . . . . . . . . 175 5.3.13 Scan to Buffer (fscanf) . . . . . . . . . . . . . . . . . . . . 175 5.3.14 Get Pointer Position (fseek) . . . . . . . . . . . . . . . . . 176 5.3.15 Pointer Position (ftell) . . . . . . . . . . . . . . . . . . . . 176 5.4 Reading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 5.4.1 Read File by Lines (fgetl) . . . . . . . . . . . . . . . . . . 177 5.4.2 Read File by Characters (fgets) . . . . . . . . . . . . . . . 177 5.4.3 Skip a Line (fskipl) . . . . . . . . . . . . . . . . . . . . . . 177 5.4.4 Read File in Binary (fread) . . . . . . . . . . . . . . . . . 178 5.4.5 CSV File Read (csvread) . . . . . . . . . . . . . . . . . . 181 5.5 Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 5.5.1 Write Into File Unformatted (fputs) . . . . . . . . . . . . 182 5.5.2 Write Into a File (fwrite) . . . . . . . . . . . . . . . . . . 182 5.5.3 CSV Write (csvwrite) . . . . . . . . . . . . . . . . . . . . 185 5.6 GUI Building . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 5.6.1 Getting Directory . . . . . . . . . . . . . . . . . . . . . . 186 5.6.2 Getting a File . . . . . . . . . . . . . . . . . . . . . . . . . 186 5.6.3 Process Status . . . . . . . . . . . . . . . . . . . . . . . . 187 5.6.4 Creating a Panel . . . . . . . . . . . . . . . . . . . . . . . 188 5.6.5 User Interactive Menu . . . . . . . . . . . . . . . . . . . . 189 5.6.6 Create a Context Menu . . . . . . . . . . . . . . . . . . . 190 5.6.7 Put String with Mouse . . . . . . . . . . . . . . . . . . . . 190 5.6.8 Create Control . . . . . . . . . . . . . . . . . . . . . . . . 191 5.6.9 Figure Tool . . . . . . . . . . . . . . . . . . . . . . . . . . 197 5.6.10 Toggle Tool . . . . . . . . . . . . . . . . . . . . . . . . . . 197 5.6.11 Dialogue Box . . . . . . . . . . . . . . . . . . . . . . . . . 198 5.6.12 Tool Bar . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 5.6.13 Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . 200 5.6.14 GUI Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 5.6.15 Handler GUI . . . . . . . . . . . . . . . . . . . . . . . . . 205 5.6.16 Wait For Time . . . . . . . . . . . . . . . . . . . . . . . . 205 5.6.17 Resume Control . . . . . . . . . . . . . . . . . . . . . . . 206
  • 10. 10 6 Binary & Digital Operation 207 6.1 Logic Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 6.1.1 AND Logic Operation (and) . . . . . . . . . . . . . . . . . 207 6.1.2 OR Logic Operation (or) . . . . . . . . . . . . . . . . . . 207 6.1.3 NOT Logic Operation (not) . . . . . . . . . . . . . . . . . 208 6.1.4 Bitwise AND Logic (bitand) . . . . . . . . . . . . . . . . . 208 6.1.5 Bitwise Comparison (bitcmp) . . . . . . . . . . . . . . . . 208 6.1.6 Get a Specific Bit (bitget) . . . . . . . . . . . . . . . . . . 209 6.1.7 Maximum Bit Length (bitmax) . . . . . . . . . . . . . . . 209 6.1.8 Bitwise OR Operation (bitor) . . . . . . . . . . . . . . . . 210 6.1.9 Set a Specific Bit (bitset) . . . . . . . . . . . . . . . . . . 210 6.1.10 Bit Shift (bitshift) . . . . . . . . . . . . . . . . . . . . . . 211 6.1.11 Bitwise Exclusive OR Operation (bitxor) . . . . . . . . . 211 6.2 Base Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 6.2.1 Decimal To Binary (dec2bin) . . . . . . . . . . . . . . . . 211 6.2.2 Binary To Decimal (bin2dec) . . . . . . . . . . . . . . . . 212 6.2.3 Decimal To Hexadecimal (dec2hex) . . . . . . . . . . . . . 212 6.2.4 Hexadecimal To Decimal (hex2dec) . . . . . . . . . . . . . 213 7 String 215 7.1 String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 7.1.1 Blank Spaces (blanks) . . . . . . . . . . . . . . . . . . . . 216 7.1.2 Code to Character (char) . . . . . . . . . . . . . . . . . . 216 7.1.3 Remove White Spaces (deblank) . . . . . . . . . . . . . . 216 7.1.4 Escape String (do string escapes) . . . . . . . . . . . . . . 216 7.1.5 Un Escape String (undo string escapes) . . . . . . . . . . 217 7.2 String Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 7.2.1 Check Character String (ischar) . . . . . . . . . . . . . . . 217 7.2.2 Whether Object is a Cell (iscell) . . . . . . . . . . . . . . 217 7.2.3 Whether Object is a Cell String (iscellstr) . . . . . . . . . 217 7.2.4 Whether Object is Alpha-Numeric (isalnum) . . . . . . . 218 7.2.5 Whether Object is Alphabatic (isalpha) . . . . . . . . . . 218 7.2.6 Whether Object is Letter (isletter) . . . . . . . . . . . . . 218 7.2.7 Whether Object is Integer (isinteger) . . . . . . . . . . . . 219 7.2.8 Whether Object is in Lower Case (islower) . . . . . . . . 219 7.2.9 Whether Object is in Upper Case (isupper) . . . . . . . . 219 7.2.10 Whether Object is Digit (isdigit) . . . . . . . . . . . . . . 219 7.2.11 Whether Object is Hexadecimal (isxdigit) . . . . . . . . . 220 7.2.12 Whether Object is Punctuation (ispunct) . . . . . . . . . 220 7.2.13 Whether Object is Space (isspace) . . . . . . . . . . . . . 220 7.2.14 Whether Object is Control Letter (iscntrl) . . . . . . . . . 220 7.2.15 Whether Object is Graph (isgraph) . . . . . . . . . . . . . 221 7.2.16 Whether Print Space (isprint) . . . . . . . . . . . . . . . . 221 7.2.17 Whether Object is ASCII (isascii) . . . . . . . . . . . . . 221
  • 11. 11 https://sites.google.com/view/arunumrao 7.2.18 Whether Object is Scalar (isscalar) . . . . . . . . . . . . . 221 7.2.19 Whether Object is Vector (isvector) . . . . . . . . . . . . 221 7.2.20 Whether Elements Are in Order (issorted) . . . . . . . . . 221 7.3 String Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 7.3.1 Part of String (substr) . . . . . . . . . . . . . . . . . . . . 222 7.3.2 String To Double (str2double) . . . . . . . . . . . . . . . 222 7.3.3 String To Function (str2func) . . . . . . . . . . . . . . . . 223 7.3.4 Function To String (func2str) . . . . . . . . . . . . . . . . 223 7.3.5 String To Number (str2num) . . . . . . . . . . . . . . . . 223 7.3.6 Joining of Strings (cstrcat) . . . . . . . . . . . . . . . . . 224 7.3.7 String Joining (strcat) . . . . . . . . . . . . . . . . . . . . 224 7.3.8 Matrix To String (mat2str) . . . . . . . . . . . . . . . . . 224 7.3.9 Number To String (num2str) . . . . . . . . . . . . . . . . 225 7.3.10 Integer To String (int2str) . . . . . . . . . . . . . . . . . . 225 7.3.11 String To Character (strchr) . . . . . . . . . . . . . . . . 225 7.3.12 String Comparison (strcmp) . . . . . . . . . . . . . . . . . 226 7.3.13 Case Insensitive Comparison (strcmpi) . . . . . . . . . . . 227 7.3.14 Search Into String (strfind) . . . . . . . . . . . . . . . . . 227 7.3.15 Cell String Joining (strjoin) . . . . . . . . . . . . . . . . . 227 7.3.16 Alignment of String (strjust) . . . . . . . . . . . . . . . . 227 7.3.17 Character Length Comparision (strncmp) . . . . . . . . . 228 7.3.18 Case Insensitive Character Length Comparision (strncmpi)228 7.3.19 Replace In String (strrep) . . . . . . . . . . . . . . . . . . 229 7.3.20 Split a String (strsplit) . . . . . . . . . . . . . . . . . . . . 229 7.3.21 String Tokenization (strtok) . . . . . . . . . . . . . . . . . 229 7.3.22 Trim String (strtrim) . . . . . . . . . . . . . . . . . . . . . 230 7.3.23 Truncate String (strtrunc) . . . . . . . . . . . . . . . . . . 230 7.3.24 Verticl Mode String Trucation (strvcat) . . . . . . . . . . 230 7.3.25 Convert to ASCII (toascii) . . . . . . . . . . . . . . . . . 231 7.3.26 Change To Lower Case (tolower) . . . . . . . . . . . . . . 231 7.3.27 Change To Upper Case (toupper) . . . . . . . . . . . . . . 231 7.3.28 Regular Expression Search (regexp) . . . . . . . . . . . . 231 7.3.29 Case Insensitive Regular Expression Search (regexpi) . . . 233 8 2D-Graphics 235 8.1 Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 8.1.1 Axis (axis) . . . . . . . . . . . . . . . . . . . . . . . . . . 235 8.2 Cartesian Plot (plot) . . . . . . . . . . . . . . . . . . . . . . . . . 236 8.2.1 Plot With Two Y Axes (plotyy) . . . . . . . . . . . . . . 241 8.2.2 Function Plot (fplot) . . . . . . . . . . . . . . . . . . . . . 242 8.2.3 Parametric Plot (ezplot) . . . . . . . . . . . . . . . . . . . 243 8.2.4 Three-D Parametric Plot (ezplot3) . . . . . . . . . . . . . 245 8.3 Bar Plot (bar) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 8.3.1 Horizontal Bar (barh) . . . . . . . . . . . . . . . . . . . . 247 8.3.2 Stair Like Bar Plot (stairs) . . . . . . . . . . . . . . . . . 247
  • 12. 12 8.3.3 Stem Plot (stem) . . . . . . . . . . . . . . . . . . . . . . . 249 8.3.4 Pareto Plot (pareto) . . . . . . . . . . . . . . . . . . . . . 249 8.4 Contour Plot (contour) . . . . . . . . . . . . . . . . . . . . . . . . 250 8.4.1 Function Contour Plot (ezcontour) . . . . . . . . . . . . . 254 8.4.2 Parametric Contour Plot (ezcontourf) . . . . . . . . . . . 255 8.4.3 Parametric Polar Plot (ezpolar) . . . . . . . . . . . . . . . 256 8.4.4 Scattered Data Plot (scatter) . . . . . . . . . . . . . . . . 258 8.5 Error Bar Plot (errorbar) . . . . . . . . . . . . . . . . . . . . . . 258 8.5.1 Semi X-axes Log Error (semilogxerr) . . . . . . . . . . . . 259 8.5.2 Semi Y-axes Log Error (semilogyerr) . . . . . . . . . . . . 259 8.5.3 Logarithmic Error (loglogerr) . . . . . . . . . . . . . . . . 259 8.6 Polar Plot (polar) . . . . . . . . . . . . . . . . . . . . . . . . . . 260 8.7 Pie Chart (pie) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 8.8 Quiver Plot (quiver) . . . . . . . . . . . . . . . . . . . . . . . . . 261 8.9 Comet Plot (comet) . . . . . . . . . . . . . . . . . . . . . . . . . 262 8.10 Area Plot (area) . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 8.11 Geometric Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 8.11.1 Cylindrical Plot (cylinder) . . . . . . . . . . . . . . . . . . 263 8.11.2 Sphere Plot (sphere) . . . . . . . . . . . . . . . . . . . . . 264 9 3D-Graphics 265 9.1 Mesh Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 9.1.1 Mesh Grid (meshgrid) . . . . . . . . . . . . . . . . . . . . 265 9.1.2 n-dimensional Grid (ngrid) . . . . . . . . . . . . . . . . . 265 9.1.3 View Point (view) . . . . . . . . . . . . . . . . . . . . . . 265 9.1.4 Mesh Plot (mesh) . . . . . . . . . . . . . . . . . . . . . . 266 9.1.5 Parametric Surface Plot (ezmesh) . . . . . . . . . . . . . . 267 9.2 Thee Dimensional Plot (plot3d) . . . . . . . . . . . . . . . . . . . 268 9.3 Surface Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 9.3.1 Parametric Surface Plot (ezsurf) . . . . . . . . . . . . . . 269 9.3.2 Surface & Contour (surfc) . . . . . . . . . . . . . . . . . . 270 9.3.3 Surface Light (surfl) . . . . . . . . . . . . . . . . . . . . . 270 9.4 Plot Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 9.4.1 Title (title) . . . . . . . . . . . . . . . . . . . . . . . . . . 271 9.4.2 X, Y & Z-axis Labels . . . . . . . . . . . . . . . . . . . . 271 9.4.3 Text (text) . . . . . . . . . . . . . . . . . . . . . . . . . . 271 9.4.4 Legends (legend) . . . . . . . . . . . . . . . . . . . . . . . 272 9.5 Graphics Object . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 9.5.1 Group of Plots . . . . . . . . . . . . . . . . . . . . . . . . 273 9.5.2 Multiple Figure Window . . . . . . . . . . . . . . . . . . . 273 9.5.3 Print or Save a Plot . . . . . . . . . . . . . . . . . . . . . 274 9.5.4 Graphics Handler . . . . . . . . . . . . . . . . . . . . . . . 275 9.5.5 Handling of Graphic’s Object . . . . . . . . . . . . . . . . 275 9.5.6 Is Handle is Figure (isfigure) . . . . . . . . . . . . . . . . 276 9.5.7 Get Current Figure Object (gcf) . . . . . . . . . . . . . . 276
  • 13. 13 https://sites.google.com/view/arunumrao 9.5.8 Get Current Axes Object (gca) . . . . . . . . . . . . . . . 278 9.5.9 Get Scalar Structure Object (get) . . . . . . . . . . . . . 280 10 Network 281 10.1 File Transfer Protocol (FTP) . . . . . . . . . . . . . . . . . . . . 281 10.1.1 Open File Transfer Protocol (ftp) . . . . . . . . . . . . . . 281 10.1.2 Close File Transfer Protocol (close) . . . . . . . . . . . . . 281 10.1.3 Read/Download a File (mget) . . . . . . . . . . . . . . . . 282 10.1.4 Write/Upload a File (mput) . . . . . . . . . . . . . . . . . 282 10.1.5 Search Directory (dir) . . . . . . . . . . . . . . . . . . . . 282 10.1.6 Delete A File (delete) . . . . . . . . . . . . . . . . . . . . 282 10.1.7 Rename A File (rename) . . . . . . . . . . . . . . . . . . . 282 10.1.8 Crete A Directory (mkdir) . . . . . . . . . . . . . . . . . . 283 10.1.9 Remove A Directory (rmdir) . . . . . . . . . . . . . . . . 283 11 Others 285 11.1 Hardware Access . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 11.1.1 CPU Bell (beep) . . . . . . . . . . . . . . . . . . . . . . . 285 11.1.2 Bell if Error (beep on error) . . . . . . . . . . . . . . . . . 285 11.1.3 Show All Variables (all) . . . . . . . . . . . . . . . . . . . 285 11.1.4 Date & Time (clock) . . . . . . . . . . . . . . . . . . . . . 285 11.1.5 Date In Number Format (datenum) . . . . . . . . . . . . 285 11.1.6 Date As String (datestr) . . . . . . . . . . . . . . . . . . . 286 11.1.7 Ticks As Date In Axes (datetick) . . . . . . . . . . . . . . 286 11.1.8 Date As Vector (datevec) . . . . . . . . . . . . . . . . . . 286 11.2 Octave Misc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 11.2.1 Close Octave Connection (exit) . . . . . . . . . . . . . . . 287 11.2.2 Quit The Session (quit) . . . . . . . . . . . . . . . . . . . 287 11.2.3 Display In Console (disp) . . . . . . . . . . . . . . . . . . 287 11.2.4 Compress Into GZIP (gzip) . . . . . . . . . . . . . . . . . 287 11.2.5 Decompress From GZIP (gunzip) . . . . . . . . . . . . . . 288 11.2.6 Get Octave Environmental Path (getenv) . . . . . . . . . 288 11.2.7 Add Octave Environmental Path (putenv) . . . . . . . . . 288 11.2.8 Set A Value to Environmental Variable (setenv) . . . . . 288 11.3 Writing Mex Macros . . . . . . . . . . . . . . . . . . . . . . . . . 288 11.3.1 Core Functions . . . . . . . . . . . . . . . . . . . . . . . . 288 mex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 mexext . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 11.3.2 Matrix & Array . . . . . . . . . . . . . . . . . . . . . . . . 291 11.3.3 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 11.3.4 Cell Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 11.3.5 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 11.3.6 Calling Other Functions . . . . . . . . . . . . . . . . . . . 297 11.4 Standalone Programs . . . . . . . . . . . . . . . . . . . . . . . . . 297
  • 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
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26. 1 3.5 × 10−12 35 × 10−12 1
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33. Arithmetic operators are explained below:
  • 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.
  • 38.
  • 39.
  • 40.
  • 41. 1 2 3 4 − λ 1 0 0 1
  • 42.
  • 43.
  • 44.
  • 46.
  • 47.
  • 48.
  • 49. 1 − λ 2 3 4 − λ
  • 50.
  • 51.
  • 52.
  • 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 =
  • 79.
  • 80.
  • 81. p (x2 − x1)2 + (y2 − y1)2
  • 82.
  • 83.
  • 84. b =
  • 85.
  • 86.
  • 87. p (x3 − x2)2 + (y3 − y2)2
  • 88.
  • 89.
  • 90. c =
  • 91.
  • 92.
  • 93. p (x3 − x1)2 + (y3 − y1)2
  • 94.
  • 95.
  • 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