Introduction To MATLAB: Kathmandu University
Introduction To MATLAB: Kathmandu University
Kathmandu University
Mathematics Group
1 Introduction
MATLAB is an interactive matrix based system for scientific and en-
gineering numerical computation and visualization.
It is a powerful tool that might solve complex numerical problems in
a fraction of time required by other programming language such as
FORTRAN or C.
It has a very extensive library of predefined programs or functions de-
signed to help engineers and scientists to solve their problems in a
faster and less painful way, having over 40 different toolboxes for dif-
ferent subject of study. Different toolboxes contain different functions.
Every toolbox is area specific and may not be useful to all researchers.
For example:
Bioinformatics Toolbox
Curve fitting Toolbox
Communication Toolbox
Optimization Toolbox
Partial Differential Equation Toolbox and so on.
The name MATLAB stands for MATrix LABoratory, because every in-
put in MATLAB has to write in Matrix. Column or row matrix, which
consists of a collection of data values organized into column or row,
known as an array is the fundamental unit of data in MATLAB pro-
gram. So, an array is the column vector or row vector in any MATLAB
program.
1
2
Column Vector A = .. ; Row Vector B = 1 2 . . . n 1n ;
.
n n1
1 2 3 4
3 4 Order Matrix C = 5 6 7 8
9 10 11 12 34
1
and
1 1 Order Matrix = 12
The 1 1 order matrix is termed as scalar.
The size of an array is specified as the number of rows and the number
of columns in the array with the number of rows mentioned first. In-
dividual elements in an array are addressed by particular element. For
example, in the above arrays C(2,3) is 7 but C(4) is 2, because in this
notation, by default, MATLAB addresses column-wise.
2 Accessing MATLAB
Start:
Access MATLAB from the menu or click the MATLAB icon on the
desktop.
Quit:
3 MATLAB Windows
The Command Window
The Workspace
2
4 Data Type in MATLAB
Every programming language has its specific way of recognizing different
data types. MATLAB as a computing language is no different from them.
MATLAB also has its own way of recognizing different data types. However,
in MATLAB, data handling is much easier than other languages like C, JAVA,
FORTARN etc.
3
names.
[v] The variable name should not clash with the name of an already
existing function.
Keywords are the reserved words used in MATLAB for writing a pro-
gram. Avoid using a keyword for a variable or function name. MAT-
LAB keywords are: break case catch continue else elseif end
for function global if otherwise persistent return switch try
while
>> 5
ans =
5
Vectors:
>> A = [1 2 3 4]
A =
1 2 3 4
OR
>> A = [1,2,3,4]
A =
1 2 3 4
>> B = [1 2 3 4]
B =
1
2
3
4
>> B = [1;2;3;4]
B =
1
2
3
4
4
Matrix
>> C = [1,2,3;4,5,6;7,8,9]
C =
1 2 3
4 5 6
7 8 9
OR
>> C = [1 2 3;4 5 6;7 8 9]
C =
1 2 3
4 5 6
7 8 9
OR
>> C = [1,2,3
4,5,6 7,8,9] C =
1 2 3
4 5 6
7 8 9
OR
>> C = [1 2 3
4 5 6 7 8 9] C =
1 2 3
4 5 6
7 8 9
The following input creates a matrix, in particular, a row vector with incre-
ment of 1.
>> A = [1:8]
A =
1 2 3 4 5 6 7 8
>> A = [-5:2:5]
A =
-5 -3 -1 1 3 5
5
logspace(a, b, n) generates a logarithmically spaced vector of length n
from 10a to 10b .
Ellipses
If a statement is too long to type on a single line, it may be continued on
successive lines by typing an ellipsis (. . .) at the end of the first line, and
then continuing on the next line. For example, the following two statements
are identical.
6
Meanings of Comma(,), Colon(:) and Semicolon(;):
Comman (,) or space bar is used to write the elements of a row.
Colon (:) is used for increment in a row vector.
Semicolon (;) is used for new row in a matrix. The other important
significant of semicolon(;) is not to display the answer in command
window but retains the input in computer memory. This we can do as
Arithmetic Operations:
Operations Meaning
+ addition
subtraction
multiplication
\ left division
/ right division
power
Note the distinction between left division (\) and right division (/). This you
can distinct with 6\2 = 0.3333 and 6/2 = 3. These operations for addition,
subtraction and multiplication are explicitly used in compatiable matrices for
matrix addition, subtraction and multiplication respectively. Note that if A
is a non singular square matrix, then A2 is same as AA. If b is compatiable
column and respectively row vector, then
Built-in-functions:
If arbitrary built in functions for scalars, e.g. exp( ), sqrt( ), sin( ) are
applied on matrix, they are evaluated componentwise.
A few built in functions in MATLAB deal with the whole matrix and
not only with its components, e.g. expm( ), sqrtm( ), logm( ).
7
The name of all built-in MATLAB function starts with a small letter,
and the arguments of the functions are stated in round bracket (.). For
example,
>> real(-3+2*i)
ans =
7
>> imag(-3+2*j)
ans =
2
>> sqrt(3)
ans =
1.7321
By default, MATLAB produces 4 digits after decimal places. If we
want the more digits after the decimal places, then do as
>> format long
>> sqrt(3)
ans =
1.73205080756888
There are other format commands too.
Trigonometric functions:
8
Vector functions:
Matrix functions:
9
>> diag(B,1)
ans =
1
1
0
0
>> diag(B,-2)
ans =
1 0 0
>> d = [2 4 6 8];
>> d1 = [-3 -3 -3];
>> d2 = [-1 -1];
>> D = diag(d) + diag(d1,1) + diag(d2,-2)
D =
2 -3 0 0
0 4 -3 0
-1 0 6 -3
0 -1 0 8
A(m:n,p): the m until the nth row of the pth column of the matrix
A.
10
7 Array, Relational and Logical Operations
Operations Meaning
. element-by-element multiplication
.\ element-by-element left division
./ element-by-element right division
. element-by-element exponentiation
>> [4 3 2]>[1 5 0]
ans =
1 0 1
>> 5==3
ans =
0
11
Logical Operations: These are the following logical operators in MATLAB.
Operations Meaning
& logical AND
&& logical AND with shorcut evaluation
| logical OR
|| logical OR with shortcut evaluation
Xor exclusive OR
logical complement(not)
>> (2==2)&(2>1)
ans =
1
>> (2==3)&(2>1)
ans =
0
>> (2==3)|(2>1)
ans =
1
12