81
111/05/06 Puff! The magic dragon, live by the sea. 1 第1第 綜綜 MATLAB

第 1 章 綜觀 MATLAB

  • Upload
    hea

  • View
    187

  • Download
    0

Embed Size (px)

DESCRIPTION

第 1 章 綜觀 MATLAB. Text book. Introduction to Matlab 6 for Engineering William J. Palm III Mc Graw Hill Matlab 6 在工程上的應用 -Matlab 6.5 Update 呂明和 黃逸群 譯 普林斯頓國際有限公司 CG 001. user:. Program Langage. Matlab ( 大小寫是有區分的 !) Fortran C++ Virtual Basic Cobol Pascal …. windows. - PowerPoint PPT Presentation

Citation preview

Page 1: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

1

第 1章 綜觀 MATLAB

Page 2: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

2

Text book

• Introduction to Matlab 6 for Engineering• William J. Palm III• Mc Graw Hill

• Matlab 6 在工程上的應用 -Matlab 6.5 Update• 呂明和 黃逸群 譯• 普林斯頓國際有限公司 CG 001

Page 3: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

3

Program Langage

• Matlab ( 大小寫是有區分的 !)• Fortran• C++• Virtual Basic• Cobol• Pascal• …

user:

Page 4: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

4

windows

• Launch pad window 啟動台視窗• Command window 命令視窗• Command history window 命令記錄視窗• Editor/Debugger window• Graph window• Tool bar• Current directory

Page 5: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

5

Page 6: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

6

Page 7: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

7

Order of precedence

• P.10 examples• P.11 T1.1-1

Page 8: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

8

P.11 T1.1-1

• (a) 6+10/13+18/(5*7)+5*9^2• (b) 6*(35^(1/4))+14^0.35

Page 9: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

9

• format short• 1/7• format long• 1/7

• help• demo

Page 10: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

10

Page 11: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

11

The assignment operator ( 指定運算子 ) or replacement operator ( 替換運算子 )

• x=3 ~ x3• x=x+2 ~ xx+2

• Error expression 6=x• x=6 is the right expression

• What about x=5+y ~ x5+y

Page 12: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

12

國中應用問題 p.12

• pie=3.1416;• r=8;• h=15;• V=pie*r^2*h;• V=V+0.20*V;• r=sqrt(V/(pie*h))• ans=

Page 13: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

13

Variable names

• <32 characters; begin with alpha..• e.g.

right expression: speed_12345wrong expression: 1aaaaa

Page 14: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

14

Page 15: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

15

whos ( 暫存變數 )

• x=2; y=6+x; x=y+7• x=2; x=2, y=8; x=15, y=8

• who• whos • whos x• Exist (‘var1’) ~ 1 in use, 0 not exist

Page 16: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

16

Long statement …

• See (p.13)

Page 17: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

17

for previous statement modification

, , tab

Page 18: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

18

Smart recall

• volume=3*5• vol +

Page 19: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

19

editing

• Del, backspace• Ctrl+k, Esc ~ clear all line• Clear ~ clear all variables• Clear var1, var2,.. ~ clear the 2 variables• Kill

Page 20: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

20

Page 21: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

21

Complex operation

• P.16,

s=3+7i; w=5-9i;w+sw*sw/s

Page 22: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

22

Menus & the toolbar

• The desktop menus• The File Menu in Matlab 6

set path• The Edit Menu in Matlab 6

Page 23: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

23

Computing with Matlab

• Arrays 內建函數 sin(x), cos(x), log(x), log10(x), exp(x), asin(x).

Page 24: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

24

Computing with Matlab

• x=[0 1 3 6]; y=[6 3 1 0];z=x+y=[6 4 4 6]

• u=[0:.1:10]; w=5*sin(u);u(7)w(7)length(w)length(u)

Page 25: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

25

Page 26: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

26

Polynomial roots

• a=[1 –7 40 –34];roots(a)

• r=[1, 3+5i, 3-5i];poly(r)

• or poly([1, 3+5i, 3-5i])• See also fzero(.)

Page 27: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

27

Self testing

• u=[cos(0): 0.02: log10(100)]length(u)u(25)

• a=[1 6 -11 290]r=roots(a)b=poly(r)

• Check a & b, are they the same?

Page 28: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

28

Page 29: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

29

Working with files

• I/O• Set path… /homework/• problem1.m1. Is ‘problem1’ a variable?2. Is ‘problem1’ a pre-constructed command?3. Is problem1.m in the present directory?4. Find it in the search path directory!

Page 30: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

30

Show the present working directory & *.m files

• pwd• dir C:\MATLAB\BIN• what C:\MATLAB\BIN

• pathtool• add path• rmpath

Page 31: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

31

Page 32: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

32

Relational operators

• x=[6 3 9]• y=[14 2 9]• z=(x<y)• z=(x~ =y)• z=(x==y)• z=x(x>8)

• Boolean operator (‘yes’=1, ‘no’=0)

Page 33: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

33

Relational operators

• x=[6 3 9 11]• y=[14 2 9 13]• z=x(x<y)• 找出陣列 x 中每一個小於陣列 y 中相對元素所的相對元素 , 所得的陣列 z=[6, 11]

Page 34: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

34

找出陣列中不為零的元素的索引值• x=[-2, 0, 4]• y=find(x)

• y=[1 3]

Page 35: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

35

比較• x=[6 3 9 11]; y=[14 2 9 13]• z1=x(x<y); z2=y(x<y); z3=find(x<y); • z1• z2• z3• Length(z3) • z1, z2 give the values; while z3 give the

indices

Page 36: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

36

Example p.26

• A=[55, 62, 60, 61, 63, 65, 62, 59, 58, 56];• B=[54, 59, 62, 64, 68, 68, 62, 59, 57, 53];• when=find(A>60)• how_many1=length(when)• above=A(A>B)• how_many1=length(above)

Page 37: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

37

Self testing p.27

• Test by your own, not by computer!

Page 38: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

38

plot

• x=[0: 0.01:10];• y=sin(2*x);• plot(x,y), xlabel(‘x’), ylabel(‘sin(2x)’)

• Use help plot to understand the above statement! See also plot, label, title, gtext

• Graphic window

Page 39: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

39

Overlay plot 重疊繪圖• x=[0: .01: 5];• y=2*sqrt(x);• z=4*sin(3*x); • plot(x,y,x,z), xlabel('x'), gtext('y'), gtext('z')• gtext ~ 指定好游標位置按下滑鼠鍵 , 以放置 gtext 所要放置的文字• [u,v]=ginput(n) ~ 滑鼠點下的 n 個位置座標

Page 40: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

40

Overlay plot 重疊繪圖• x=[0: .01: 5];• y=2*sqrt(x);• z=4*sin(3*x); • plot(x,y,x,z), xlabel('x'), gtext('y'), gtext('z') • [u,v]=ginput(n)

Page 41: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

41

data marker

• x=[15: 2: 23];• y=[20 50 60 90 70];• plot(x,y,’+’,x,y), xlabel(‘x (seconds)’),

ylabel(‘y (volts)’)

Page 42: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

42

Plotting Polynomials

• a=[9, -5, 3, 7];• x=[-2: .01:5];• f=polyval(a,x); • plot(x,f), xlabel('x'), ylabel('f(x)'), grid

• f(x)=9x^3-5x^2+3x+7• find f(0), f(2), …,f(10)

Page 43: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

43

Page 44: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

44

Page 45: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

45

Self testing p.31

• Write down the statements on a blank paper, and try to use your brain & this book to ‘run’ this program!

Page 46: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

46

Linear Algebraic Equations

6x+12y+4z=707x-2y+3z=52x+8y-9z=64Ax=Bx=A\B• A=[6,12,4; 7,-2,3; 2,8,-9];• B=[70; 5; 64];• A\B• A/B

Page 47: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

47

Linear Algebraic Equations

6x+12y+4z=707x-2y+3z=52x+8y-9z=64Ax=Bx=A-1B therefore we use A\B• A=[6,12,4; 7,-2,3; 2,8,-9]; 3 by 3• B=[70; 5; 64]; 3 by 1• A\B• A/B

Page 48: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

48

Self testing p.32

Page 49: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

49

Other functions

• Statistics• Numerical analysis & Differential equations• Symbolic Processing

Page 50: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

50

Script files & the Editor/Debugger

• 1. Direct command ~ 互動模式• 2. Script files

Page 51: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

51

2 types of M-files

• 1. Script files (global variables 全域變數 )• 2. Function files (local variables 局部變數 )

Page 52: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

52

Page 53: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

53

有效的使用草稿檔• P.35 1. kscript.m ~ start with alph.2. kscript should not be the same as any one of t

he the global variables in this script file.3. exist(‘kscript’)4. who5. type kscript

Page 54: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

54

除錯• 適時去除程式敘述末端的分號

Page 55: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

55

Programming style

1. Comment section• (a) filename & keywords in the 1st line• (b) date & author in the 2nd line• (c) Input/output variables definitions• (d) call functions2. Input section3. Calculation section4. Output section

Page 56: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

56

Document units of measurement

• 單位使用正確

Page 57: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

57

Using script files to store data

• % mydata.m : 儲存溫度資料• % 儲存包含華氏刻度的各種溫度之陣列 tem

p_F• temp_F=[72 68 75 77 83 79]

• temp_C=5*(temp_F-32)/9

Page 58: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

58

Conditional statements

Page 59: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

59

Conditional statements

if x>= 25y=50*sqrt(x)elseif x>=0y=10*xelsey=0end

25;50

250;100;0

xxy

xxyxy

Page 60: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

60

Loops

m=0;x(1)=10;for k=2:3:11m=m+1;x(m+1)=x(m)+k^2;

We will havex(2)=14, x(3)=39, x(4)=103, x(5)=224

Page 61: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

61

x=5;k=0;while x<25k=k+1;y(k)=3*x;x=2*x-1;

We will have x=[9 17 33]; y=[15 27 51]

Page 62: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

62

Example p.41

amount=500;k=0;while amount < 10000k=k+1;amount=amount*1.05+500;endamountk

Page 63: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

63

Self testing p.42

Page 64: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

64

The Matlab help system

1. Help browser2. Help functions ~ help, lookfor, doc3. Other resources ~ demo

Page 65: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

65

Page 66: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

66

• Viewing documentation• Using the contents tab• Using the index tab• Using the search tab• Using the favorites tab

Page 67: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

67

Help functions

• help function_name• lookfor topic• doc function_name

Page 68: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

68

Page 69: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

69

Problem solving methodologies

Page 70: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

70

Page 71: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

71

Page 72: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

72

Page 73: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

73

Page 74: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

74

Page 75: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

75

Page 76: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

76

Summary, p.58

Page 77: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

77

Page 78: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

78

Page 79: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

79

Page 80: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

80

HW#CH1

• 1. Read the following statements, and (a) tell me what are they doing?a=1; b=2; c=3;u=[a b c];x=roots(u);(b) try to change a,b,c with other values; what happen about the x vector?

Page 81: 第 1 章   綜觀 MATLAB

112/04/22 Puff! The magic dragon, live by the sea.

81

HW#CH1

• Problems• 2, 5, 9, 20, 23, 25, 30, 31, 35