Top Banner
INTRODUCTION TO INTRODUCTION TO MATLAB MATLAB SHAWN SHAWN NTOU NTOU
47

INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

INTRODUCTION TO INTRODUCTION TO MATLAB MATLAB

SHAWNSHAWN

NTOUNTOU

Page 2: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

What Is MATLAB? What Is MATLAB?

MATLAB® is a high-performance MATLAB® is a high-performance language for technical computing. language for technical computing.

It integrates computation, visualization, It integrates computation, visualization, and programming in an easy-to-use and programming in an easy-to-use environment where problems and environment where problems and solutions are expressed in familiar solutions are expressed in familiar mathematical notation. mathematical notation.

The name MATLAB stands for matrix The name MATLAB stands for matrix laboratory. laboratory.

Page 3: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

What Is MATLAB?What Is MATLAB?

Typical uses includeTypical uses includeMath and computation Math and computation Algorithm development Algorithm development Data acquisition Data acquisition Modeling,simulation, and prototyping Modeling,simulation, and prototyping Data analysis, exploration, and visualization Data analysis, exploration, and visualization Scientific and engineering graphics Scientific and engineering graphics Application development, including graphical Application development, including graphical

user interface building user interface building

Page 4: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

The MATLAB SystemThe MATLAB System

The MATLAB system consists of five main The MATLAB system consists of five main parts:parts:Development Environment. Development Environment. The MATLAB Mathematical Function Library. The MATLAB Mathematical Function Library. The MATLAB Language.The MATLAB Language.Graphics.Graphics.The MATLAB Application Program Interface The MATLAB Application Program Interface

(API).(API).

Page 5: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 6: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Command windowCommand window

Page 7: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Command HistoryCommand History

Page 8: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Help windowHelp window

Page 9: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Work spaceWork space

Page 10: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Array EditorArray Editor

Page 11: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Editor / DebugerEditor / Debuger

Page 12: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Entering vectors and matricesEntering vectors and matrices

Page 13: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Entering vectors and matrices; Entering vectors and matrices;

Page 14: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

built-in variables and functions; helpbuilt-in variables and functions; help

Page 15: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

OperatorsOperators

Page 16: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Generating MatrixGenerating Matrix

Page 17: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Matrix OperatingMatrix Operating

Page 18: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 19: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 20: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 21: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 22: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 23: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 24: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Creating a plotCreating a plot

Page 25: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 26: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 27: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 28: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Adding plot to an exiting graphAdding plot to an exiting graph

Page 29: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Figure windowFigure window

Page 30: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Multiple plot in one figureMultiple plot in one figure

Page 31: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

Flow controlFlow control

Page 32: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 33: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 34: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 35: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 36: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 37: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.
Page 38: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

簡易數學 簡易數學 如果我們要算 如果我們要算 1+2+31+2+3 及及 1×10+2×20+3×301×10+2×20+3×30 這二個式子,這二個式子,

以下例子接著提示符號以下例子接著提示符號 >>>> 之後的是要鍵入的算式,之後的是要鍵入的算式, MATLABMATLAB 將計算的結果以將計算的結果以 ansans 顯示。 顯示。

>> 1+2+3 >> 1+2+3 ans = ans = 6 6 >> 1*10 + 2*20 + 3*30 >> 1*10 + 2*20 + 3*30 ans = ans = 140 140 >> x=1+2+3 >> x=1+2+3 x = x = 6 6

Page 39: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

簡易數學簡易數學 如果在上述的例子結尾加上如果在上述的例子結尾加上 ;; ,則計算結果,則計算結果

不會顯示在指令視窗上,要得知計算值只不會顯示在指令視窗上,要得知計算值只須鍵入該變數值即可 須鍵入該變數值即可

>> x=1+2+3; >> x=1+2+3; >> x >> x x = x = 66

Page 40: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

MATLABMATLAB 的變數使用的變數使用 >> apple=5 >> apple=5 apples = apples = 5 5

>> total_cost=apple*2+orange*4 >> total_cost=apple*2+orange*4 total_cost = total_cost = 50 50

>> average_cost=total_cost/(apple+orange) >> average_cost=total_cost/(apple+orange) average_cost = average_cost = 3.333343.33334

Page 41: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

其他的基本運算其他的基本運算MATLABMATLAB 提供基本的算術運算有: 提供基本的算術運算有:

加 加 (+)(+) 、減 、減 (-)(-) 、乘 、乘 (*)(*) 、除 、除 (/)(/) 、冪次方 、冪次方 (^)(^)範例為:範例為: 5+3, 5-3, 5*3, 5/3, 5^35+3, 5-3, 5*3, 5/3, 5^3

要計算面積要計算面積 Area = ,Area = , 半徑半徑 r r = 2= 2 ,則可鍵入 ,則可鍵入 >> r=2; >> r=2; >> area=pi*r^2; >> area=pi*r^2; >> area = >> area = 12.566412.5664

Page 42: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

其他的基本運算其他的基本運算 我們也可以將上述指令打在同一行,以我們也可以將上述指令打在同一行,以 , , 或是或是 ; ;

分開,例如 分開,例如 >> r=2, area=pi*r^2 >> r=2, area=pi*r^2 >> r=2; area=pi*r^2;>> r=2; area=pi*r^2;

如果一個指令過長可以在結尾加上如果一個指令過長可以在結尾加上 ...... (代表此行(代表此行指令與下一行連續),例如 指令與下一行連續),例如

>> r=2; >> r=2; >> area = pi ... >> area = pi ... *r^2*r^2

Page 43: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

符號註解 符號註解 由由 %% 起頭,也就是說在起頭,也就是說在 %% 之後的任何文字之後的任何文字

都被視為程式的註解。 都被視為程式的註解。 註解的功能是簡要的說明程式的內容,過多的註解的功能是簡要的說明程式的內容,過多的

註解在程式中或許沒有必要,但是我們寫程式註解在程式中或許沒有必要,但是我們寫程式時往往用了太少的註解。 時往往用了太少的註解。

>> r=2; % >> r=2; % 鍵入半徑 鍵入半徑 >> area=pi*r^2; % >> area=pi*r^2; % 計算面積 計算面積

Page 44: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

數字格式數字格式 我們可以在指令視窗上的功能選單上的我們可以在指令視窗上的功能選單上的 OptionsOptions

下選 下選 Numerical FormatNumerical Format ,, 或者直接在指令視窗鍵入以下的各個數字顯示格或者直接在指令視窗鍵入以下的各個數字顯示格

式的指令,式的指令, 以以 ππ 值為例 指令數字值說明值為例 指令數字值說明

format shortformat short 3.14163.1416 預設的 預設的 4 4 位有效小位有效小數位數 數位數

format longformat long 3.141592653589793.14159265358979 15 15 位有效小數位數 位有效小數位數 format short eformat short e 3.1416e+0003.1416e+000 4 4 位有效小位有效小數位數加上指數表格式 數位數加上指數表格式

Page 45: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

MATLAB MATLAB 變數變數 MATLABMATLAB 對使用變數名稱的規定: 對使用變數名稱的規定:

變數名稱的英文大小寫是有區別的(變數名稱的英文大小寫是有區別的( apple, Apple, AppLeapple, Apple, AppLe ,三個變數不,三個變數不同)。 同)。

變數的長度上限為 變數的長度上限為 19 19 個字元。 個字元。 變數名的第一個字必須是一英文字,隨後可以摻雜英文字、數字或是底變數名的第一個字必須是一英文字,隨後可以摻雜英文字、數字或是底

線。線。 以下列出以下列出 MATLABMATLAB 所定義的特別變數及其意義 變數名意義所定義的特別變數及其意義 變數名意義

helphelp 線上說明線上說明 , , 如 如 help quit help quit whowho 列出所有定義過的變數名稱 列出所有定義過的變數名稱 ansans 預設的計算結果的變數名 預設的計算結果的變數名 epsMATLABepsMATLAB 定義的正的極小值定義的正的極小值 =2.2204e-16 =2.2204e-16 Pi Pi 內建的內建的 ππ 值 值 inf∞inf∞ 值,無限大 值,無限大 ( ) ( ) NaNNaN 無法定義一個數目 無法定義一個數目 ( ) ( )

Page 46: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

其它功能 其它功能 當要暫時執行作業系統(例如當要暫時執行作業系統(例如 DosDos )的指令而還)的指令而還

要執行要執行 MATLABMATLAB ,可以利用,可以利用 !! 加上原作業系統的加上原作業系統的指令,指令, 例如 例如 !dir, !format a:!dir, !format a: 。。

鍵入鍵入 whowho 可以查看所有定義過的變數名稱。可以查看所有定義過的變數名稱。 鍵入鍵入 clearclear 則是去除所有定義過的變數名稱;則是去除所有定義過的變數名稱;

如果只是要去除如果只是要去除 xx 及及 y y 二個變數則可以鍵入二個變數則可以鍵入 clear x yclear x y 。。

Page 47: INTRODUCTION TO MATLAB SHAWNNTOU. What Is MATLAB? MATLAB® is a high-performance language for technical computing. MATLAB® is a high-performance language.

線上說明 線上說明 在在 MATLABMATLAB 系統中相關的線上系統中相關的線上 (on-line)(on-line) 求助方式求助方式

有三: 有三: 是利用是利用 helphelp 指令,如果你已知要找的題材 指令,如果你已知要找的題材 (topic) (topic) 為為

何的話,直接鍵入何的話,直接鍵入 help <topic>help <topic> 。所以即使身旁沒有使。所以即使身旁沒有使用手冊,也可以使用用手冊,也可以使用 helphelp 指令查詢不熟悉的指令或是指令查詢不熟悉的指令或是題材之用法,例如題材之用法,例如 help sqrt, help topichelp sqrt, help topic 。 。

是利用是利用 lookforlookfor 指令,它可以從你鍵入的關鍵字指令,它可以從你鍵入的關鍵字 (key-w(key-word)ord) (即始這個關鍵字並不是(即始這個關鍵字並不是 MATLABMATLAB 的指令)列出的指令)列出所有相關的題材,例如所有相關的題材,例如 lookfor cosine, lookfor sinelookfor cosine, lookfor sine 。 。

是利用指令視窗的功能選單中的是利用指令視窗的功能選單中的 HelpHelp ,從中選取,從中選取 TablTable of Contentse of Contents (目錄)或是(目錄)或是 IndexIndex (索引)。 (索引)。