314 Appendix A
acotd Inverse cotangent, result in degrees
acoth Inverse hyperbolic cotangent
hypot Square root of sum of squares
Exponential
exp Exponential
expm1 Compute exp(x) – 1 accurately
log Natural logarithm
log1p Compute log(1 + x) accurately
log10 Common (base 10) logarithm
log2 Base 2 logarithm and dissect oating point number
pow2 Base 2 power and scale oating point number
sqrt Square root
nthroot Real nth root of real numbers
Complex
abs Absolute value
angle Phase angle
complex Construct complex data from real and imaginary parts
conj Complex conjugate
imag Complex imaginary part
real Complex real part
isreal True for real array
Rounding and Remainder
x Round toward zero
oor Round toward minus innity
ceil Round toward plus innity
round Round toward nearest integer
mod Modulus (signed remainder after division)
rem Remainder after division
sign Signum
MATLAB also provides values of useful constants.
pi 3.14159265
i Imaginary unit
j Same as i
eps Floating-point relative precision
realmin Smallest oating-point number
realmax Largest oating-point number
Inf Innity
NaN Not-a-number
315Appendix A
A.4 Matrix Operations
Examples of zeros, ones, and rand functions are given below.
>> Y = zeros(3,2)
Y =
0 0
0 0
0 0
>> X = ones(2,3)
X =
1 1 1
1 1 1
>> Z = rand(2)
Z =
0.1656 0.2630
0.6020 0.6541
The rand function generates a uniformly generated random number
between 0 and 1.
The matrix A can be saved in the same directory, for a later use, by the
command:
>> save –ascii aa X
Sometimes it is necessary to clear all variables and functions from the
command window. This is done with the command
>> clear all
Now if A is punched in the command prompt it results in an error.
>> A
??? Undened function or variable ‘A.
To get back the saved value of matrix A, use the load command
>> A=load('aa')
A =
1 2 3
4 -1 -2
5 6 7
To know about a function name, use help from the menu or simply type
help functionnane in the command. For example,
>> help clc
316 Appendix A
CLC Clear command window.
CLC clears the command window and homes the cursor.
If one is not able to recollect the function name, use the lookfor command.
For example, to get the name of absolute function:
>> lookfor absolute
abs Absolute value
genelowvallter Filters genes with low absolute expression levels
imabsdiff Absolute difference of two images
meanabs Mean of absolute elements of a matrix or matrices
sumabs Sum of absolute elements of a matrix or matrices
mae Mean absolute error performance function
sae Sum absolute error performance function
dmae Mean absolute error performance derivative function
circlepick Pick bad triangles using an absolute tolerance
mad Mean/median absolute deviation
The concatenation of the matrices is shown with the following example.
>> A = [1 2 3; 4 -1 -2; 5 6 7]
A =
1 2 3
4 -1 -2
5 6 7
>> B = [8;9;10]
B =
8
9
10
>> Z = [A B]
Z =
1 2 3 8
4 -1 -2 9
5 6 7 10
Suppose we want to delete the second column of the Z matrix. This can be
done by
>> Z(:,2)=[]
Z =
1 3 8
4 -2 9
5 7 10
317Appendix A
The inverse of the square matrix can be computed by
>> inv(Z)
ans =
-0.3517 0.1102 0.1822
0.0212 -0.1271 0.0975
0.1610 0.0339 -0.0593
The eigenvalues of the square matrix are computed by
>> eig(Z)
ans =
17.1878
-2.3534
-5.8344
Some of the array operators are
+ Addition
Subtraction
.* Element-by-element multiplication
./ Element-by-element division
.\ Element-by-element left division
.^ Element-by-element power
.' Unconjugated array transpose
For example,
>> U = [1 2 3]
U =
1 2 3
>> V = [-1 -2 -3]
V =
-1 -2 -3
>> U.*V
ans =
-1 -4 -9
The display of numbers is controlled by the format command. Typical
commands are
format short
format long
The previous command can be brought back into the command prompt
using the key .
318 Appendix A
A.5 Plotting
If x and y are two vectors then plot(x, y) makes a graph. For example, con-
sider the following example.
>> x = 0:0.01:2*pi;
>> y = cos(x);
>> y1 = sin(x);
>> plot(x,y,'--',x,y1,'r:')
>> xlabel('0 \leq x \leq 2\pi')
>> ylabel('Sine and Cosine functions')
>> legend('cos(x)', 'sin(x)')
>> title('Multiple Plots')
Figure A.2 is displayed on the desktop and can be edited using the gure
menu.
The following example demonstrates how to make a contour plot (Figure
A.3).
>> [X,Y] = meshgrid(-2:.01:2,-2:.01:3);
>> Z = X.^2+Y.^2;
>> v=[1;2;3;4;5;6;7;8;9;10;11;12];
>> [c,h] = contour(X,Y,Z,v); clabel(c,h);
Figure A.2
Multiple plots.

Get Optimization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.