Vous êtes sur la page 1sur 20

Pengenalan MATLAB

Pengenalan MATLAB I-2

Basic Matlab
Matrix definition
a=[1,2 ; 3,4] => a= 1 2
34

Vector is a matrix with one column or one row.

a=[1;2;3;4;5 …;…;… ] one column


b=[1,2,3,4,5,6 …,…,..] one row
Pengenalan MATLAB I-3

Basic Matlab
Example Example
a=[1,2,3,4,5]; a=[1;2;3;4;5];
b=[a;a;a] b=[a,a,a,a]

b= 12345 b= 1111
12345 2222
12345 3333
4444
Example (transpose) 5555
a=[1;2;3;4;5];
b=a’
b= 1 2 3 4 5
Pengenalan MATLAB I-4

Basic Matlab

Strings Strings Strings


a=[‘a’,’b’,’c’] a=[‘a’,’b’,’c’] a=[‘a’,’b’,’c’]
a= abc a= abc b=[‘a’,’c’,’c’]
b=[a;a;a] b=[a,a,a] c=(a==b);
b= abc b= abcabcabc c= 1 0 1
abc
abc
Pengenalan MATLAB I-5

Basic Matlab

1 2  5 12 
A A. * B   
3 4   21 32 
5 6 1 4 
B  A. ^ 2   
7 8  9 16 
6 8  sin(1) sin(2) 
A B    sin( A)  
10 12  
sin(3) sin(4) 
19 22 
A* B   
 43 50 
Pengenalan MATLAB I-6

Basic Matlab

A=zeros(a1, a2, a3,… an);


A is an n dimensional matrix of zeros.
A=ones(a1, a2, a3,… an);
A is an n dimensional matrix of ones.

size(A) return the size of A at each diminution.

size(A,Dim) return the size of A at the diminution Dim.


Pengenalan MATLAB I-7

Basic Matlab

A(m,n) returns the value of the matrix in row-m


and column-n.

b=A(1:end,1) : b will be equal to column 1 of A

b=A(5:10,5:10) : b will a-6x6 matrix containing all


values of A from rows 5-10 and columns 5-10.
Pengenalan MATLAB I-8

Basic Matlab
Functions in Matlab

function [output variables]=function_name (input variables)

Input and output variables, can be of any type.


Pengenalan MATLAB I-9

Basic Matlab
Functions in Matlab
function [out_1,out_2,out_3] = Function_Dec (in_1,in_2,in_3)

out_1=in_1+in_2+in_3;
out_2=[ 'hello' ; 'world' ];
out_3=[1,2,3,4,5];

return;
Pengenalan MATLAB I - 10

Basic Matlab
Functions in Matlab
input: input:
» [a,b,c]=Function_dec(5,3,2) » [a,b,c]=Function_dec([1,2,3],[6,5,4],[3,4,5])

output: output:
a = 10 a = 10 11 12
b = hello b= hello
world world

c= 1 2 3 4 5 c=1 2 3 4 5
Pengenalan MATLAB I - 11

Basic Matlab
Bit-wise operations
Variables must be integers

BITAND (a,b) Bit-wise AND.


BITOR (a,b) Bit-wise OR.
BITXOR (a,b) Bit-wise XOR.
BITGET (a,bit-num) Get bit.
BITSET (a,bit-num,1/0) Set bit.
BITSHIFT (a,+/- shift_size) Bit-wise shift.
Pengenalan MATLAB I - 12

Basic Matlab
conditions
If ( Boolean expression)
.
end;

Boolean expression
== : Is Equal
~=: Not Equal
> : Is grater then
< : Is less Then
>=: Is grater then or equal to
<=: Is less then or equal to
Pengenalan MATLAB I - 13

Basic Matlab
conditions
switch switch_expr
case case_expr,
statement, ..., statement
case {case_expr1, case_expr2, case_expr3,...}
statement, ..., statement
...
otherwise,
statement, ..., statement
end
Pengenalan MATLAB I - 14

Basic Matlab
Loops
for j=start:step:end while Boolean expression,
. .
end; end;
example: example:
for j=-1:0.2:3 while a>b,
. .
end; end;

break - Terminate execution of WHILE or FOR loop.


break terminates the execution of FOR and WHILE loops.
In nested loops, BREAK exits from the innermost loop only.
Pengenalan MATLAB I - 15

Basic Matlab
Drawing
plot(X,Y) plots vector Y versus vector X
y yellow . point - solid
m magenta o circle : dotted
c cyan x x-mark -. dashdot
r red + plus -- dashed
g green * star
b blue s square
w white d diamond
k black v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
Pengenalan MATLAB I - 16

Basic Matlab
Drawing
Drawing a circle Drawing a doted circle.
•t=0:0.1:2*pi; •t=0:0.1:2*pi;
•x=10*cos(t); •x=10*cos(t);
•y=10*sin(t); •y=10*sin(t);
•plot (x,y); •plot (x,y,’.’);
Pengenalan MATLAB I - 17

Basic Matlab
Drawing
ezplot(‘x^2+y^2=1’); ezplot(‘x^2/5+y^2/20=1’);
Pengenalan MATLAB I - 18

Basic Matlab
Drawing

a=0:0.2:2*pi;
b=ones(1,length(a));
c=sin(a'*b);

figure;
subplot(1,2,1);
surf(c);
Pengenalan MATLAB I - 19

Basic Matlab
Drawing

figure;
t=0:0.1:2*pi+0.1;
b=ones(1,length(t));
z=b'*(1:1:length(t));
x=(10*sin(z)+15).*sin(t'*b);
y=(10*sin(z)+15).*cos(t'*b);
surf(x,y,z);
Pengenalan MATLAB I - 20

Basic Matlab
Some useful commands
figure -open new figure.
drawnow -draw to screen immediately.
hold on -draw the next draw to the same figure and axes.
’ -Transpose
% - remark

help command- return the help information on the command.


lookfor Word - return a list of all commands that have
the desired word in their help.

Vous aimerez peut-être aussi