Matlab 簡介 王偉仲 國立高雄大學應用數學系 版本 : 2003/9/10 大綱 Matlab 基本介紹 陣列運算 ( 向量與矩陣 ) 資料視覺化 ( 二維與三維繪圖 ) 程式寫作 ( 正確, 效率, 穩定 ) 數學演算法, 電腦硬體架構 Matlab 簡介 W. Wang 2 1
驅動問題 以蒙地卡羅法求 值 了解問題 擬定策略 編寫程式 顯示圖形 執行程式 分析數據 1 unit Matlab 簡介 W. Wang 3 自學資源 MATLAB 程式設計與應用, 張智星, 初版, 清蔚科技, 2000 >> demo Gerald Recktenwald 投影片 http://www.prenhall.com/recktenwald. 其他線上教學資料 例如 http://www.mines.utah.edu/gg_computer_seminar/matlab/matlab.html Matlab 簡介 W. Wang 4 2
在視窗系統中啟動 Matlab Matlab 簡介 W. Wang 5 基本介紹 3
什麼是 Matlab? MATLAB = MATrix LABoratory Mathworks: http://www.mathworks.com 主要設計理念 : 以矩陣為基礎的數值計算 高階程式語言 圖形與資料視覺化 各種應用工具箱 Matlab 簡介 W. Wang 7 什麼是 Matlab? ( 續 ) 跨平台支援 (PC / Macintosh / UNIX) 存在與其他程式語言或儀器介面 C, Fortran (MATLAB is callable) 外部資料輸入輸出功能 大小寫在 Matlab 中視為不同字元 ( 例如 :mtxa ~= MTXA) Matlab 簡介 W. Wang 8 4
計算機 Matlab 簡介 W. Wang 9 計算機 ( 續 )» -5/(4.8+5.32)^2-5/(4.8+5.32)^2 ans ans = -0.0488-0.0488» (3+4i)*(3-4i) (3+4i)*(3-4i) ans ans = 25 25» cos(pi/2) cos(pi/2) ans ans = 6.1230e-017 6.1230e-017» exp(acos(0.3)) exp(acos(0.3)) ans ans = 3.5470 3.5470» a = 2; 2;» b = 5; 5;» a^b a^b ans ans = 32 32» x = 5/2*pi; 5/2*pi;» y = sin(x) sin(x) y = 1» z = asin(y) asin(y) z = 1.5708 1.5708 Matlab 簡介 W. Wang 10 5
Matlab 的內建常數 Matlab 簡介 W. Wang 11 Matlab 的內建函式 Matlab 簡介 W. Wang 12 6
生存法則 : 尋找關鍵字 Matlab 簡介 W. Wang 13 生存法則 : 線上救援 Matlab 簡介 W. Wang 14 7
利用分號隱藏輸出 Matlab 簡介 W. Wang 15 單行多敘述 ( 指令 ) Matlab 簡介 W. Wang 16 8
核磁共振影像 Matlab 簡介 W. Wang 17 風流 Matlab 簡介 W. Wang 18 9
震動中的波 In Matlab: demo > Graphics > Vibrating logo Matlab 簡介 W. Wang 19 混沌 In Matlab: demo > Graphics > Lorenz attractor animation 混沌是非週期性之物理系統的演化模式 系統的演化對初始條件非常敏感 實例 : 勞倫茲吸引子 (Lorenz Attractor) Matlab 簡介 W. Wang 20 10
生物現象 人眼可輕易偵測移動中的物體, 但難以分辨同一物體的靜止狀態 對動物來說, 這是重要的生存本能 ( 掠奪 捕食 逃避 ) 範例 : 隱藏的形狀 In Matlab: demo > More Examples > Hiden objects in motion Matlab 簡介 W. Wang 21 快速傅立葉轉換 Fast Fourier Transformation (FFT) Time domain to frequency domain 訊號處理, 影像處理 範例 : 太陽黑子強度頻率 In Matlab: demo > Numerics > Fast Fourier Transformation Matlab 簡介 W. Wang 22 11
幾何結構 Convex hull: 凸包 Delaunay tessellation: 四面體嵌格 Voronoi diagram: 多面體分區 在科學, 工程, 統計, 數學上用來分析數據 範例 : 九點立方體 In Matlab: demo > Numerics > Tessellation and interpolation of scattered data Matlab 簡介 W. Wang 23 向量與矩陣 12
向量與矩陣 A = 1 2 Rows ( 行 ) (m) 3 4 5 Columns ( 列 ) (n) 1 2 3 4 5 4 10 1 6 2 1 6 11 16 21 8 1.2 9 4 25 2 7 12 17 22 7.2 5 7 1 11 3 8 13 18 23 0 0.5 4 5 56 4 9 14 19 24 23 83 13 0 10 5 10 15 20 25 A (2,4) A (17) 純量 : 1-by-1 陣列 向量 : m-by-1 陣列 1-by-n 陣列 矩陣 : m-by-n 陣列 矩陣元素可以是數值或字元 以行為主 Column-major Matlab 簡介 W. Wang 25 向量與矩陣 ( 續 ) Matlab 簡介 W. Wang 26 13
矩陣 Matlab 簡介 W. Wang 27 矩陣一維與二維索引 (index) A = A(3,1) A(3) 1 2 3 4 5 1 2 3 4 5 4 10 1 6 2 1 6 11 16 21 8 1.2 9 4 25 2 7 12 17 22 7.2 5 7 1 11 3 8 13 18 23 0 0.5 4 5 56 4 9 14 19 24 23 83 13 0 10 5 10 15 20 25 A(1:5,5) A(:,5) A(21:25) A(4:5,2:3) A([9 14;10 15]) A(1:end,end) A(:,end) A(21:end) Matlab 簡介 W. Wang 28 14
冒號 Matlab 簡介 W. Wang 29 冒號 ( 續 ) Matlab 簡介 W. Wang 30 15
冒號 ( 續 ) Matlab 簡介 W. Wang 31 複數 Matlab 簡介 W. Wang 32 16
複數 ( 續 ) Matlab 簡介 W. Wang 33 複數 ( 續 ) Matlab 簡介 W. Wang 34 17
操作向量與矩陣 Matlab 簡介 W. Wang 35 操作向量與矩陣 ( 續 )» a = [1 [12 3 4; 4; 5 6 7 8]; 8];» b = ones(4,3); ones(4,3); [2x4] [4x3]» c = a*b a*b c = [2x4]*[4x3] [2x3] 10 10 10 10 10 10 26 26 26 26 26 26 a(2nd row).b(3rd column) Matlab 簡介 W. Wang 36 18
操作向量與矩陣 ( 續 )» A = [-1 [-11 2; 2; 3-1 -11;-1 1;-13 4]; 4];» b = [2;6;4]; [2;6;4];» x = inv(a)*b inv(a)*b x = 1.0000 1.0000-1.0000-1.0000 2.0000 2.0000» x = A\b A\b x = 1.0000 1.0000 %x %x 1 1-1.0000-1.0000%x %x 2 2 2.0000 2.0000 %x %x 3 3 -x 1 + x 2 + 2x 3 = 2 3x 1 - x 2 + x 3 = 6 -x 1 + 3x 2 + 4x 3 = 4 Matlab 簡介 W. Wang 37 向量化 Matlab 簡介 W. Wang 38 19
向量化 ( 續 )» tic; tic; for for I = 1:1000 1:1000 Density(I) Density(I) = Mass(I)/(Length(I)*Width(I)*Height(I)); Mass(I)/(Length(I)*Width(I)*Height(I)); end; end; toc toc elapsed_time elapsed_time = 0.0500 0.0500 使用 tic 與 toc 測量時間間隔» tic; tic; Density Density = Mass./(Length.*Width.*Height); Mass./(Length.*Width.*Height); toc toc elapsed_time elapsed_time = 0 向量化程式比迴圈快上許多 Matlab 簡介 W. Wang 39 向量化 ( 續 ) Matlab 簡介 W. Wang 40 20
陣列元素運算元 Matlab 簡介 W. Wang 41 陣列元素運算元 ( 續 ) Matlab 簡介 W. Wang 42 21
陣列元素運算元 ( 續 ) Matlab 簡介 W. Wang 43 程式寫作 22
布林 (Boolean) 運算元 Boolean Operators = = equal to > greater than < less than ~ not & and or isempty() isfinite(), etc.... any() all() 1 = 真 TRUE 0 = 假 FALSE Matlab 簡介 W. Wang 45 程式流程控制 邏輯控制 : if/ switch if I == J A(I,J) = 2; elseif abs(i-j) == 1 A(I,J) = -1; else A(I,J) = 0; end switch algorithm case 'ode23 str = '2nd/3rd order'; case {'ode15s', 'ode23s'} str = 'stiff system'; otherwise str = 'other algorithm'; end Matlab 簡介 W. Wang 46 23
程式流程控制 ( 續 ) 迴圈控制 : for / while N = 10; for I = 1:N for J = 1:N A(I,J) = 1/(I+J-1); end end I = 1; N = 10; while I <= N J = 1; while J <= N A(I,J) = 1/(I+J- 1); J = J+1; end I = I+1; end Matlab 簡介 W. Wang 47 底稿 (script) 標準 ASCII 文字檔包含一連串 MATLAB 的指令 輸入 PlotSine 指令後,Matalb 會將 PlotSine.m 檔直譯 (Interpreter) 並執行 (execution) % 後面是註解 底稿中的變數是基本工作空間的全域變數 容易進行變數檢視及除錯 Matlab 簡介 W. Wang 48 24
函式 (function) 函式可將程式模組化, 提供更多功能, 並易於維護與改善 通常包含輸入與輸出 變數預設為局部的 (local) 使用者可以將變數改為全域的 (global) Matlab 簡介 W. Wang 49 輸出引數 函式結構 函式名稱 ( 須與檔案名稱相同.m) 輸入引數 線上說明 MATLAB 程式主體 function y = mean(x) % MEAN MEAN Average or or mean mean value. value. % For For vectors, MEAN(x) returns the the mean mean value. value. % For For matrices, MEAN(x) is is a row row vector vector % containing the the mean mean value value of of each each column. [m,n] [m,n] = size(x); if ifm == == 1 m = n; n; end end y = sum(x)/m;»output_value = mean(input_value) 指令列文法 Matlab 簡介 W. Wang 50 25
工作空間 (Workspace) MATLAB ( 基本 ) 工作空間 : 供指令列與底稿變數使用 函式工作空間 : 每個函式有它自己的工作空間以儲存該函式的局部變數 可透過輸入與輸出引數與函式空間傳遞變數值 可增加程式結構化與避免變數名稱衝突 全域工作空間 : 全域工作空間可供多重工作空間使用 若要在各個工作空間使用需要有啟動的動作 Matlab 簡介 W. Wang 51 不同工作空間中傳遞資料 MATLAB 工作空間 函式輸入輸出引數 函式工作空間 在相關工作空間中設定全域變數»global variable_name 全域變數 全域工作空間 Matlab 簡介 W. Wang 52 26
二維與三維繪圖 二維繪圖 給定 x 座標值與 y 座標值 指定顏色, 連線型態, 標誌符號 plot(xdata, ydata, 'color_linestyle_marker') plot(x1, y1, 'clm1', x2, y2, 'clm2',...) Matlab 簡介 W. Wang 54 27
二維繪圖 ( 續 ) Matlab 簡介 W. Wang 55 二維繪圖 ( 續 ) Matlab 簡介 W. Wang 56 28
二維繪圖 ( 續 ) TITLE LEGEND YLABEL TEXT or GTEXT CURVES XLABEL Matlab 簡介 W. Wang 57 二維繪圖 ( 續 )» x = 0:.1:2*pi;» y = sin(x);» plot(x,y) Matlab 簡介 W. Wang 58 29
二維繪圖 ( 續 )» x = 0:.1:2*pi;» y = sin(x);» plot(x,y,'b')» grid grid on on» hold hold on on» plot(x,exp(-x),'r:*') Matlab 簡介 W. Wang 59 二維繪圖 ( 續 )» title('2-d Plots')» xlabel('time')» ylabel('sin(t)')» text(pi/3,sin(pi/3),... '<--Sin(\pi/3)')» legend('sine Wave', Wave',...... 'Decaying Exponential') Matlab 簡介 W. Wang 60 30
二維繪圖 ( 續 )» axis([0 2*pi 2*pi 0 1]) 1]) Matlab 簡介 W. Wang 61 二維繪圖 ( 續 ) subplot(#rows, #cols, index)»subplot(2,2,1);»plot(1:10)»subplot(2,2,2)»x»x = 0:.1:2*pi;»plot(x,sin(x))»subplot(2,2,3)»x»x = 0:.1:2*pi;»plot(x,exp(-x), r )»subplot(2,2,4)»plot(peaks) Matlab 簡介 W. Wang 62 31
二維繪圖 ( 續 ) LOGLOG Both axes logarithmic SEMILOGY log Y linear X SEMILOGX log X linear Y PLOTYY 2 sets of linear axes Matlab 簡介 W. Wang 63 三維繪圖 plot3(xdata, ydata, zdata, 'clm',...)» z = 0:0.1:40;» x = cos(z);» y = sin(z);» plot3(x,y,z) Matlab 簡介 W. Wang 64 32
三維繪圖 ( 續 ) Matlab 簡介 W. Wang 65» x = [0 [01 2 3]; 3];» y = [0 [02 4 6 8]; 8];» [xx, [xx, yy] yy] = meshgrid(x,y) meshgrid(x,y) xx xx = 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 yy yy = 0 0 0 0 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8» zz zz = xx xx + yy; yy;» surf(xx,yy,zz) surf(xx,yy,zz) 三維繪圖 ( 續 ) Matlab 簡介 W. Wang 66 33
三維繪圖 ( 續 ) Matlab 簡介 W. Wang 67 三維繪圖 ( 續 ) Matlab 簡介 W. Wang 68 34
參考資料 鈦思科技股份有限公司 Matlab 簡介投影片 Slides by Gerald Recktenwald http://www.prenhall.com/recktenwald. Matlab 簡介 W. Wang 69 線上教學 MATLAB 程式設計與應用 ( 網路版 ), 張智星 - MATLAB 基本運算, 基本平面繪圖, 基本立體繪圖 http://www.cs.nthu.edu.tw/~jang/mlbook/ - 習題精選 http://neural.cs.nthu.edu.tw/jang/books/matlab/exercise/list.asp?sepfield=chapter 計算機應用, 吳俊諆 - MATLAB 使用方法與工程上的應用 http://webclass.ncu.edu.tw/~junwu/ 國立台北科技大學, 微積分電腦輔助學習網站 - MATLAB 使用介紹 ( 包含 MATLAB 指令簡介 ): http://math.ntut.edu.tw:8000/matlab/course.htm - MATLAB 應用實例 : http://math.ntut.edu.tw:8000/matlab/ex.htm Matlab 簡介 W. Wang 70 35
線上教學 ( 續 ) Matlab 線上教材, 單維彰 http://libai.math.ncu.edu.tw/bcc16/b/matlab/ Matlab 簡介, 馮丁樹 http://ecaaser5.ecaa.ntu.edu.tw/weifang/matlab/index-matlab.htm Matlab 程式設計, 陳榮輝 - 首頁 http://che.cycu.edu.tw/jason/complang(fall2002)/index.htm - 課程內容 http://che.cycu.edu.tw/jason/complang(fall2002)/class%20handouts.htm Matlab 簡介 W. Wang 71 線上教學 ( 續 ) MATLAB 工具箱功 \ 能簡介 http://wavenet.cycu.edu.tw/~cpse/matlab.html 如何建立 stand-alone 的 MATLAB 應用程式, 方煒 饒瑞佶 http://ecaaser5.ecaa.ntu.edu.tw/weifang/bio-ctrl/compiler/standalone.htm Matlab 簡介 W. Wang 72 36
線上教學 ( 續 ) (1) MATLAB Online Reference Documentation (U. of Maryland) http://www-unix.umbc.edu/matlab/referencetoc.html (2) Matlab Resources (George Mason U.) http://bass.gmu.edu/matlab/matlab.html (3) MatLab Tutorial (Utah) http://www.math.utah.edu/~eyre/computing/matlab-intro/index.html (4) Matlab Basics Tuturial (Carnegie Mellon) http://www.engin.umich.edu/group/ctm/basic/basic.html Matlab 簡介 W. Wang 73 線上教學 ( 續 ) (5) A Practical Introduction to MATLAB (Michigan Tech) http://www.math.mtu.edu/~msgocken/intro/intro.html (6) Matlab Lessons (US Naval Academy) http://web.ew.usna.edu/~mecheng/design/cad/matlab/usna.html (7) Maine 大學的 MATLAB Educational Sites http://www.eece.maine.edu/mm/matweb.html (8) MATLAB 教學 University of New Hampshire http://www.math.unh.edu/~mathadm/tutorial/software/matlab/ Matlab 簡介 W. Wang 74 37
線上教學 ( 續 ) (9) MATLAB 教學 University of British Columbia http://www.cs.ubc.ca/spider/cavers/matlabguide/guide.html (10) MATLAB 教學 University of Florida http://www.math.ufl.edu/help/matlab-tutorial/ (11) MATLAB 教學 University of California at Los Angeles http://www.biomath.medsch.ucla.edu/faculty/sblower/biomath209/tutorial/ MatlabTutorial.htm (12) 伊利諾大學 Heath 教授所收集的 MATLAB Resources http://www.cse.uiuc.edu/heath/scicomp/matlab.html Matlab 簡介 W. Wang 75 38