第 3 章 檔案、 函數 、 資料結構 files, functions , and data structures

61
Puff! The magic dragon, live by the tree… 第3第 檔檔檔檔檔檔檔檔 files, functions, and data structures

Upload: declan

Post on 19-Mar-2016

124 views

Category:

Documents


0 download

DESCRIPTION

第 3 章 檔案、 函數 、 資料結構 files, functions , and data structures. Why use ‘files’?. 多行指令 or 日後重複使用. 3 types of files. *.m, ~ in ASCII format *.mat, ~ in binary format (variables in work space) data file ~ in ASCII format. Recording ur session. diary ~ save in the file ‘ diary ’ - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

第 3章 檔案、函數、資料結構files, functions, and data structures

Page 2: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 3: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Why use ‘files’?

• 多行指令 or 日後重複使用

Page 4: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

3 types of files

• *.m, ~ in ASCII format• *.mat, ~ in binary format

(variables in work space)• data file ~ in ASCII format

Page 5: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Recording ur session

• diary ~ save in the file ‘diary’• type diary ~ u can see the record after u h

ave typed ‘diary’• diary filename ~ assign the filename that w

ill be recorded• get(O,’diary’) ~ ‘on’ or ‘off’ ~ see the situatio

n that whether diary is on or off!

Page 6: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Saving and retriving ur workspace variables~ save & load

• save ~ in matlab.mat• save filename• load filename• save filename var1 var2• save filename –ASCII• save filename –double

Page 7: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Importing data from externally generated files

• load filename (remember to remove the header first)

• e.g. load force.dat ~ than we get a matrix named ‘force’

• Don’t use ‘*.mat’ as the data file!

Page 8: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Importing spreadsheet files

• M=wk1read(‘filename’)• A=xlsread(‘filename’)• [A,B] =xlsread(‘filename’)

1. Try to generate a 3 by 10 matrix with all elements are ’10’ in excel, and save it as ‘ggg.xls’

2. Try to read it from matlab

Page 9: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

The import wizard

• importing data• exporting data

Page 10: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Importing ASCII data

• P.129 attentions!• Data type: numeric? Text? Or in mixed

type!

Page 11: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Import ASCII data files with text headers

• P.130-131

Page 12: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Importing mixed text and numeric ASCII data

• P.131-132

Page 13: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Importing binary data files• ?A=[1 2 3; 4 5 6; 7 8 9]• A =• 1 2 3• 4 5 6• 7 8 9• ?save my_data.out A -ascii• ?type A• ??? Error using ==> type• a.m: File not found.• type my_data.out

Page 14: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Controlling input & output

• ?speed=30• speed =• 30• ?disp('The predicted speed is:')• The predicted speed is:• ?disp(speed)• 30

Page 15: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Example (p.134-135)

Page 16: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

• ?a=3/7• a =• 0.4286• ?format long• ?a• a =• 0.42857142857143• ?format short e• ?a• a =• 4.2857e-001• ?format long e• ?a• a =• 4.285714285714286e-001

Page 17: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 18: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 19: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 20: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 21: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

• ?format rat• ?a• a =• 3/7 • ?format bank • ?a• a =• 0.43

Page 22: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

User input

• input (‘please enter the value of x’)• If x, y exist first!• ?k=menu('Choose a data marker','o','*','x');• ?type=['o','*','x'];• ?k• k =• 2• ?plot(x,y,x,y,type(k))

Page 23: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

User input

• xi=input ('please enter the initial value of x ')• xf=input ('please enter the initial value of x ')• x=[xi:0.01:xf];• y=sin(x);• k=menu('Choose a data marker','o','*','x');• k• type=['o','*','x'];• plot(x,y,x,y,type(k))

Page 24: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Elementary mathematical functions

• ?lookfor imaginary• I Imaginary unit.• J Imaginary unit.• COMPLEX Construct complex result fro

m real and imaginary parts.• IMAG Complex imaginary part.

Page 25: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Exponential & logarithmic functions

• ?x=9;• ?y=sqrt(x);• ?x,y• x =• 9• y =• 3

Page 26: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Complex number functions

• x=a+b*I ~ (M,th)• M=sqrt(a^2+b^2) ~ abs(x)• th=arctan(b/a) ~ angle(x)• a=M*cos(th) ~ real(x)• b=M*sin(th) ~ imag(x)

Page 27: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Complex number functions• a=input('please input the real part ')• b=input('please input the real part ')• x=a+b*i• M=sqrt(a^2+b^2)• M1=abs(x)• th=atan(b/a)• th1=angle(x)• th2=atan2(b,a)• aa=M*cos(th2)• aa1=real(x)• bb=M*sin(th2)• bb1=imag(x)

Page 28: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Complex number functions• >> test5• please input the real part -3• a =• -3• please input the real part 4• b =• 4• x =• -3.0000 + 4.0000i• M =• 5• M1 =• 5

• th =• -0.9273• th1 =• 2.2143• th2 =• 2.2143• aa =• -3.0000• aa1 =• -3• bb =• 4.0000• bb1 =• 4

Page 29: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 30: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

• x =• 3.0000 - 4.0000i• ?abs(x)• ans =• 5• ?angle(x)• ans =• -0.9273• 第四象限

• ?y=-3+4*i• y =• -3.0000 + 4.0000i• ?angle(y)• ans =• 2.2143• 第二象限

Page 31: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

x is a vector

• abs(x) ~ return all the absolute value of each elements ~ a vector again!

• We can use sqrt(x’*x) to obtain the ‘length’ of the vector x when x is a column vector

• We can use sqrt(x*x’) to obtain the ‘length’ of the vector x when x is a row vector

Page 32: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

• ?x=-3+4i• x =• -3.0000 + 4.0000i• ?y=6-8i• y =• 6.0000 - 8.0000i• ?mag_x=abs(x)• mag_x =• 5• ?angle_x=angle(x)• angle_x =• 2.2143• ?angle_y=angle(y)• angle_y =• -0.9273

• ?sum_angle=angle_x+angle_y• sum_angle =• 1.2870• ?sum1=angle(x+y)• sum1 =• -0.9273• ?angle_product=angle(x*y)• angle_product =• 1.2870

Page 33: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Numerical functions

• ?x=[5,7,15]• x =• 5 7 15• ?y=sqrt(x)• y =• 2.2361 2.6458 3.8730

Page 34: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

• ?x1=2.1• x1 =• 2.1000• ?x2=2.9• x2 =• 2.9000• ?round(x1)• ans =• 2• ?round(x2)• ans =• 3• ?fix(x1)• ans =• 2• ?fix(x2)• ans =• 2

• ?ceil(x1)• ans =• 3• ?ceil(x2)• ans =• 3• ?floor(x1)• ans =• 2• ?floor(x2)• ans =• 2

• What happen when x1=-2.1, x2=-2.9?

Page 35: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 36: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Trigonometric functions• ?x=[1;2;3]• x =• 1• 2• 3• ?sin(x.^2+5)• ans =• -0.2794• 0.4121• 0.9906

• ?sin(sqrt(x)+1)• ans =• 0.9093• 0.6649• 0.3982

Page 37: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

atan(y/x), atan2(y,x)• x =• 3• ?y=-4• y =• -4• ?atan(y/x)• ans =• -0.9273• ?atan2(y,x)• ans =• -0.9273

Page 38: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 39: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Hyperbolic functions

2cosh

xx eex

2sinh

xx eex

Page 40: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 41: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Self testing

• P.143• Use ur calculator now• Use ur computer after this class

Page 42: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

User-defined functions

• function file ~ local variables• function definition line 函數定義列 ~1st lin

efunction[output variables]=function_name(input variables);

• save as function_name.m

Page 43: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

function definition line 函數定義列1. function[area_square]=square(side)

1 input/1 output2. function area_square=square(side)

1 input/1 output3. function[volume_box]=box(height, width,lengt

h) 3 input/1 output4. function[area_circle,circumf]=circle(radius)

1 input/2 output5. function sqplot(side) ~ just plotting

Page 44: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

• a=32.2; %call_drop.m• initial_speed=10;• time=5;• [feet_Dropped,speed]=drop(a, initial_speed,time)• %[feet_Dropped,speed]=drop(32.2,10, 5)• %[feet_Dropped,speed]=drop(32.2,10, [0:1:5])

• function[dist, vel]=drop(g,v0,t);• %calculate the velocity and falling distance for the free fal

ling body• %input var: acceleration, initial vel. v0, falling time t• vel=g*t+v0;• dist=0.5*g*t.^2+v0*t;

Page 45: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Local variables

• U can use it freely in the functions• U don’t have to care any multi-use of the

variable

Page 46: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Global variables

• Common used variables!• See p.147-149 for detail description

Page 47: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

• function P=ideal_1(T,Vhat,R);• P=R*T./Vhat;• function P=ideal_2(T,Vhat);• R=0.08206;• P=R*T./Vhat;• function P=ideal_3(T,Vhat);• global R;• P=R*T./Vhat;• global R• R=0.08206;• ideal_3([300,330],20)

Page 48: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

• function P=vdwaals_1(T,Vhat,R,a,b);• P=R*T./(Vhat-b)-a./Vhat.^2;• function P=vdwaals_2(T,Vhat);• R=0.08206• A=6.49; b=0.0562;• P=R*T./(Vhat-b)-a./Vhat.^2;• vdwaals_3(300,20)• P???????????• P=vdwaals_3(300,20)

• global a,b

Page 49: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

applications

• Finding zeros of a function• ~ roots ~ for single variable only• ~fzero(‘function’,x0)~ find out the zero for

‘function’ near x0• ~fzero(‘function’,x0(1),xo(2))~ f(x0(1))*f(x0(2))<0

Page 50: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

• function y=f1(x)• y=x+2*exp(-x)-3;

Page 51: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 52: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

minimizing a function of one variable

• fminbnd(‘function’,x1,x2) ????????• Pp.151-152

Page 53: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 54: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 55: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 56: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 57: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 58: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 59: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 60: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…

Page 61: 第 3 章   檔案、 函數 、 資料結構 files,  functions , and  data structures

Puff! The magic dragon, live by the tree…