MATLAB minntnu edu tw http math ntnu edu
科學計算 具介紹 MATLAB 黃聰明 min@ntnu. edu. tw http: //math. ntnu. edu. tw/~min MATLAB介紹 T. -M. Huang 0
矩陣的索引或下標 (II) 1 4 6 10 2 8 1 7 2 3 A= 7 5 A(2, 3) A(12) MATLAB 基本功能介紹 18 14 10 23 5 19 5 15 13 22 7 1 4 13 17 13 9 21 2 4 7 3 23 12 8 4 16 6 9 5 0 11 A(1: 5, 5) A(: , 5) A(21: 25) 24 4 20 0 25 3 A(4: 5, 2: 3) A(1: 5, end) A(: , end) A(21: end) A([ 9 14; 10 15 ]) T. -M. Huang 7
預先分配陣列與向量化陣列 n = 20000; tic; square = zeros(1, n); for ii = 1: n square(ii) = ii^2; end fprintf('Total cpu time = %g n', toc); n = 20000; tic; for ii = 1: n square(ii) = ii^2; end fprintf('Total cpu time = %g n', toc); n = 20000; tic; ii = 1: n; square = ii. ^2; fprintf('Total cpu time = %g n', toc); 迴圈 T. -M. Huang 動手做 main_script_dense_2 DLapla cian. m 10
顯示輸出資料 指令格式 結 果 舉 例 format short 顯示 4位小數(預設值) 12. 3457 format long 顯示 14位小數 12. 345678901234567 format short e 顯示 5個數字加冪次方 1. 2346 e+001 format short g 總共顯示 5個數字(可加 12. 346 或不加冪次方) format long e 顯示 15位小數字加冪次 1. 234567890123457 e+001 方 format long g 總共顯示 15個數字(可 加或不加冪次方) 12. 3456789012346 16位元進位格式 4028 b 0 fcd 32 f 707 a format hex MATLAB 基本功能介紹 T. -M. Huang 12
disp num 2 str 轉換數字成字串 >> str = [‘ The value of pi = ’ , num 2 str(pi)]; >> disp(str); The value of pi = 3. 1416 int 2 str 轉換整數成字串 MATLAB 基本功能介紹 >> File. Name 1 = 'rslt_w'; >> File. Name = strcat(File. Name 1, int 2 str(10)) File. Name = rslt_w 10 T. -M. Huang 13
fprintf格式化輸出 一個或多個陣列變數 fprintf(format, data) 字串:描述輸出資料的方式 >> fprintf('The value of pi is %f n', pi) The value of pi is 3. 141593 >> fprintf('The value of pi is %6. 2 f n', pi) The value of pi is 3. 14 MATLAB 基本功能介紹 T. -M. Huang 14
繪圖功能簡介 x = 0: 0. 1: 10; y = x. ^2– 10. *x+15; plot(x, y); title('Plot of y = x. ^2 -10. *x+15') ylabel('y'); grid on; grid off; xlabel('x'); MATLAB 基本功能介紹 T. -M. Huang 17
多重線條繪圖 x = 0: pi/100: 2*pi; y 1 = sin(2*x); y 2 = 2*cos(2*x); plot( x, y 1, x, y 2 ); plot(x, y 1); hold on; plot(x, y 2); hold off MATLAB 基本功能介紹 T. -M. Huang 18
線條顏色、形式、 資料標記及說明文字(I) plot(x, y 1); hold on; plot(x, y 2); hold off plot(x, y 1, 'ro-. ' ); hold on; plot(x, y 2, 'gx: '); hold off MATLAB 基本功能介紹 T. -M. Huang 19
線條顏色、形式、 資料標記及說明文字(I) legend('sin(2 x)', '2 cos(2 x)', 'Location', 'North. West'); text(3. 3, 1. 5, ‘ 2 cos(2 x)') text(1. 5, 0. 5, 'sin(2 x)') MATLAB 基本功能介紹 T. -M. Huang 20
線條顏色、形式、 資料標記及說明文字(II) x = 0: pi/100: 2*pi; y 1 = sin(2*x); y 2 = 2*cos(2*x); plot(x, y 1, 'ro-. ' ); hold on; plot(x, y 2, 'gx: '); hold off legend('sin(2 x)', '2 cos(2 x)', 'Location', 'North. West'); text(1. 5, 0. 5, 'sin(2 x)') text(3. 3, 1. 5, '2 cos(2 x)') set(gca, 'xtick', [0 1/2*pi pi 3/2*pi]) set(gca, 'xticklabel', {'0', '1/2 pi', '3/2 pi', '2 pi'}) MATLAB 基本功能介紹 T. -M. Huang 21
x = -2. 5*pi: pi/100: 2. 5*pi; y 1 = sin(2*x); y 2 = 2*cos(2*x); figure(1) plot(x, y 1, 'ro-. '); title('plot sin(2 x)'); figure(2) plot(x, y 2, 'gx: '); title('plot 2 cos(2 x)'); 分支宣告與程式設計 T. -M. Huang 13
subplot(m, n, p) x = -2. 5*pi: pi/100: 2. 5*pi; y 1 = sin(2*x); y 2 = 2*cos(2*x); z 1 = exp(x/10); z 1 = z 1. *y 2; z 2 = y 1. *y 2; subplot(2, 2, 1) plot(x, y 1, 'r'); title('plot sin(2 x)'); subplot(2, 2, 2) plot(x, y 2, 'g'); title('plot 2 cos(2 x)'); subplot(2, 2, 3) plot(x, z 1, 'b'); title('plot 2 cos(2 x)exp(x/10)'); subplot(2, 2, 4) plot(x, z 2, 'm'); title('plot 2 cos(2 x)sin(2 x)'); 分支宣告與程式設計 T. -M. Huang 14
圖形化影像輸出(I) print <options> <filename> x = 0: 0. 1: 10; y = x. ^2– 10. *x+15; plot(x, y); print –djpeg fig_ch 2. jpeg 產生一個 jpeg 圖形, 檔案名稱fig_ch 2. jpeg MATLAB 基本功能介紹 option 功能敘述 -deps 產生一個灰階的 eps 圖形 -depsc 產生一個彩色的 eps 圖形 -dpng 產生一個 png 圖形 -dtiff 產生一個壓縮的 tiff 圖形 T. -M. Huang 24
mesh 和 surf mesh:可畫出立體的「網狀圖」(Mesh Plots) surf:可畫出立體的「曲面圖」(Surface Plots) z = [0 2 1; 3 2 4; 4 4 4; 7 6 8]; mesh(z); xlabel('X 軸 = column index'); ylabel('Y 軸 = row index'); 其它的資料型態與繪圖型態 T. -M. Huang 46
分支 (Branching Command) 條 件 指 令 if-else switch - case otherwise if expression statements else statements end 分支宣告與程式設計 switch_expr case_expr, statements case {case_expr 1, case_expr 2, . . . } statements. . . otherwise, statements end T. -M. Huang 4
While 迴圈 迴 圈 for while 確定重複執行之次數 不確定重複執行次數 for variable = expr statements end 迴圈 while expression statements end T. -M. Huang 36
for 迴圈(1) for ii = 1: 4 statement 1 … statement n end 1 2 3 4 ii = 1 ii = 2 ii = 3 ii = 4 statement 1 1 … … statement n n end 迴圈 T. -M. Huang 37
for 迴圈(2) for ii = 1: 2: 6 statement 1 … statement n end 1 3 5 ii = 1 ii = 3 ii = 5 statement 1 1 … … … statement n n end 迴圈 T. -M. Huang 38
邏輯陣列與向量優化 a = [1 2 3; 4 5 6; 7 8 9]; b = a > 5; for ii = 1: size(a, 1) for jj = 1: size(a, 2) if ( a(ii, jj) > 5 ) a(ii, jj) = sqrt(a(ii, jj)); end end >> a(b) = sqrt(a(b)) a= 1. 0000 2. 0000 3. 0000 4. 0000 5. 0000 2. 4495 2. 6458 2. 8284 3. 0000 迴圈 T. -M. Huang 39
- Slides: 43