309
Appendix A: Introduction to MATLAB
®
A.1 Introduction
MATLAB is a software package of The MathWorks Inc., for technical comput-
ing that does both computing and visualization with ease. It has a number of
built-in functions that can be used by an individuals application. The acro-
nym MATLAB stands for MATrix LABoratory. Matrices are the basic build-
ing blocks of MATLAB. Though MATLAB is primarily used for numerical
computations, it also supports symbolic computations. The main advantage
of MATLAB is the ease with which one can translate the idea into an appli-
cation. MATLAB runs on almost all computer platforms, whether Microsoft
Windows, Apple Macintosh or Unix. On Microsoft Windows, MATLAB can
be started by double clicking the MATLAB shortcut icon. See Figure A.1 for
a typical desktop of MATLAB.
Observe that the desktop has four windows: current folder, command
window, workspace, and command history. The command prompt is shown
by >>. All commands are to be typed here. The command history windows
keep a record of the previously typed commands across multiple sessions.
The previously typed command in this window can be double-clicked so
that it can be executed again. All les listed in the left window correspond
to the current folder directory. The le can be opened for editing by simply
double-clicking on it. The type and size of the variables are shown in the
workspace window (empty in this gure). There is a provision to select the
variables and plot them.
A.2 Matrices and Arrays
Type the matrix A in the command prompt
>> A = [1 2 3; 4 -1 -2; 5 6 7]
Then press enter. The following output is displayed in the command
window.
310 Appendix A
A =
1 2 3
4 -1 -2
5 6 7
Observe that a declaration of dimensions of A is not required. Let us learn
few more commands.
>> sum(A)
ans =
10 7 8
The sum function adds the elements of each column. To get sum of each
row
>> sum(A')
ans =
6 1 18
where A' is the transpose of the matrix A. The diagonal elements can be
obtained using the diag function.
>> diag(A)
ans =
1
-1
7
Simultaneous use of functions in a single command is also permissible.
For example,
>> sum(diag(A))
ans =
7
Suppose we want to assign the element –2 in matrix A to a variable x. The
element –2 is in second row and third column of A. Then
>> x = A(2,3)
x =
-2
Consider the colon operator
>> x = 1:2:10
The output is a row vector containing numbers from 1 to 10 in steps of 2:
ans =
1 3 5 7 9
311Appendix A
Figure A.1
Command window.
312 Appendix A
To get all rows (or columns) of a matrix, a colon operator can be used. For
example, to get second column of A,
>> A(:,2)
ans =
2
-1
6
To get third row of A
>> A(3,:)
ans =
5 6 7
A.3 Expressions
MATLAB does not require variable type declarations. For example,
drag_coefficient = 0.6
The variables are case sensitive; that is, the variable LIFT is different from
lift.
MATLAB uses conventional decimal notation. Scientic notation uses the
letter e to specify a power-of-ten scale factor. Imaginary numbers use either
i or j as a sufx. Some examples are
6 -999 0.0005 109.1237 1.60210e-20 9.123e23 7i
-6.28j 4e6i
MATLAB uses the following operators and the precedence follows stan-
dard mathematical rules.
+ Addition
Subtraction
* Multiplication
/ Division
\ Left division
^ Power
() Specify evaluation order
313Appendix A
The relational operators >, <, >=, <= consider only the real part for the pur-
pose of comparison while the operator = = considers both real and imagi-
nary parts.
Some elementary functions in MATLAB are
Trigonometric
sin Sine
sind Sine of argument in degrees
sinh Hyperbolic sine
asin Inverse sine
asind Inverse sine, result in degrees
asinh Inverse hyperbolic sine
cos Cosine
cosd Cosine of argument in degrees
cosh Hyperbolic cosine
acos Inverse cosine
acosd Inverse cosine, result in degrees
acosh Inverse hyperbolic cosine
tan Tangent
tand Tangent of argument in degrees
tanh Hyperbolic tangent
atan Inverse tangent
atan2 Four-quadrant inverse tangent
atanh Inverse hyperbolic tangent
sec Secant
secd Secant of argument in degrees
sech Hyperbolic secant
asec Inverse secant
asecd Inverse secant, result in degrees
asech Inverse hyperbolic secant
csc Cosecant
cscd Cosecant of argument in degrees
csch Hyperbolic cosecant
acsc Inverse cosecant
acscd Inverse cosecant, result in degrees
acsch Inverse hyperbolic cosecant
cot Cotangent
cotd Cotangent of argument in degrees
coth Hyperbolic cotangent
acot Inverse cotangent

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.