Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chap.1 簡介與入門使用 方煒 台大生機系 彙整.

Similar presentations


Presentation on theme: "Chap.1 簡介與入門使用 方煒 台大生機系 彙整."— Presentation transcript:

1 Chap.1 簡介與入門使用 方煒 台大生機系 彙整

2 小傳(1) 由MathWorks公司於1984年推出的數學軟體。 名稱是由「矩陣實驗室」﹙MATrix LABoratory)所合成。
MATLAB為各種動態系統模擬、數位訊號處理、科學計算、科學目視等領域的標準程式語言。 MATLAB 的許多的核心計算技術是源自於 LINPACK 及 EISPACK 。

3 小傳(2) MATLAB早在 1978 年即已現身,是用 Fortran 撰寫的免費軟體,其作者是當時任教於新墨西哥大學的 Cleve Moler 教授。 Jack Little(又稱為 John Little)將 MATLAB 以C語言重寫,並於 1984 年成立 MathWorks公司,首次推出 MATLAB 商用版。 MathWorks在 Newsgroup 上進行對使用者的技術指導,在 WWW 興起之後,就提供各項技術支援與搜尋功能,並在內聯網(Intranet)方面,以 Web 與資料庫的整合來進行軟體 bug 的追蹤、修復與管理。

4 簡介 MATLAB 早期以矩陣運算為主,第 4 版推出握把式圖形(Handle Graphics),第 5 版中允許使用者建立許多不同的資料型態,(如多維陣列、結構陣列、異質陣列等)。 MATLAB 是一個計算核心,圍繞著這個計算核心,有許多針對不同應用所開發的應用程式,稱為工具箱(Toolboxes) 。

5 Simulink 及 Stateflow Simulink 專用於連續或離散時間的動態系統模擬。 Simulink 是一個模擬核心,圍繞著這個核心所開發的應用程式稱為方塊集(Blocksets)。 Stateflow 則用於模擬有限狀態機(Finite State Machines)或事件驅動系統(Event-driven Systems)。

6 MATLAB、Simulink 及 Stateflow
由現有 Simulink 與 Stateflow 的 C 程式碼自動產生功能,以及定點運算方塊集(Fixed-point Blockset)與 C 程式碼至 VHDL 的自動轉換功能,可看出「高階的系統模擬」或「低階的晶片演算法設計」,都可用 MATLAB/Simulink/Stateflow 及相關的工具箱來達成。 MATLAB Toolboxes Compiler Simulink Stateflow Blocksets RTW Coder

7 Toolboxes Fuzzy logic Communications Garch Control system
Curve fitting Data acquisition Database Datafeed Filter design Financial Financial derivatives Financial time series Fuzzy logic Garch Image processing Instrument control LMI control Matlab link for code composer studio Mapping Model predictive control Model-based calibration

8 Toolboxes Mu-analysis and synthesis Neural Network Optimization
Partial Differential Robust control Signal processing Spline Statistics Symbolic math System identification Virtual reality Wavelet

9 第五版的外觀 啟動方式:( 以MATLAB 5.3版R11為主) 以滑鼠點選兩次桌面上的MATLAB圖示( )
在『開始』的主選單下,選取『程式集/ Matlab / MATLAB 5.3』 於 DOS 視窗輸入下列命令來啟動 MATLAB: {MATLAB 根目錄}\bin\matlab

10 下拉式選單(Pull-down Menu)
下拉式選單: File,Edit,View,Window,Help 等,其中較重要的是 File/Preference 下的圖形介面,可以設定個人的喜好值,如下:

11 工作空間瀏覽器 圖示為 ,可用於瀏覽位於工作空間中的變數,包含其名稱、資料形態、維度大小、佔用位元大小等,其外觀如下:

12 路徑瀏覽器 圖示為 ,功能是以視窗面的方式來對搜尋路徑進行新增、修改或刪除等功能,其外觀如下:

13 Simulink 方塊庫瀏覽器 Simulink 是以方塊圖為基礎的動態模擬系統,是架於 MATLAB 之上的另一個產品。

14 線上支援視窗 圖示為 ,可開啟 MATLAB 的線上支援視窗,並將 MATLAB 所有的指令分門別類。外觀如下:

15 第六版的外觀(1) MATLAB 第六版( 以6.5 (R13)版為主 )的啟動方法和第五版相同。
介面是以 Java 撰寫,因此第一次啟動時間較久。外觀如下:

16 第六版的外觀(2) MATLAB 桌面被分割成三個小視窗: 命令視窗 Command Window
工作空間變數列表Workspace Window 命令歷史列表 List of Command History

17 「View」的下拉式選單(1) 較常用的五個視窗 Profiler: 開啟或關閉”計時器控制頁”。 help: 開啟或關閉”技術支援頁”。
Command Window: 開或關”命令視窗”。 Command History: 開或關”命令歷史列表”。 Current Directory: 開或關”目前目錄檢視窗”。 Workspace: 開啟或關閉”工作空間變數列表”。 Launch Pad: 開啟或關閉”啟動面版”。 前四者為預設(default)的視窗 Profiler: 開啟或關閉”計時器控制頁”。 help: 開啟或關閉”技術支援頁”。

18 「View」的下拉式選單(2) Undock from Desktop

19 「View」的下拉式選單(3) 點選View/Desktop Layout/Five Panel可同時開啟其中較重要的五個視窗:
點選View/Desktop Layout/Default可恢復原狀。

20 「 File 」的下拉式選單(1) 請輸入 A=magic(3); 點選File/New/M file可叫出程式編輯器
點選File/Save 以test1 檔名儲存於預設目錄 請輸入 A=magic(3);

21 「 File 」的下拉式選單(2) 關閉程式編輯器 在Command Window 輸入Test1執行該程式
連續輸入三個指令如Command history 所示 輸出結果如Command Window 視窗所示

22 「 File 」的下拉式選單(3) 點選File/Open 可叫出目前目錄 選取 test1叫出該M file 輸入新的指令如右所示
到Command Window 視窗看執行結果 持續輸入D=A’ 輸入clc 清除Command Window 輸入clear A清除變數A 輸入clear all 清除所有變數

23 「 File 」的下拉式選單(4) 點選File/Set Path可建立程式工作區, Matlab 會依照預設的path 與順序來尋找所要的檔案。

24 第七版的外觀 MATLAB 7.0 (R14) 發行於 2004 年六月,其啟動方法和第六版相同。

25 「活頁夾」型式的MATLAB桌面 點選Desktop/Desktop Layout/All Tabbed可將各個小視窗改為「活頁夾」的型式。
(由左圖可以看出 共有六個小視窗) 若要回到預設的桌面配置,可點選Desktop/Desktop Layout/Default

26

27

28

29

30

31

32

33

34

35 Command Description addpath dirname Adds the directory dirname to the search path. cd dirname Changes the current directory to dirname. dir Lists all files in the current directory. dir dirname Lists all the files in the directory dirname. path Displays the MATLAB search path. pathtool Starts the Set Path tool.

36 System, Directory, and File Commands
Command Description pwd Displays the current directory. rmpath dirname Removes the directory dirname from the search path. what Lists the MATLAB-specific files found in the current working directory. Most data files and other non-MATLAB files are not listed. Use dir to get a list of all files. what dirname Lists the MATLAB-specific files in directory dirname.

37 The default MATLAB Desktop.

38 Entering Commands and Expressions
MATLAB retains your previous keystrokes. Use the up-arrow key to scroll back back through the commands. Press the key once to see the previous entry, and so on. Use the down-arrow key to scroll forward. Edit a line using the left- and right-arrow keys the Backspace key, and the Delete key. Press the Enter key to execute the command.

39 Commands for managing the work session
Command Description clc Clears the Command window. clear Removes all variables from memory. clear v1 v2 Removes the variables v1 and v2 from memory. exist(‘var’)Determines if a file or variable exists having the name ‘var’. quit Stops MATLAB.

40 who Lists the variables currently in memory.
whos Lists the current variables and sizes, and indicates if they have imaginary parts. : Colon; generates an array having regularly spaced elements. , Comma; separates elements of an array. ; Semicolon; suppresses screen printing; also denotes a new row in an array. ... Ellipsis; continues a line.

41 When you type problem1 1. MATLAB first checks to see if problem1 is a variable and if so, displays its value. 2. If not, MATLAB then checks to see if problem1 is one of its own commands, and executes it if it is. 3. If not, MATLAB then looks in the current directory for a file named problem1.m and executes problem1 if it finds it. 4. If not, MATLAB then searches the directories in its search path, in order, for problem1.m and then executes it if found.

42 Two ways to perform operations in MATLAB
1. In the interactive mode, in which all commands are entered directly in the Command window, or By running a MATLAB program stored in script file. You can run the file by typing its name at the Command window prompt.

43 直譯器 Interpreter >> 8/10 ans = 0.8000 >> 5*ans 4
>> s=20*r s = 16

44 Polynomial Roots To find the roots of x3 – 7x2 + 40x – 34 = 0, the session is >>a = [1,-7,40,-34]; >>roots(a) ans = i i 1.0000 The roots are x = 1 and x = 3 ± 5i.

45 A graphics window showing a plot.

46 Some MATLAB plotting commands
plot(x,y) Generates a plot of the array y versus the array x on rectilinear axes. title(’text’) Puts text in a title at the top of the plot. xlabel(’text’) Adds a text label to the horizontal axis (the abscissa). ylabel(’text’) Adds a text label to the vertical axis (the ordinate).

47 Scalar Arithmetic Operations
Symbol Operation MATLAB form ^ exponentiation: ab a^b * multiplication: ab a*b / right division: a/b a/b \ left division: b/a a\b + addition: a + b a + b - subtraction: a - b a - b

48 使用變數與基本運算 一般數學符號運算 在MATLAB 命令視窗(Command Window)內的提示符號(>>)之後輸入運算式,並按入 Enter 鍵即可。例如: >> (5*2+3.5)/5 ans = 2.7000 若不想讓 MATLAB 每次都顯示運算結果,只需在運算式最後加上分號(;)即可,例如: >> (5*2+3.5)/5; >> 1+1 >> 2*3 >> 5.23e12/6.2e5 >> exp(-3) >> atan2(-1,2) >> sin(5) In radians

49 加入註解 若要加入註解(Comments),可以使用百分比符號(%)例如:
>> y = (5*2+3.5)/5; % 將運算結果儲存在變數 y,但不用顯示於螢幕 >> z = y^ % 將運算結果儲存在變數 z,並顯示於螢幕 z = 7.2900

50 註解 COMMENTS The comment symbol may be put anywhere in the line. MATLAB ignores everything to the right of the % symbol. For example, >>% This is a comment. >>x = 2+3 % So is this. x = 5 Note that the portion of the line before the % sign is executed to compute x.

51 m 檔案 (script file) 若要一次執行大量的 MATLAB 指令,可將這些指令存放於一個副檔名為 m 的檔案,並在 指令提示號(>>)下鍵入此檔案主檔名即可。 >> pwd % 顯示目前的工作目錄 >> dir 顯示目前工作目錄的內容 >> cd 可改變工作目錄 >> type myTest.m % 顯示 myTest.m 的內容 >> myTest % 執行 myTest.m “Save and Run” shortcut key, F5. m file中程式太長可用… 連接a=sin(x)*exp(-y)*... log(z)+sqrt(b);

52 Keep in mind when using script files
1. The name of a script file must begin with a letter, and may include digits and the underscore character, up to 31 characters. 2. Do not give a script file the same name as a variable. 3. Do not give a script file the same name as a MATLAB command or function. You can check to see if a command, function or file name already exists by using the exist command.

53 The Assignment Operator =
Typing x = 3 assigns the value 3 to the variable x. We can then type x = x This assigns the value = 5 to x. But in algebra this implies that 0 = 2. In algebra we can write x + 2 = 20, but in MATLAB we cannot. In MATLAB the left side of the = operator must be a single variable. The right side must be a computable value.

54 Numeric Display Formats
Command Description and Example format short Four decimal digits (the default); format long 14 decimal digits; format short e Five digits (four decimals) plus exponent; e+03. format long e 16 digits (15 decimals) plus exponent; e–04.

55 Some plotting commands
Description [x,y] = ginput(n) Enables the mouse to get n points from a plot, and returns the x and y coordinates in the vectors x and y, which have a length n. grid Puts grid lines on the plot. gtext(’text’) Enables placement of text with the mouse.

56 Command window with the Editor/Debugger open.

57 Getting Help Throughout each chapter margin notes identify where key terms are introduced. Each chapter contains tables summarizing the MATLAB commands introduced in that chapter. At the end of each chapter is a summary guide to the commands covered in that chapter. Appendix A contains tables of MATLAB commands, grouped by category, with the appropriate page references. There are three indexes. The first lists MATLAB commands and symbols, the second lists Simulink blocks, and the third lists topics.

58 The Help Navigator contains four tabs:
Contents: a contents listing tab, Index: a global index tab, Search: a search tab having a find function and full text search features, and Demos: a bookmarking tab to start built-in demonstrations.

59 Help Navigator

60 線上支援 help funcname :用來查詢已知指令的用法
>>help atan2 lookfor topic:用來尋找未知的指令或以topic為主題的相關訊息。找到所需的指令後,即可用 help 進一步找出其用法。 譬如輸入 help elliptic 查不到任何內容. 但輸入 lookfor elliptic 有進一步訊息,原來拼錯字了 help ellipke helpwin 或 helpdesk:產生線上支援視窗,其效果和直接點選 MATLAB 命令視窗工作列的圖示是一樣的。 doc funcname:展示funcname函數的線上支援。

61 搜尋路徑 若要檢視 MATLAB 已設定的搜尋路徑,鍵入 path 指令即可: 若只要查詢某一特定指令所在的搜尋路徑,可用 which 指令
要將目錄加入 MATLAB 的搜尋路徑之最前端,使用 addpath 指令,如 >> addpath d:\matlabbook 要將目錄加入 MATLAB 的搜尋路徑之最末,使用 addpath 指令,如 >> addpath d:\matlabbook –end 要將目錄自 MATLAB 的搜尋路徑移除,使用 rmpath 指令,如 >> rmpath d:\matlabbook 搜尋路徑定義在matlabrc.m 檔案內,可直接修改該檔案

62 離開 MATLAB 在命令視窗內,鍵入 exit 指令。 在命令視窗內,鍵入 quit 指令。 直接關閉 MATLAB 的命令視窗。

63 End of Chapter 1


Download ppt "Chap.1 簡介與入門使用 方煒 台大生機系 彙整."

Similar presentations


Ads by Google