Chap.1 簡介與入門使用

63
MATLAB 程程程程程程程程程 Chap.1 程程程程程程程 程程 程程程程程 程程

description

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

Transcript of Chap.1 簡介與入門使用

MATLAB 程式設計與工程應用

Chap.1 簡介與入門使用

方煒 台大生機系 彙整

MATLAB 程式設計與工程應用

小傳 (1)

由 MathWorks 公司於 1984 年推出的數學軟體。

名稱是由「矩陣實驗室」﹙ MATrix LABoratory )所合成。

MATLAB 為各種動態系統模擬、數位訊號處理、科學計算、科學目視等領域的標準程式語言。

MATLAB 的許多的核心計算技術是源自於 LINPACK 及 EISPACK 。

MATLAB 程式設計與工程應用

小傳 (2) MATLAB 早在 1978 年即已現身,是用 Fortran 撰寫的免費軟體,其作者是當時任教於新墨西哥大學的 Cleve Moler 教授。

Jack Little (又稱為 John Little )將 MATLAB 以 C 語言重寫,並於 1984 年成立 MathWorks公司,首次推出 MATLAB 商用版。

MathWorks 在 Newsgroup 上進行對使用者的技術指導,在 WWW 興起之後,就提供各項技術支援與搜尋功能,並在內聯網( Intranet )方面,以 Web 與資料庫的整合來進行軟體 bug 的追蹤、修復與管理。

MATLAB 程式設計與工程應用

簡介 MATLAB 早期以矩陣運算為主,第 4 版推出握把式圖形( Handle Graphics ),第 5 版中允許使用者建立許多不同的資料型態,(如多維陣列、結構陣列、異質陣列等)。

MATLAB 是一個計算核心,圍繞著這個計算核心,有許多針對不同應用所開發的應用程式,稱為工具箱( Toolboxes ) 。

MATLAB 程式設計與工程應用

Simulink 及 Stateflow Simulink 專用於連續或離散時間的動態系統模擬。 Simulink 是一個模擬核心,圍繞著這個核心所開發的應用程式稱為方塊集( Blocksets )。

Stateflow 則用於模擬有限狀態機( Finite State Machines )或事件驅動系統( Event-driven Systems )。

MATLAB 程式設計與工程應用MATLAB 、 Simulink 及 Stateflow

MATLAB 、 Simulink 及 Stateflow 三者的關係:

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

MATLABToolboxes Compiler

Simulink

Stateflow

Blocksets RTW

Coder

MATLAB 程式設計與工程應用

Toolboxes Communications 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

MATLAB 程式設計與工程應用

Toolboxes Mu-analysis and

synthesis Neural Network Optimization Partial Differential Robust control Signal processing Spline Statistics

Symbolic math System

identification Virtual reality Wavelet

MATLAB 程式設計與工程應用

第五版的外觀 啟動方式:( 以 MATLAB 5.3 版 R11 為主) 以滑鼠點選兩次桌面上的 MATLAB 圖示( )

在『開始』的主選單下,選取『程式集 / Matlab / MATLAB 5.3 』

於 DOS 視窗輸入下列命令來啟動 MATLAB :{MATLAB 根目錄 }\bin\matlab

MATLAB 程式設計與工程應用下拉式選單( Pull-down Menu )

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

MATLAB 程式設計與工程應用

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

MATLAB 程式設計與工程應用

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

MATLAB 程式設計與工程應用

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

Simulink 方塊庫瀏覽器圖示為 ,可以顯示可用的 Simulink 內建方塊圖,其外觀如下:

MATLAB 程式設計與工程應用

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

MATLAB 程式設計與工程應用

第六版的外觀 (1)

MATLAB 第六版( 以 6.5 (R13) 版為主 )的啟動方法和第五版相同。

介面是以 Java 撰寫,因此第一次啟動時間較久。外觀如下:

MATLAB 程式設計與工程應用

第六版的外觀 (2)

MATLAB 桌面被分割成三個小視窗:

工作空間變數列表Workspace Window

命令歷史列表List of Command

History

命令視窗Command Window

MATLAB 程式設計與工程應用

「 View 」的下拉式選單 (1) 較常用的五個視窗

Command Window: 開或關”命令視窗”。 Command History: 開或關”命令歷史列表”。 Current Directory: 開或關”目前目錄檢視窗”。 Workspace: 開啟或關閉”工作空間變數列表”。 Launch Pad: 開啟或關閉”啟動面版”。 前四者為預設 (default) 的視窗

Profiler: 開啟或關閉”計時器控制頁”。 help: 開啟或關閉”技術支援頁”。

MATLAB 程式設計與工程應用

「 View 」的下拉式選單 (2)

Undock from Desktop

MATLAB 程式設計與工程應用

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

點選 View/Desktop Layout/Default 可恢復原狀。

MATLAB 程式設計與工程應用

「 File 」的下拉式選單 (1) 點選File/New/M file可叫出程式編輯器

點選File/Save 以test1 檔名儲存於預設目錄

請輸入

A=magic(3);

MATLAB 程式設計與工程應用

「 File 」的下拉式選單 (2) 關閉程式編輯器

在 Command Window 輸入Test1執行該程式

連續輸入三個指令如Command history 所示

輸出結果如Command Window 視窗所示

MATLAB 程式設計與工程應用

「 File 」的下拉式選單 (3)

點選 File/Open 可叫出目前目錄

選取 test1叫出該M file 輸入新的指令如右所示 直接按 F5執行新修改的程

式 到 Command Window

視窗看執行結果 持續輸入 D=A’ 輸入 clc 清除 Command

Window 輸入 clear A清除變數 A 輸入 clear all 清除所有變

MATLAB 程式設計與工程應用

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

MATLAB 程式設計與工程應用

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

MATLAB 程式設計與工程應用

「活頁夾」型式的 MATLAB 桌面 點選 Desktop/Desktop Layout/All Tabbed 可將各個小視窗改為「活頁夾」的型式。

(由左圖可以看出

共有六個小視窗)

若要回到預設的桌面配置,可點選 Desktop/Desktop Layout/Default

MATLAB 程式設計與工程應用

MATLAB 程式設計與工程應用

MATLAB 程式設計與工程應用

MATLAB 程式設計與工程應用

MATLAB 程式設計與工程應用

MATLAB 程式設計與工程應用

MATLAB 程式設計與工程應用

MATLAB 程式設計與工程應用

MATLAB 程式設計與工程應用

MATLAB 程式設計與工程應用

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.

MATLAB 程式設計與工程應用

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.

System, Directory, and File Commands

MATLAB 程式設計與工程應用

The default MATLAB Desktop.

MATLAB 程式設計與工程應用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.

MATLAB 程式設計與工程應用

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.

Commands for managing the work session

MATLAB 程式設計與工程應用

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.

MATLAB 程式設計與工程應用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.

MATLAB 程式設計與工程應用

1. In the interactive mode, in which all commands are entered directly in the

Command window, or2. By running a MATLAB program stored in

script file.

You can run the file by typing its name at the Command window prompt.

Two ways to perform operations in MATLAB

MATLAB 程式設計與工程應用

>> 8/10ans = 0.8000>> 5*ansans = 4>> r=8/10r = 0.8000>> rr = 0.8000>> s=20*rs = 16

直譯器 Interpreter

MATLAB 程式設計與工程應用

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.

Polynomial Roots

MATLAB 程式設計與工程應用

A graphics window showing a plot.

MATLAB 程式設計與工程應用

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

MATLAB 程式設計與工程應用

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

Scalar Arithmetic Operations

MATLAB 程式設計與工程應用

使用變數與基本運算 一般數學符號運算

在 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

MATLAB 程式設計與工程應用

加入註解 若要加入註解( Comments ),可以使用百分比符號( % )例如:

>> y = (5*2+3.5)/5; % 將運算結果儲存在變數 y ,但不用顯示於螢幕

>> z = y^2 % 將運算結果儲存在變數 z ,並顯示於螢幕

z = 7.2900

MATLAB 程式設計與工程應用

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.

註解 COMMENTS

MATLAB 程式設計與工程應用

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);

MATLAB 程式設計與工程應用

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.

MATLAB 程式設計與工程應用

The Assignment Operator = Typing x = 3 assigns the value 3 to the variable x. We can then type x = x + 2. This assigns the

value 3 + 2 = 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.

MATLAB 程式設計與工程應用

Numeric Display Formats

Command Description and Example

format short Four decimal digits (the default); 13.6745.

format long 14 decimal digits; 17.27484029463547.

format short e Five digits (four decimals) plus exponent; 6.3792e+03.

format long e 16 digits (15 decimals) plus exponent; 6.379243784781294e–04.

MATLAB 程式設計與工程應用

Some plotting commands

Command

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

Description

MATLAB 程式設計與工程應用

Command window with the Editor/Debugger open.

MATLAB 程式設計與工程應用

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.

MATLAB 程式設計與工程應用

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.

MATLAB 程式設計與工程應用

Help Navigator

MATLAB 程式設計與工程應用

線上支援 help funcname :用來查詢已知指令的用法

>>help atan2 lookfor topic:用來尋找未知的指令或以 topic為主題的相關訊息。找到所需的指令後,即可用 help 進一步找出其用法。 譬如輸入 help elliptic 查不到任何內容 . 但輸入 lookfor elliptic 有進一步訊息,原來拼錯字了 help ellipke

helpwin 或 helpdesk :產生線上支援視窗,其效果和直接點選 MATLAB 命令視窗工作列的圖示是一樣的。

doc funcname:展示 funcname函數的線上支援。

MATLAB 程式設計與工程應用

搜尋路徑 若要檢視 MATLAB 已設定的搜尋路徑,鍵入 path 指令即

可:>> path

若只要查詢某一特定指令所在的搜尋路徑,可用 which 指令 要將目錄加入 MATLAB 的搜尋路徑之最前端,使用

addpath 指令,如 >> addpath d:\matlabbook 要將目錄加入 MATLAB 的搜尋路徑之最末,使用 addpath

指令,如 >> addpath d:\matlabbook –end 要將目錄自 MATLAB 的搜尋路徑移除,使用 rmpath 指令,

如 >> rmpath d:\matlabbook 搜尋路徑定義在 matlabrc.m 檔案內,可直接修改該檔案

MATLAB 程式設計與工程應用

離開 MATLAB

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

MATLAB 程式設計與工程應用

End of Chapter 1