第 1 章 綜觀 matlab
Post on 14-Feb-2016
187 Views
Preview:
DESCRIPTION
TRANSCRIPT
112/04/22 Puff! The magic dragon, live by the sea.
1
第 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
112/04/22 Puff! The magic dragon, live by the sea.
3
Program Langage
• Matlab ( 大小寫是有區分的 !)• Fortran• C++• Virtual Basic• Cobol• Pascal• …
user:
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
112/04/22 Puff! The magic dragon, live by the sea.
5
112/04/22 Puff! The magic dragon, live by the sea.
6
112/04/22 Puff! The magic dragon, live by the sea.
7
Order of precedence
• P.10 examples• P.11 T1.1-1
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
112/04/22 Puff! The magic dragon, live by the sea.
9
• format short• 1/7• format long• 1/7
• help• demo
112/04/22 Puff! The magic dragon, live by the sea.
10
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
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=
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
112/04/22 Puff! The magic dragon, live by the sea.
14
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
112/04/22 Puff! The magic dragon, live by the sea.
16
Long statement …
• See (p.13)
112/04/22 Puff! The magic dragon, live by the sea.
17
for previous statement modification
, , tab
112/04/22 Puff! The magic dragon, live by the sea.
18
Smart recall
• volume=3*5• vol +
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
112/04/22 Puff! The magic dragon, live by the sea.
20
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
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
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).
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)
112/04/22 Puff! The magic dragon, live by the sea.
25
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(.)
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?
112/04/22 Puff! The magic dragon, live by the sea.
28
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!
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
112/04/22 Puff! The magic dragon, live by the sea.
31
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)
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]
112/04/22 Puff! The magic dragon, live by the sea.
34
找出陣列中不為零的元素的索引值• x=[-2, 0, 4]• y=find(x)
• y=[1 3]
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
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)
112/04/22 Puff! The magic dragon, live by the sea.
37
Self testing p.27
• Test by your own, not by computer!
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
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 個位置座標
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)
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)’)
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)
112/04/22 Puff! The magic dragon, live by the sea.
43
112/04/22 Puff! The magic dragon, live by the sea.
44
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!
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
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
112/04/22 Puff! The magic dragon, live by the sea.
48
Self testing p.32
112/04/22 Puff! The magic dragon, live by the sea.
49
Other functions
• Statistics• Numerical analysis & Differential equations• Symbolic Processing
112/04/22 Puff! The magic dragon, live by the sea.
50
Script files & the Editor/Debugger
• 1. Direct command ~ 互動模式• 2. Script files
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 局部變數 )
112/04/22 Puff! The magic dragon, live by the sea.
52
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
112/04/22 Puff! The magic dragon, live by the sea.
54
除錯• 適時去除程式敘述末端的分號
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
112/04/22 Puff! The magic dragon, live by the sea.
56
Document units of measurement
• 單位使用正確
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
112/04/22 Puff! The magic dragon, live by the sea.
58
Conditional statements
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
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
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]
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
112/04/22 Puff! The magic dragon, live by the sea.
63
Self testing p.42
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
112/04/22 Puff! The magic dragon, live by the sea.
65
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
112/04/22 Puff! The magic dragon, live by the sea.
67
Help functions
• help function_name• lookfor topic• doc function_name
112/04/22 Puff! The magic dragon, live by the sea.
68
112/04/22 Puff! The magic dragon, live by the sea.
69
Problem solving methodologies
112/04/22 Puff! The magic dragon, live by the sea.
70
112/04/22 Puff! The magic dragon, live by the sea.
71
112/04/22 Puff! The magic dragon, live by the sea.
72
112/04/22 Puff! The magic dragon, live by the sea.
73
112/04/22 Puff! The magic dragon, live by the sea.
74
112/04/22 Puff! The magic dragon, live by the sea.
75
112/04/22 Puff! The magic dragon, live by the sea.
76
Summary, p.58
112/04/22 Puff! The magic dragon, live by the sea.
77
112/04/22 Puff! The magic dragon, live by the sea.
78
112/04/22 Puff! The magic dragon, live by the sea.
79
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?
112/04/22 Puff! The magic dragon, live by the sea.
81
HW#CH1
• Problems• 2, 5, 9, 20, 23, 25, 30, 31, 35
top related