1 / 63

Chap.1 簡介與入門使用

Chap.1 簡介與入門使用. 方煒 台大生機系 彙整. 小傳 (1). 由 MathWorks 公司於 1984 年推出的數學軟體 。 名稱是由「矩陣實驗室」 ﹙ MAT rix LAB oratory )所合成 。 MATLAB 為各種動態系統模擬、數位訊號處理、科學計算、科學目視等領域的標準程式語言。 MATLAB 的許多的核心計算技術是源自於 LINPACK 及 EISPACK 。. 小傳 (2).

Download Presentation

Chap.1 簡介與入門使用

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

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

  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 之上的另一個產品。 • Simulink 方塊庫瀏覽器圖示為 ,可以顯示可用的 Simulink 內建方塊圖,其外觀如下:

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

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

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

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

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

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

  20. 「 File 」的下拉式選單(1) • 點選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 • 輸入新的指令如右所示 • 直接按F5執行新修改的程式 • 到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. 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.

  27. 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.

  28. The default MATLAB Desktop.

  29. 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.

  30. 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.

  31. 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.

  32. 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. When you type problem1

  33. 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.

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

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

  36. A graphics window showing a plot.

  37. 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).

  38. Scalar Arithmetic Operations Symbol Operation MATLAB form ^ exponentiation:aba^b * multiplication:aba*b / right division:a/b a/b \ left division:b/a a\b + addition:a + ba + b - subtraction:a - ba - b

  39. 使用變數與基本運算 • 一般數學符號運算 • 在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

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

  41. 註解 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.

More Related