Você está na página 1de 25

1

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

Declaration of variables, types, sizes and

Features of Matlab

1. Every numeric data is stored in form


of a matrix.
2. Has graphics
3. Precise numerical calculations
4. User friendly
5. Large set of inbuilt functions and
toolbox
6. Functions are written in optimized C,
with inner loops
coded in assembly
copyright@2009-2010 PCCOE,
Mrs Preethi S.J

Matlab has

1. Command window- where we type


commands, execute programs and
launch other windows
2. Editor window- Here we write and edit
matlab programs
3. Figure Window- To visualize plots and
images
4. Help Window- It provides help
information and documentation
copyright@2009-2010 PCCOE,
Mrs Preethi S.J

5. Command history window- Displays


the commands typed in the previous
Matlab sessions.
6. Current directory window- Shows files
and directory available in current
working directory of matlab
7. Workspace Window- This is a window
where information about variables
and their data is stored. It allows us
to directly modify data for a variable
copyright@2009-2010 PCCOE,
Mrs Preethi S.J

1. EXIT,QUIT=> ends matlab session


2. To start with simple mathematical
operations (like calculator)
Variables
1.Have not to be previously declared
2.Variable names can contain up to
31Characters
3.Variable names must start with a letter
followed by letters, digits, and
underscores.
4.Variable names are case sensitive
copyright@2009-2010 PCCOE,
Mrs Preethi S.J

Matlab Special Variables


1.ans : Default variable name for results
2.pi : Value of
3.eps : Smallest incremental number
4.Inf: Infinity
5.NaN: Not a number e.g. 0/0
6.realmin :The smallest usable positive real
number
7.realmax :The largest usable positive real
number

copyright@2009-2010 PCCOE,
Mrs Preethi S.J

Matlab Assignment &


Operators
Assignment = a = b (assign b to a)
Addition + a + b
Subtraction - a -b
Multiplication * or.* a*b or a.*b
Division / or ./ a/b or a./b
Power ^ or .^ a^b or a.^b

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

statement, ..., statement

case {case_expr1, case_expr2, case_expr3, ...}

statement, ..., statement

otherwise

statement, ..., statement


End
Terminate execution of for or while loop
break

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

Você também pode gostar