Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

II.

Introduction of MATLAB
and SIMULINK

PME3005 System Dynamics and Analysis 1

To Start
To start the MATLAB, you need to double-click
the icon on desktop twice. A MATLAB window
will display three areas: command window, work
space browser, and command history.
The command window is the main window where
you key in the MATLAB command after the
prompt >>.

1. help command will list the commands installed.


2. help topic command will display the usage of
the particular MATLAB topic or syntax.
3. demo command is used to bring up a demo window.

PME3005 System Dynamics and Analysis 2

1
User Interface

PME3005 System Dynamics and Analysis 3

User Interface
A set of tool bar on the top perform the following functions.

PME3005 System Dynamics and Analysis 4

2
The Language

• Shell-Like
• Interpreter
• M-File
– Batch-Like
– Control Loop Support

PME3005 System Dynamics and Analysis 5

Data Classes

PME3005 System Dynamics and Analysis 6

3
Data Classes
Note:
MATLAB將所有變數均存成double的形式,
所以不需經過變數宣告(Variable
declaration)。MATLAB同時也會自動進行
記憶體的使用和回收,而不必像C語言,必
須由使用者一一指定。這些功能使得
MATLAB易學易用,使用者可專心致力於
撰寫程式,而不必被軟體枝節問題所干擾

PME3005 System Dynamics and Analysis 7

Arithmetic Operators (1)

PME3005 System Dynamics and Analysis 8

4
Arithmetic Operators (2)

PME3005 System Dynamics and Analysis 9

Arithmetic Operators (3)

若不想讓MATLAB每次都顯示運算結果,
只需在運算式最後加上分號(;)即可,如
下例:

y = sin(10)*exp(-0.3*4^2);

若要顯示變數y的值,直接鍵入y即可:

>>y

y =
-0.0045

PME3005 System Dynamics and Analysis 10

5
Arithmetic Operators (4)
MATLAB常用的基本數學函數
abs(x):純量的絕對值或向量的長度
angle(z):複數z的相角(Phase angle)
sqrt(x):開平方
real(z):複數z的實部
imag(z):複數z的虛部
conj(z):複數z的共軛複數
round(x):四捨五入至最近整數
fix(x):無論正負,捨去小數至最近整數
floor(x):地板函數,即捨去正小數至最近整數
ceil(x):天花板函數,即加入正小數至最近整數
rat(x):將實數x化為分數表示
rats(x):將實數x化為多項分數展開
sign(x):符號函數 (Signum function)。
當x<0時,sign(x)=-1;
當x=0時,sign(x)=0;
當x>0時,sign(x)=1。
rem(x,y):求x除以y的餘數

exp(x):自然指數 pow2(x):2的指數 log(x):以e為底的對數,即自然對數或

log2(x):以2為底的對數 log10(x):以10為底的對數

PME3005 System Dynamics and Analysis 11

Arithmetic Operators (5)


MATLAB常用的三角函數

sin(x):正弦函數
cos(x):餘弦函數
tan(x):正切函數
asin(x):反正弦函數
acos(x):反餘弦函數
atan(x):反正切函數
atan2(x,y):四象限的反正
切函數
sinh(x):超越正弦函數
cosh(x):超越餘弦函數
tanh(x):超越正切函數
asinh(x):反超越正弦函數
acosh(x):反超越餘弦函數
atanh(x):反超越正切函數
PME3005 System Dynamics and Analysis 12

6
Arithmetic Operators (6)
變數也可用來存放向量或矩陣,並進行各種運算,
如下例的列向量(Row vector)運算:

x = [1 3 5 2];
y = 2*x+1
y =
3 7 11 5
Note:變數命名的規則

1. 第一個字母必須是英文字母
2. 字母間不可留空格
3. 最多只能有19個字母,MATLAB會忽
略多餘字母
PME3005 System Dynamics and Analysis 13

Arithmetic Operators (7)


我們可以隨意更改、增加或刪除向量的元素:

y(3) = 2 % 更改第三個元素
y =
3 7 2 5
y(6) = 10 % 加入第六個元素
y =
3 7 2 5 0 10
y(4) = [] % 刪除第四個元素
y =
3 7 2 0 10

在上例中,MATLAB會忽略所有在百分比符號
(%)之後的文字,因此百分比之後的文字均可
視為程式的註解(Comments)。
PME3005 System Dynamics and Analysis 14

7
Arithmetic Operators (8)
MATLAB亦可取出向量的一個元素或一部份來做運算:

x(2)*3+y(4) % 取出x的第二個元素和y的第四個元素來
做運算
ans =

y(2:4)-1 % 取出y的第二至第四個元素來做運算

ans =
6 1 -1
在上例中,2:4代表一個由2、3、4組成的向量

PME3005 System Dynamics and Analysis 15

Arithmetic Operators (9)


MATLAB亦可取出向量的一個元素或一部份來做運算:

x(2)*3+y(4) % 取出x的第二個元素和y的第四個元素來
做運算
ans =

y(2:4)-1 % 取出y的第二至第四個元素來做運算

ans =
6 1 -1
在上例中,2:4代表一個由2、3、4組成的向量

PME3005 System Dynamics and Analysis 16

8
Arithmetic Operators (10)
等差數列:
x = 7:16
x =
7 8 9 10 11 12 13 14 15 16
若不希望公差為1,則可將所需公差直接至於4與13之間:
x = 7:3:16 % 公差為3的等差數列
x =
7 10 13 16
事實上,我們可利用linspace來產生任意的等差數列:
x = linspace(4, 10, 6) % 等差數列:首項為4,末
項為10,項數為6

x =
4.0000 5.2000 6.4000 7.6000 8.8000 10.0000

PME3005 System Dynamics and Analysis 17

Arithmetic Operators (11)


若要輸入矩陣,則必須在每一列結尾加上分號
(;),如下例:

A = [1 2 3 4; 5 6 7 8; 9 10 11 12];

A =
1 2 3 4
5 6 7 8
9 10 11 12

PME3005 System Dynamics and Analysis 18

9
Arithmetic Operators (12)
A(2,3) = 5 % 改變位於第二列,第三行的元素值

A =
1 2 3 4
5 6 5 8
9 10 11 12

B = A(2,1:3) % 取出部份矩陣B

B =
5 6 5

A = [A B'] % 將B轉置後以行向量併入A

A =
1 2 3 4 5
5 6 5 8 6
9 10 11 12 5
PME3005 System Dynamics and Analysis 19

Arithmetic Operators (13)


B = A(2,1:3) % 取出部份矩陣B

B =
5 6 5

A = [A B'] % 將B轉置後以行向量併入A

A =
1 2 3 4 5
5 6 5 8 6
9 10 11 12 5

PME3005 System Dynamics and Analysis 20

10
Arithmetic Operators (14)
A(:, 2) = [] % 刪除第二行(:代表所有列)

A =
1 3 4 5
5 5 8 6
9 11 12 5

A = [A; 4 3 2 1] % 加入第四列

A =
1 3 4 5
5 5 8 6
9 11 12 5
4 3 2 1
PME3005 System Dynamics and Analysis 21

Arithmetic Operators (15)


A([1 4], :) = [] % 刪除第一和第四列(:代表所有行)

A =

5 5 8 6
9 11 12 5

B = reshape(A, 4, 2) % 4是新矩陣的列數,2是新矩陣
的行數
B =
5 8
9 12
5 6
11 5
PME3005 System Dynamics and Analysis 22

11
Arithmetic Operators (16)
MATLAB可在同時執行數個命令,只要以逗號或分號將
命令隔開:

x = sin(pi/3); y = x^2; z = y*10,


z =

7.5000

若一個數學運算是太長,可用三個句點將其延伸到下一
行:

z = 10*sin(pi/3)* ...
sin(pi/3);

PME3005 System Dynamics and Analysis 23

Arithmetic Operators (17)

MATLAB的永久常數

i或j:基本虛數單位(即 )
eps:系統的浮點(Floating-point)精確度
inf:無限大, 例如1/0
nan或NaN:非數值(Not a number),例如0/0
pi:圓周率 (= 3.1415926...)
realmax:系統所能表示的最大數值
realmin:系統所能表示的最小數值
nargin: 函數的輸入引數個數
nargout: 函數的輸出引數個數

PME3005 System Dynamics and Analysis 24

12
Arithmetic Operators (18)

PME3005 System Dynamics and Analysis 25

Relation Operators

PME3005 System Dynamics and Analysis 26

13
Logical Operators and Functions

PME3005 System Dynamics and Analysis 27

Flow Controls

PME3005 System Dynamics and Analysis 28

14
For Statements

• Examples:

x = zeros(1,6); % x是一個1x6的零矩陣
for i = 1:6,
x(i) = 1/i;
end
Note:預先配置矩陣 在上面的例子,我們使用zeros來預先配置
(Allocate)了一個適當大小的矩陣。若不預先配置矩
陣,程式仍可執行,但此時MATLAB需要動態地增加
(或減小)矩陣的大小,因而降低程式的執行效率。所
以在使用一個矩陣時,若能在事前知道其大小,則最好
先使用zeros或ones等命令來預先配置所需的記憶體(即
矩陣)大小。
PME3005 System Dynamics and Analysis 29

if and while Statements

• Examples:

if I==J I = 1;
A(I,J) = 2; while I <= 100
elseif abs(I-J)== 1 sum = sum + I;
A(I,J) = -1; I = I + 1;
else end
A(I,J) = 0;
end

PME3005 System Dynamics and Analysis 30

15
case Statements

• Example:

switch szOption
case {'linear', 'bilinear'}
% do something...
case 'cubic'
% do something...
otherwise
% do something...
end

PME3005 System Dynamics and Analysis 31

plot Statements
plot是繪製一維曲線的基本函數,但在使用此函數之前,我們
需先定義曲線上每一點的x及y座標。下例可畫出一條正弦曲
線:

close all; x=linspace(0, 2*pi, 100); % 100個


點的x座標
y=sin(x); % 對應的y座標
plot(x,y);

MATLAB基本繪圖函數
plot: x軸和y軸均為線性刻度(Linear scale)
loglog: x軸和y軸均為對數刻度(Logarithmic scale)
semilogx: x軸為對數刻度,y軸為線性刻度
semilogy: x軸為線性刻度,y軸為對數刻度
PME3005 System Dynamics and Analysis 32

16
plot Statements

PME3005 System Dynamics and Analysis 33

plot Statements
若要畫出多條曲線,只需將座標對依次放入plot函
數即可:
plot(x, sin(x), x, cos(x));

PME3005 System Dynamics and Analysis 34

17
plot Statements
若要改變顏色,在座標對後面加上相關字串即可:
plot(x, sin(x), 'c', x, cos(x), 'g');

PME3005 System Dynamics and Analysis 35

plot Statements
若要同時改變顏色及圖線型態(Line style),也是在
座標對後面加上相關字串即可:
plot(x, sin(x), 'co', x, cos(x), 'g*');

PME3005 System Dynamics and Analysis 36

18
plot Statements

plot繪圖函數的參數
字元 顏色 字元 圖線型態
y 黃色 . 點
k 黑色 o 圓
w 白色 x x
b 藍色 + +
g 綠色 * *
r 紅色 - 實線
c 亮青色 : 點線
m 錳紫色 -. 點虛線
-- 虛線

PME3005 System Dynamics and Analysis 37

plot Statements
圖形完成後,我們可用
axis([xmin,xmax,ymin,ymax])函數來調整圖軸的
範圍:
axis([0, 6, -1.2, 1.2]);

PME3005 System Dynamics and Analysis 38

19
mesh Statements
mesh和plot是三度空間立體繪圖的基本命令,mesh可
畫出立體網狀圖,plot則可畫出立體曲面圖,兩者產
生的圖形都會依高度而有不同顏色。下列命令可畫出由
函數 z=xe -(x2+y2)形成的立體網狀圖:
x=linspace(-2, 2, 25); % 在x軸上取25點
y=linspace(-2, 2, 25); % 在y軸上取25點

[xx,yy]=meshgrid(x, y); % xx和yy都是


%21x21的矩陣

zz=xx.*exp(-xx.^2-yy.^2); % 計算函數值,
%zz也是21x21的矩陣

mesh(xx, yy, zz); % 畫出立體網狀圖


PME3005 System Dynamics and Analysis 39

mesh Statements

PME3005 System Dynamics and Analysis 40

20
surface Statements
surf和mesh的用法類似:

x=linspace(-2, 2, 25); % 在x軸上取25點


y=linspace(-2, 2, 25); % 在y軸上取25點

[xx,yy]=meshgrid(x, y); % xx和yy都是21x21的矩


% 陣

zz=xx.*exp(-xx.^2-yy.^2); % 計算函數值,zz也是
% 21x21的矩陣

surf(xx, yy, zz); % 畫出立體曲面圖

PME3005 System Dynamics and Analysis 41

surface Statements

PME3005 System Dynamics and Analysis 42

21
SIMULINK
SIMULINK is a block orientated program that allows one to
simulate dynamic systems in a block diagram form at whether
they are linear or nonlinear, in continuous or discrete
forms.

To invoke the SIMULINK programme, follow


these steps:

☆Start MATLAB, by double clicking on the


MATLAB icon.
☆ In the MATLAB command window, type
simulink<CR>

 The following window should now appear


PME3005 System Dynamics and Analysis 43

SIMULINK

Select New from the File menu. This creates


a new work space where the block diagram
for the system will be created.

PME3005 System Dynamics and Analysis 44

22
SIMULINK Example-1
1
Let’
s use a transfer function s 1 as an example
1. From SIMULINK library browser , hit File then New,
and choose model create a new workspace.
2. Again in the browser, clink on source to choose a
step input block and drag to the workspace.
3. Choose continuous, then select Transfer Fcn
and drag to the workspace.
4. Choose Signal routing, then select MUX to create dual
signal rout.
5. Choose Math operations, then select sum and
insert between input and transfer fcn. Double click on
the sum block to change sign listing to +-.
6. Choose Sinks and select scope for output display.
PME3005 System Dynamics and Analysis 45

SIMULINK Example-2
7. Connect all the signal need as follows. You will have

8. From new work space, choose Simulation


then Start.
9. Hit the Scope to show the simulation result.

PME3005 System Dynamics and Analysis 46

23
SIMULINK Example-3

PME3005 System Dynamics and Analysis 47

SIMULINK Example-4
You may change the Y-axis scale, by right click on scope
window, then select axis property.

PME3005 System Dynamics and Analysis 48

24
SIMULINK Example-5
You may change the Transfer Fcn by left click on Transfer Fcn
and then replace [1 1 0]by [1 1].

PME3005 System Dynamics and Analysis 49

Chapter Review
1. To create a m-file to be executed in MATLAB browser
window.
2. The basic functions and commands of MATLAB
3. To create a SIMULINK model .mdl file for simulation.

PME3005 System Dynamics and Analysis 50

25

You might also like