AM2 MATLAB
-
Upload
jia-long-yeh -
Category
Education
-
view
122 -
download
1
Transcript of AM2 MATLAB
B�P�0
l$MATLABR�
Matalbcz• F�[Zb => �I�X �{�
• �^i/ => 5P�I�Xu]i/
• vi/ => ��@i�i.e. integer -> double�
• �sQ�yMT
• A1S�~rJ
.��4�*�W�pA1S�~rJ�t*�toolbox�
A1S�~rJ�t*�toolbox�
A1S�~rJ�t*�toolbox�
Matlab z&
Matlab z&
current folder
variablecommand line
command• lookfor <keyword>
• help <command>
• a=1
• a=2;
• whos a
• clear 0
• %
simple calculator
• a = 1
• b = a+1
• c = a + b
• d = a*b^c - c/b
predefined variables
• pi %=> 3.14……
• i or j %=> 0.0000+1.0000i
• %Tips: use 1i
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
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.
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])
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.
operator
• matrix operatior
• *
• /
• ^
• …..
• element-wise operator
• .*
• ./
• .^
• …..
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
matlab function
• v=1:20;
• sin(v)
• cos(v), tan(v)….
• exp(v), log(v)…
• diag, eig( )
• sum( )
• check how to use: help <function>
try it• m = magic(100);
• Get following arrays:
• 1st row
• 25th column
• diagonal
• Get the summation of above arrays. Are they the same?
example 1: eigenvalue problem
• A=magic(5)
• [vectors, values] = eig(A)
• isequal(A*vectors,vectors*values) %=> ?
• A*vectors-vectors*values %=> ?
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);
BUT !!!
���
� ���������
����
����
����� �������
����
� ���
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);
compare two scalar• ==
• >
• <
• >=
• <=
• true: return 1
• otherwise: return 0
• Try it
• 1==2
• 3>=2
• 1>=2
• 3<3
if…else
• grade = 65;
• if grade >= 60
• disp('Pass~');
• else
• disp('Fail!! QQ~');
• end
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
try it
• m = magic(100);
• Get row index and column index (i,j) for all the elements which grater than 3.
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�
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
'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
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
example