32
BP0 l$MATLABR

AM2 MATLAB

Embed Size (px)

Citation preview

Page 1: AM2 MATLAB

B�P�0

l$MATLABR�

Page 2: AM2 MATLAB

Matalbcz• F�[Zb => �I�X �{�

• �^i/ => 5P�I�Xu]i/

• vi/ => ��@i�i.e. integer -> double�

• �sQ�yMT

• A1S�~rJ

Page 3: AM2 MATLAB

.��4�*�W�pA1S�~rJ�t*�toolbox�

A1S�~rJ�t*�toolbox�

A1S�~rJ�t*�toolbox�

Page 4: AM2 MATLAB

Matlab z&

Page 5: AM2 MATLAB

Matlab z&

current folder

variablecommand line

Page 6: AM2 MATLAB

command• lookfor <keyword>

• help <command>

• a=1

• a=2;

• whos a

• clear 0

• %

Page 7: AM2 MATLAB

simple calculator

• a = 1

• b = a+1

• c = a + b

• d = a*b^c - c/b

Page 8: AM2 MATLAB

predefined variables

• pi %=> 3.14……

• i or j %=> 0.0000+1.0000i

• %Tips: use 1i

Page 9: AM2 MATLAB

variable declaration• s => undefined

• scalar: s=1

• vector:

• v1=[1,2,3]

• v1=1:3

• v2=[1;2;3]

• v2=[1,2,3]'

• matrix

• m1=[1,2;3,4]

1 2

3 4

row 1

row 2

col 1 col 2

Page 10: AM2 MATLAB

vector indexing

• v=1:2:10

• v(5)

• v(1:3)

• v(:)

• v(end)

• v(1:end~=3)

• Try it:

• Get the 2nd element from v.

• Get 3rd to end elements.

• Put 1st, 3rd, 5th element as a new array named b.

Page 11: AM2 MATLAB

matrix indexing• matrix

• m=[1:3;4:6;7:9]

1 2 3

4 5 6

7 8 9

• matrix

• m=[1:3;4:6;7:9]

• m(1,:) % get 1st row

• m(:) % get all element

• m(1:2,3) % submatrix

• m(8) ???

• linear indexing

• m([1,4,5,6])

Page 12: AM2 MATLAB

try it

• m = magic(5)

• Get first column from m.

• Get 3~5 rows from m.

• Get submatrix which formed by the intersection of 1~3 rows and 2~4 columns of m and store in a new matrix m1(3x3 matrix).

• Get a column vector which formed by all the odd linear index elements in m.

Page 13: AM2 MATLAB

operator

• matrix operatior

• *

• /

• ^

• …..

• element-wise operator

• .*

• ./

• .^

• …..

Page 14: AM2 MATLAB

try it

• v1=1:3

• v2=v1'

• v3=v1*2

• v3*v2

• v2*v3

• v1.*v3

• v1.^v3

• m1=[1:3;4:6]

• m2=m1'

• m3=m1*2

• m3*m2

• m2*m3

• m1.*m3

• 10.^m1

Page 15: AM2 MATLAB

matlab function

• v=1:20;

• sin(v)

• cos(v), tan(v)….

• exp(v), log(v)…

• diag, eig( )

• sum( )

• check how to use: help <function>

Page 16: AM2 MATLAB

try it• m = magic(100);

• Get following arrays:

• 1st row

• 25th column

• diagonal

• Get the summation of above arrays. Are they the same?

Page 17: AM2 MATLAB

example 1: eigenvalue problem

• A=magic(5)

• [vectors, values] = eig(A)

• isequal(A*vectors,vectors*values) %=> ?

• A*vectors-vectors*values %=> ?

Page 18: AM2 MATLAB

example 2: make a plot

• linspace(<start>,<end>,<sep>)

• x = linspace(0,2*pi,100);

• y = sin(x);

• figure;

• plot(x,y);

• y2 = cos(x);

• plot(x,y,x,y2);

Page 19: AM2 MATLAB
Page 20: AM2 MATLAB

BUT !!!

Page 21: AM2 MATLAB

���

� ���������

����

����

����� �������

����

� ���

Page 22: AM2 MATLAB

plot cont.

• plot(x,y,x,y2,'LineWidth',2);

• title('AM2 plot','FontWeight','bold','FontSize',20);

• axis tight;

• xlabel('x','FontWeight','bold','FontSize',20);

• ylabel('y','FontWeight','bold','FontSize',20);

• set(gca,'XTick',[0 pi 2*pi],'XTickLabel',{'0','\pi','2\pi'});

• set(gca,'FontWeight','bold','fontsize',20);

Page 23: AM2 MATLAB
Page 24: AM2 MATLAB

compare two scalar• ==

• >

• <

• >=

• <=

• true: return 1

• otherwise: return 0

• Try it

• 1==2

• 3>=2

• 1>=2

• 3<3

Page 25: AM2 MATLAB

if…else

• grade = 65;

• if grade >= 60

• disp('Pass~');

• else

• disp('Fail!! QQ~');

• end

Page 26: AM2 MATLAB

for loop• grades = [60,50,40,70];

• names = {'John','Peter','Chen','Lucky'};

• for i=1:numel(grades)

• if grades(i) >= 60

• disp([names(i) ': Pass']);

• else

• disp([names(i) ': Fail']);

• end

• end

Page 27: AM2 MATLAB

try it

• m = magic(100);

• Get row index and column index (i,j) for all the elements which grater than 3.

Page 28: AM2 MATLAB

some tips for speed up• �����Gu]����y��

• zeros(n,m)

• C;Q��,2for loop�

• tic;toc; %timer

• JIT compiler

• e;O�>d�P��sQ��

• q�logical array6�?o���find�

Page 29: AM2 MATLAB

N = 1e7; tic w = zeros(1, N); for i = 1:N w(i) = i*5; end fprintf('For loop: %.4fs\n', toc);%=> 0.053s tic y = (1:N)*5; fprintf('Vectorized: %.4fs\n', toc);%=> 0.082s

Try it: feature(‘accel’,’off ’);

and run again

Page 30: AM2 MATLAB

'7-} parforN = 1e7; tic w = zeros(1, N); parfor i = 1:N w(i) = i*5; end fprintf('Parfor loop: %.4fs\n', toc); %=> 1.853s

Page 31: AM2 MATLAB

homework• ���%_3Kmatlab

• id = ���N

• id_last = id��=P

• `�y=5sin(2/id_last*x)�:�Vw�2

• xC;xk�010pi� #\500U

• �:�f!�g[j[

• f<+m AM2 MATLAB <id>

• x�xk�010pi�(fh9�pi�H=

• "YD���20N|8Y

• �:�Lceiba)Ea

Page 32: AM2 MATLAB

example