??? ??MATLAB by t5p8903

VIEWS: 0 PAGES: 81

									            第1章 綜觀MATLAB




2012/5/27      Puff! The magic dragon,   1
                    live by the sea.
                 Text book
• Introduction to Matlab 6 for Engineering
• William J. Palm III
• Mc Graw Hill

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

2012/5/27         Puff! The magic dragon,    2
                       live by the sea.
user:


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

    2012/5/27      Puff! The magic dragon,   3
                        live by the sea.
               windows
•   Launch pad window 啟動台視窗
•   Command window 命令視窗
•   Command history window 命令記錄視窗
•   Editor/Debugger window
•   Graph window
•   Tool bar
•   Current directory

2012/5/27      Puff! The magic dragon,   4
                    live by the sea.
2012/5/27   Puff! The magic dragon,   5
                 live by the sea.
2012/5/27   Puff! The magic dragon,   6
                 live by the sea.
            Order of precedence
• P.10 examples
• P.11 T1.1-1




2012/5/27         Puff! The magic dragon,   7
                       live by the sea.
              P.11 T1.1-1

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




2012/5/27       Puff! The magic dragon,   8
                     live by the sea.
•   format short
•   1/7
•   format long
•   1/7

• help
• demo

2012/5/27          Puff! The magic dragon,   9
                        live by the sea.
2012/5/27   Puff! The magic dragon,   10
                 live by the sea.
 The assignment operator (指定運算子) or
   replacement operator (替換運算子)
• x=3 ~ x3
• x=x+2 ~ xx+2

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

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

2012/5/27         Puff! The magic dragon,   11
                       live by the sea.
            國中應用問題 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=

2012/5/27           Puff! The magic dragon,   12
                         live by the sea.
              Variable names
• <32 characters; begin with alpha..
• e.g.
  right expression: speed_12345
  wrong expression: 1aaaaa




2012/5/27         Puff! The magic dragon,   13
                       live by the sea.
2012/5/27   Puff! The magic dragon,   14
                 live by the sea.
              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

2012/5/27               Puff! The magic dragon,   15
                             live by the sea.
            Long statement …
• See (p.13)




2012/5/27       Puff! The magic dragon,   16
                     live by the sea.
              for previous statement
                 modification
• , , tab




2012/5/27         Puff! The magic dragon,   17
                       live by the sea.
                Smart recall
• volume=3*5
• vol +




2012/5/27      Puff! The magic dragon,   18
                    live by the sea.
                       editing
•   Del, backspace
•   Ctrl+k, Esc ~ clear all line
•   Clear ~ clear all variables
•   Clear var1, var2,.. ~ clear the 2 variables
•   Kill




2012/5/27            Puff! The magic dragon,      19
                          live by the sea.
2012/5/27   Puff! The magic dragon,   20
                 live by the sea.
            Complex operation
• P.16,

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


2012/5/27         Puff! The magic dragon,   21
                       live by the sea.
            Menus & the toolbar
• The desktop menus
• The File Menu in Matlab 6
  set path
• The Edit Menu in Matlab 6




2012/5/27        Puff! The magic dragon,   22
                      live by the sea.
            Computing with Matlab
• Arrays
  內建函數
  sin(x), cos(x),
  log(x), log10(x),
  exp(x), asin(x).




2012/5/27         Puff! The magic dragon,   23
                       live by the sea.
            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)
2012/5/27         Puff! The magic dragon,   24
                       live by the sea.
2012/5/27   Puff! The magic dragon,   25
                 live by the sea.
             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(.)


2012/5/27          Puff! The magic dragon,   26
                        live by the sea.
                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?

2012/5/27        Puff! The magic dragon,   27
                      live by the sea.
2012/5/27   Puff! The magic dragon,   28
                 live by the sea.
             Working with files
•     I/O
•     Set path… /homework/
•     problem1.m
1.    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!

2012/5/27          Puff! The magic dragon,       29
                        live by the sea.
Show the present working directory
           & *.m files
• pwd
• dir C:\MATLAB\BIN
• what C:\MATLAB\BIN

• pathtool
• add path
• rmpath

2012/5/27     Puff! The magic dragon,   30
                   live by the sea.
2012/5/27   Puff! The magic dragon,   31
                 live by the sea.
            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)
2012/5/27        Puff! The magic dragon,   32
                      live by the sea.
            Relational operators
•   x=[6 3 9 11]
•   y=[14 2 9 13]
•   z=x(x<y)
•   找出陣列x中每一個小於陣列y中相對元素
    所的相對元素, 所得的陣列z=[6, 11]




2012/5/27        Puff! The magic dragon,   33
                      live by the sea.
  找出陣列中不為零的元素的索引值
• x=[-2, 0, 4]
• y=find(x)

• y=[1 3]




2012/5/27        Puff! The magic dragon,   34
                      live by the sea.
                         比較
•   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

2012/5/27           Puff! The magic dragon,     35
                         live by the sea.
                 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)


2012/5/27           Puff! The magic dragon,       36
                         live by the sea.
            Self testing p.27
• Test by your own, not by computer!




2012/5/27        Puff! The magic dragon,   37
                      live by the sea.
                          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

2012/5/27           Puff! The magic dragon,       38
                         live by the sea.
            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個位置座標

2012/5/27            Puff! The magic dragon,         39
                          live by the sea.
            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)




2012/5/27             Puff! The magic dragon,            40
                           live by the sea.
                 data marker
• x=[15: 2: 23];
• y=[20 50 60 90 70];
• plot(x,y,’+’,x,y), xlabel(‘x (seconds)’),
  ylabel(‘y (volts)’)




2012/5/27          Puff! The magic dragon,    41
                        live by the sea.
             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)

2012/5/27             Puff! The magic dragon,      42
                           live by the sea.
2012/5/27   Puff! The magic dragon,   43
                 live by the sea.
2012/5/27   Puff! The magic dragon,   44
                 live by the sea.
             Self testing p.31
• Write down the statements on a blank paper,
  and try to use your brain & this book to ‘run’
  this program!




2012/5/27         Puff! The magic dragon,          45
                       live by the sea.
            Linear Algebraic Equations
6x+12y+4z=70
7x-2y+3z=5
2x+8y-9z=64
Ax=Bx=A\B
• A=[6,12,4; 7,-2,3; 2,8,-9];
• B=[70; 5; 64];
• A\B
• A/B
2012/5/27           Puff! The magic dragon,   46
                         live by the sea.
            Linear Algebraic Equations
6x+12y+4z=70
7x-2y+3z=5
2x+8y-9z=64
Ax=Bx=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
2012/5/27           Puff! The magic dragon,   47
                         live by the sea.
            Self testing p.32




2012/5/27       Puff! The magic dragon,   48
                     live by the sea.
             Other functions
• Statistics
• Numerical analysis & Differential equations
• Symbolic Processing




2012/5/27         Puff! The magic dragon,       49
                       live by the sea.
 Script files & the Editor/Debugger
• 1. Direct command ~ 互動模式
• 2. Script files




2012/5/27     Puff! The magic dragon,   50
                   live by the sea.
            2 types of M-files
• 1. Script files (global variables全域變數)
• 2. Function files (local variables局部變數)




2012/5/27       Puff! The magic dragon,     51
                     live by the sea.
2012/5/27   Puff! The magic dragon,   52
                 live by the sea.
            有效的使用草稿檔
• P.35
1. kscript.m ~ start with alph.
2. kscript should not be the same as any one of
   the the global variables in this script file.
3. exist(‘kscript’)
4. who
5. type kscript

2012/5/27         Puff! The magic dragon,          53
                       live by the sea.
                 除錯
• 適時去除程式敘述末端的分號




2012/5/27   Puff! The magic dragon,   54
                 live by the sea.
            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 functions
2. Input section
3. Calculation section
4. Output section
2012/5/27         Puff! The magic dragon,   55
                       live by the sea.
    Document units of measurement
• 單位使用正確




2012/5/27     Puff! The magic dragon,   56
                   live by the sea.
       Using script files to store data
• % mydata.m : 儲存溫度資料
• %儲存包含華氏刻度的各種溫度之陣列
  temp_F
• temp_F=[72 68 75 77 83 79]



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

2012/5/27         Puff! The magic dragon,   57
                       live by the sea.
            Conditional statements




2012/5/27         Puff! The magic dragon,   58
                       live by the sea.
            Conditional statements

if x>= 25          y  0; x  0
y=50*sqrt(x)      
elseif x>=0        y  10 x; 0  x  25
y=10*x            
else               y  50 x ; x  25
y=0
end
2012/5/27         Puff! The magic dragon,   59
                       live by the sea.
                    Loops
m=0;
x(1)=10;
for k=2:3:11
m=m+1;
x(m+1)=x(m)+k^2;

We will have
x(2)=14, x(3)=39, x(4)=103, x(5)=224
2012/5/27        Puff! The magic dragon,   60
                      live by the sea.
x=5;
k=0;
while x<25
k=k+1;
y(k)=3*x;
x=2*x-1;

We will have x=[9 17 33]; y=[15 27 51]
2012/5/27        Puff! The magic dragon,   61
                      live by the sea.
            Example p.41
amount=500;
k=0;
while amount < 10000
k=k+1;
amount=amount*1.05+500;
end
amount
k
2012/5/27      Puff! The magic dragon,   62
                    live by the sea.
            Self testing p.42




2012/5/27       Puff! The magic dragon,   63
                     live by the sea.
            The Matlab help system
1. Help browser
2. Help functions ~ help, lookfor, doc
3. Other resources ~ demo




2012/5/27         Puff! The magic dragon,   64
                       live by the sea.
2012/5/27   Puff! The magic dragon,   65
                 live by the sea.
•   Viewing documentation
•   Using the contents tab
•   Using the index tab
•   Using the search tab
•   Using the favorites tab




2012/5/27          Puff! The magic dragon,   66
                        live by the sea.
             Help functions
• help function_name
• lookfor topic
• doc function_name




2012/5/27       Puff! The magic dragon,   67
                     live by the sea.
2012/5/27   Puff! The magic dragon,   68
                 live by the sea.
     Problem solving methodologies




2012/5/27      Puff! The magic dragon,   69
                    live by the sea.
2012/5/27   Puff! The magic dragon,   70
                 live by the sea.
2012/5/27   Puff! The magic dragon,   71
                 live by the sea.
2012/5/27   Puff! The magic dragon,   72
                 live by the sea.
2012/5/27   Puff! The magic dragon,   73
                 live by the sea.
2012/5/27   Puff! The magic dragon,   74
                 live by the sea.
2012/5/27   Puff! The magic dragon,   75
                 live by the sea.
            Summary, p.58




2012/5/27      Puff! The magic dragon,   76
                    live by the sea.
2012/5/27   Puff! The magic dragon,   77
                 live by the sea.
2012/5/27   Puff! The magic dragon,   78
                 live by the sea.
2012/5/27   Puff! The magic dragon,   79
                 live by the sea.
                  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?

2012/5/27         Puff! The magic dragon,           80
                       live by the sea.
                   HW#CH1
• Problems
• 2, 5, 9, 20, 23, 25, 30, 31, 35




2012/5/27          Puff! The magic dragon,   81
                        live by the sea.

								
To top