Escolar Documentos
Profissional Documentos
Cultura Documentos
0
1
1
1
copyright@2009-2010 PCCOE,
Mrs Preethi S.J
0
1
MATLAB
1. Matlab is a product of
MATHWORKS
2. It is software package designed for
scientific and engineering
computation
3. Writing programs in Matlab is
easier compared to other
programming languages, because
u need not worry about
copyright@2009-2010 PCCOE,
Mrs Preethi S.J
Features of Matlab
Matlab has
copyright@2009-2010 PCCOE,
Mrs Preethi S.J
copyright@2009-2010 PCCOE,
Mrs Preethi S.J
Matlab Matrices
A matrix with only one row is called a row
vector. A row vector can be created in
Matlab as follows (note the commas):
rowvec = [12 , 14 , 63]
rowvec =
12 14 63
colvec = [13 ; 45 ; -2]
colvec =
13
45
-2
copyright@2009-2010 PCCOE,
Mrs Preethi S.J
matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]
matrix =
1
4
7
2
5
8
3
6
9
>> matrix(2,:)
ans =
4
>> matrix(:,3)
ans =
3
6
9
copyright@2009-2010 PCCOE,
Mrs Preethi S.J
10
Matlab Matrices
A column vector can be extracted from a matrix.
As an example we create a matrix below:
matrix=[1,2,3;4,5,6;7,8,9]
matrix =
123
456
789
Here we extract column 2 of the matrix and make
a column vector:
col_two=matrix( : , 2)
col_two =
2
5
8
copyright@2009-2010 PCCOE,
Mrs Preethi S.J
11
>> y=eye(3)
y=
1
0
0
0
1
0
0
0
1
>> z=zeros(4,3)
z=
0
0
0
0
0
0
0
0
0
0
0
0
copyright@2009-2010 PCCOE,
Mrs Preethi S.J
12
>> w=ones(3,5)
w=
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
>>x = rand(1,3)
x=
0.9501 0.2311 0.6068
Operators (relational,
logical)
== equal
~= not equal
< less than
<= less than or equal
> greater than
>= greater than or
equal
& AND
| OR
~ NOT
pi 3.14159265
j imaginary unit,
i same as j
[]
()
concatenation
subscription
x = [ zeros(1,3) ones(1,2) ]
x =
0 0 0 1 1
x = [ 1 3 5 7 9]
x =
1 3 5 7 9
y = x(2)
y =
3
y = x(2:4)
y =
3 5 7
Graph Functions
(summary)
plot
linear plot
stem discrete plot
grid
add grid lines
xlabel add X-axis label
ylabel add Y-axis label
title
add graph title
subplot
divide figure window
figure create new figure window
COMPLEX NUMBERS
>> x=1-2i
x=
1.0000 - 2.0000i
>> abs(x)
ans =
2.2361
>> angle(x)
ans =
-1.1071
>> real(x)
ans =
1
>> imag(x)
ans =
-2
switch
Switch among several cases, based on expression
Syntax
switch switch_expr
case case_expr
otherwise
input
Request user input
Syntax
user_entry = input('prompt')
user_entry = input('prompt', 's')
disp
Display text or array
Syntax
disp(X)
1. Structures
2. Functions
3. Strings
4. Files
0
1
1
0
1
0
1
copyright@2009-2010 PCCOE,
Mrs Preethi S.J
25