1 / 60

Kai-Chun Fan

Introduction to. MAT rix LAB oratory. Kai-Chun Fan. 2010.12.30. Agenda. Basic Usages for Mathematics Coding environments Files I/O Statistic tools Plot tools. 基本操作畫面. >> (prompt) …. How to Use MATLAB. >> DEMO >> HELP [ SIGNAL ] % same as man [ SIGNAL ] in UNIX

stephan
Download Presentation

Kai-Chun Fan

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to MATrix LABoratory Kai-Chun Fan 2010.12.30

  2. Agenda • Basic Usages for Mathematics • Coding environments • Files I/O • Statistic tools • Plot tools

  3. 基本操作畫面 >>(prompt) …

  4. How to Use MATLAB >> DEMO >> HELP [ SIGNAL ] % same as man [ SIGNAL ] in UNIX >> google is your good friend.

  5. 基本數學運算 >> (5*2+1.3-0.8)*10/25 (return) ans = 4.2000 >> x = (5*2+1.3-0.8)*10/25 (return) x = 4.2000 % %為 comment sign % defaultvariable data type:double % MATLAB stores the result in double ans % in default

  6. 基本數學運算 >> (5*2+1.3-0.8)*10/25 (return) ans = 4.2000 >> x = (5*2+1.3-0.8)* … (return) 10/25 (return) x = 4.2000 % 式子太長可用 … 換行

  7. Appendix

  8. Vector & Matrix % vector >> x = [1 3 5 2] (return) x = 1 3 5 2 % matrix >> x = [1 3; 5 2] (return) x = 1 3 5 2

  9. X = 3 X 為一個1 x 1 matrix[3]

  10. Vector & Matrix >> x = [1 3 5 2]; (return) >> y = 2 * x + 1 (return) y = 3 7 11 5 % y is an array for 2 * x(i) + 1

  11. >> x = [1 3 5 2]; (return) >> y = 2 * x + 1 (return) y = 3 7 11 5 >> x = [1 3 5 2] (return) x = 1 3 5 2 >> y = 2 * x + 1 (return) y = 3 7 11 5 ; 抑制輸出

  12. Operators for Vector and Matrix M = A◎B-- ◎處可為 加法(+)、減法(-)、乘法(*)、左除(\)、右除(/) M = A.◎B-- ◎處可為 乘法(*)、乘幂(^)、左除(\)、右除(/) M = A◎c-- ◎處可為 加法(+)、減法(-)、乘法(*)、乘幂(^)、右除(/) M = c◎B-- ◎處可為 加法(+)、減法(-)、乘法(*)、乘幂(^)、左除(\)、右除(./)

  13. Special Operators • M = A .◎ B >>x = [1 2]; >> y = [118 59]; >> x .* y ans = 118 118 >> x * y error

  14. Index for Vector & Matrix % variable(index) >> x = [1 3 5 2]; (return) >> x(1) = 118 (return) x = 118 3 5 2 >> x = [1 3; 5 2]; (return) >> x(2, 1) = 118 (return) x = 1 3 118 2 % index starts from 1, not 0.

  15. Index for Vector & Matrix % range index >> x = [1 2 3; 4 5 6; 7 8 9]; (return) >> x(2, 1:3) (return) ans = 4 5 6 >> x(1:3, 2) (return) ans = 2 5 8

  16. Add & Delete % add element >> x = [1 3 5 2]; (return) >> x(6) = 631 (return) x = 1 3 5 2 0 631 % delete element >> x(3) = []; (return) >> x (return) x= 1 3 2 0 631

  17. Catenate & Inverse % catenate a = 1 2 3 4 5 6 >> a = [a; 7 8 9]; >> a = [a(1,:) 118; a(2,:) 118; a(3,:) 118] a = 1 2 3 118 4 5 6 118 7 8 9 118 % inverse a = 39 39 889 >> a’ a = 39 39 889

  18. % start [: different] : end >> x = 7 : 13 x = 7 8 9 10 11 12 13 >> y = 7 : 1 : 13 y = 7 8 9 10 11 12 13 >> z = 7 : 4 : 13 z = 7 11 : >> a = [1:3; 10:10:30] a = 1 2 3 10 20 30

  19. Variables in Workspace % information >> whos (return) name size bytes class a 2x4 64 double array b 4x2 64 double array ans 1x1 8 double array x 1x1 8 double array y 1x1 8 double array z 1x1 8 double array grand total is 20 elements using 160 bytes % release >> clear a >> a ??? Undefined function or variable ‘a'.

  20. Code in Files % write your code in files, *.m % for example, kerker.m >> kerker (return)

  21. For-loop % for <variable> = <vector> % … % end % in kerker.m for i = 0:2:4, for j = 1:2, h(i/2+1, j) = i/(j*4); end end disp(h) % display, like “cout” format rat % use fraction to display disp(h) % output >> kerker (return) 0 0 0.5 1 2 0.5 0 0 1/2 1 2 1/2

  22. While-loop % while <condition> % … % end x = zeros(1,6); % create an 1 x 6 ZERO matrix while i <= 6, x(i) = 1/i; i = i + 1; end

  23. if % if <condition> % … % end % in world118.m if rand(1,1) > 0.5, % create an 1x1 matrix with random elements disp('Given random number is greater than 0.5.'); end >> world118 Given random number is greater than 0.5.

  24. break

  25. Appendix

  26. nan NaN C o n s t a n t s pi eps i n f ior j nargin nargout realmax realmin

  27. File I/O • Like C language • Read from file filepointer = fopen(‘filename’); variable = fscanf(filepointer, format, [m n]); fclose(filepointer);

  28. File I/O Example fp = fopen('sinx.txt');A = fscanf(fp, '%g %g', [2 inf]); fclose(fp);>> A = A'A =0 0.31420.6283 0.94251.2566 1.57081.8850 2.19912.5133 2.82743.1416 00.3090 0.58780.8090 0.95111.0000 0.95110.8090 0.58780.3090 0

  29. File I/O % load filename x = filename(m, n) load sinx.txtA = sinx.txt(:, 1:2); % A = sinx.txt(inf, 1:2);>> AA =0 0.31420.6283 0.94251.2566 1.57081.8850 2.19912.5133 2.82743.1416 00.3090 0.58780.8090 0.95111.0000 0.95110.8090 0.58780.3090 0

  30. Random Number Generators 均勻分佈亂數 rand常態分佈亂數 randn常態亂數(Normal (Gaussian))-normrnd貝他亂數(Beta)-betarnd二項式亂數(Binomial)- binorndX's亂數(Chi square)-chi2rnd極值亂數(Extreme value)-evrnd指數亂數(Exponential)-exprndF亂數(F)-frnd伽瑪亂數(Gamma)-gamrnd伽瑪亂數(Gamma (unit scale))-randg幾何亂數(Geometric)-geornd超幾何亂數(Hypergeometric)-hygernd反威夏亂數(Inverse Wishart)-iwishrnd常態對數亂數(Lognormal)-lognrnd多變異亂數(Multivariate normal)-mvnrnd多變T亂數(Multivariate t)-mvtrnd負二項式亂數(Negative binomial)-nbinrnd非中央F亂數(Noncentral F)-ncfrnd非中央亂數(Noncentral)-nctrnd非中央X's亂數(Noncentral Chi-square)-ncx2rnd波義松亂數(Poisson)-poissrnd定群樣本亂數(finite population)-randsample魏利亂數(Rayleigh)-raylrndT亂數(T)- trnd離散均勻亂數(Discrete uniform)-unidrnd均勻亂數(Uniform)-unifrnd魏伯亂數(Weibull)-wblrnd威夏亂數矩陣(Wishart)-wishrnd % *nd are not “dname.”

  31. Random Number Generators random(‘dname', mean, var, m, n)

  32. xy - Plane Basic • plot(…) • x-axis: linear scale • y-axis: linear scale • loglog(…) • x-axis: log scale • y-axis: log scale • semilogx(…) • x-axis: log scale • y-axis: linear scale • semilogy(…) • x-axis: linear scale • y-axis: log scale

  33. KER plot(x1) - y-axis adopts the column number plot(x1, y1) plot(x1, y1, LineSpec, ‘PropertyName’, Property value,‘PropertyName’, Property value, …) - LineWidth, MarkerEdgeColor, MarerFaceColor, MarkerSize, …

  34. 2 or More than 2 lines plot(set1, set2, …) plot(set1) hold on; plot(set2) … plot(x1, x2)

  35. x = -pi:0.1:pi; y = sin(x); plot(x,y)

  36. x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); plot(x,y,'--rs',... 'LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10)

  37. LineSpec

  38. 其它線參數 點我 http://www.mathworks.com/help/techdoc/ref/plot.html

  39. Other Properties for Plots axis([xmin, xman, ymin, ymax]) - range of x-axis and y-axis. - the dot out of the range will not be presented. xlabel(‘name for x-axis’) ylabel(‘name for y-axis’) title(‘title for the plot’) test(x, y, text, alignment base, position) - notes for a specific point legend(‘note for line1’, ‘note for line2’, …) grid on; - show the grid line

  40. x = -pi:.1:pi; y = sin(x); p = plot(x,y) xlabel('-\pi \leq \Theta \leq \pi') ylabel('sin(\Theta)') title('Plot of sin(\Theta)') text(-pi/4,sin(-pi/4),'\leftarrow sin(-\pi\div4)',... 'HorizontalAlignment','left') set(p,'Color','red','LineWidth',2) set(gca,'XTick',-pi:pi/2:pi) set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'})

  41. subplot subplot(i, j, #) plot(); subplot(4, 2, 1:2) % #1&#2 subplot(4, 2, [1 3 5]) % #1&#3&#5

  42. Other types of plots

  43. boxplot • x1 = normrnd(5, 1, 100, 1); • x2 = normrnd(6, 1, 100, 1); • boxplot([x1, x2], 'notch', 'on')

  44. boxplot • x = randn(100,25); • subplot(2,1,1) boxplot(x) • subplot(2,1,2) boxplot(x,'plotstyle','compact')

  45. plotyy • x = 0:0.01:10;y1 = 100*exp(-0.1*x).*sin(x);y2 = 0.8*exp(-0.5*x).*sin(10*x);[ax,h1,h2] = plotyy(x,y1,x,y2,‘plot’);set(get(ax(1),‘Ylabel’),‘String’,‘左側 y1 函數’)set(get(ax(2),‘Ylabel’),‘String’,‘右側 y2 函數’)set(h1,‘Linewidth’,4)set(h2,‘LineStyle’,‘:’,‘color’,'k')xlabel('時間 0-10 \mus')title('指令plotyy之應用')

  46. ezplot • ezplot(‘u*sin(u)+v*cos(v)-2')

  47. bar x =1:10; y =rand(size(x)); bar(x,y)

  48. errorbar x = linspace(0, 2*pi, 30); y = sin(x); e = std(y) * ones(size(x)); errorbar(x, y, e)

  49. fplot • fplot(func, range); • % sampled more precisely • fplot('sin(1/x)', [0.02 0.2])

More Related