Académique Documents
Professionnel Documents
Culture Documents
Basic Matlab
Matrix definition
a=[1,2 ; 3,4] => a= 1 2
34
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
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
Basic Matlab
Basic Matlab
Functions in Matlab
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
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;
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