使用者定义函式_第1页
使用者定义函式_第2页
使用者定义函式_第3页
使用者定义函式_第4页
使用者定义函式_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、使用者定義函式 黃聰明 國立臺灣師範大學數學系 1使用函式的效益個別測試子任務程式碼能重複使用 減少整個程式開發所需要的時間與心力 簡化程式的除錯工作避免無心的錯誤函式從擁有一連串資料(輸入引數清單)的程式中,接收所需的輸入資料,並經由輸出引數清單傳回結果給程式。每個函式都擁有自己的獨立變數,並且在自己的工作區內執行,與其他的函式及所呼叫的程式無關。在呼叫函式中唯一可以被函式看見的變數,只有這些輸入引數,而函式中唯一能被呼叫程式看見的變數,也只有輸出引數。某一函式不小心犯下的程式錯誤,只會影響到此函式內的變數,不會影響整個程式工作區內的變數。5-1 MATLAB 函式介紹function ou

2、t1, out2, . = funname(in1, in2, .) statements 函式名稱輸出引數輸入引數儲存之檔案名稱須與函式名稱一樣計算點(x1,y1)與點(x2,y2)的距離function distance = dist2(x1, y1, x2, y2)%DIST2 Calculate the distance between two points% Function DIST2 calculates the distance between % two points (x1,y1) and (x2,y2) in a Cartesian% coordinate system.

3、% Calling sequence:% distance = dist2(x1, y1, x2, y2) % Define variables:% x1 - x-position of point 1% y1 - y-position of point 1% x2 - x-position of point 2% y2 - y-position of point 2% distance - Distance between points % Calculate distance.distance = sqrt(x2-x1).2 + (y2-y1).2); end % function dis

4、t2儲存之檔案名稱為 dist2.mH1 comment line lookfor distanceDIST2 Calculate the distance between two points help dist2 DIST2 Calculate the distance between two points Function DIST2 calculates the distance between two points (x1,y1) and (x2,y2) in a Cartesian coordinate system. Calling sequence: distance = di

5、st2(x1, y1, x2, y2) 呼叫 dist2% Get input data.disp(Calculate the distance between two points:);ax = input(Enter x value of point a: );ay = input(Enter y value of point a: );bx = input(Enter x value of point b: );by = input(Enter y value of point b: ); % Evaluate functionresult = dist2 (ax, ay, bx, by

6、); % Write out result.fprintf(The distance between points a and b is %fn,result);function distance = dist2(x1, y1, x2, y2)在函式呼叫之前的工作區在函式呼叫期間的工作區在函式呼叫之後的工作區5-2 MATLAB的變數傳遞方式: 按值傳遞MATLAB程式使用pass-by-value的方式,進行程式與函式間的溝通聯絡,當程式呼叫函式時,MATLAB便複製實質引數,並傳遞這些實質引數的備份提供函式使用。function out = sample(a, b)fprintf(In s

7、ample: a = %f, b = %f %fn,a,b);a = b(1) + 2*a;b = a .* b;out = a + b(1);fprintf(In sample: a = %f, b = %f %fn,a,b);輸入引數值改變a = 2; b = 6 4;fprintf(Before sample: a = %f, b = %f %fn,a,b);out = sample(a,b);fprintf(After sample: a = %f, b = %f %fn,a,b);fprintf(After sample: out = %fn,out); test_sampleBef

8、ore sample: a = 2.000000, b = 6.000000 4.000000In sample: a = 2.000000, b = 6.000000 4.000000In sample: a = 10.000000, b = 60.000000 40.000000After sample: a = 2.000000, b = 6.000000 4.000000After sample: out = 70.000000範例:資料排序(由小到大)程 式 要 求使用者輸入任意數量的資料以遞增的順序來排序資料輸出排序過的資料function out = ssort(a) % Get

9、 the length of the array to sortnvals = size(a,2);% Sort the input arrayfor ii = 1:nvals-1 % Find the minimum value in a(ii) through a(n) iptr = ii; for jj = ii+1:nvals if a(jj) test_ssortEnter number of values to sort: 6Enter value 1: -5Enter value 2: 4Enter value 3: -2Enter value 4: 3Enter value 5

10、: -2Enter value 6: 0Sorted data: -5.0000 -2.0000 -2.0000 0.0000 3.0000 4.00005-3 選擇性的引數nargin:決定函式實際輸入變數的個數nargout:決定函式實際輸出變數的個數nargchk:假如用來呼叫函式的引數太少或太 多,這個函式將傳回一個標準的錯 誤訊息error:顯示錯誤的訊息,並放棄執行產生錯 誤的函式warning:顯示警告的訊息,並繼續執行函式使 用 語 法message = nargchk(min_args, max_args, num_args); 引數的最小數目引數的最大數目實際引數的數目er

11、ror(msg); 錯誤訊息的字元字串可與nargchk互相搭配,當程式發生錯誤時,便會產生一個錯誤訊息warning(msg); 範例輸入直角座標(x,y),轉換成極座標 輸出如果只輸入一個引數,則函式假設y值為0如果呼叫此函式的敘述式只有一個輸出引數,則傳回距離值angle : angle in degreemag :magnitudeif nargin 2 y = 0;endif nargout = 2 angle = atan2(y,x) * 180 / pi;endfunction mag, angle = polar_value(x,y)%POLAR_VALUE Converts

12、(x,y) to (r,theta)% Check for a legal number of input arguments.narginchk(1,2); % If the y argument is missing, set it to 0.if nargin mag angle = polar_value? Error using = polar_valueNot enough input arguments. mag angle = polar_value(1,-1,1)? Error using = polar_valueToo many input arguments. mag

13、angle = polar_value(1)mag = 1angle = 0 mag angle = polar_value(0,0)Warning: Both x and y are zero: angle is meaningless! In polar_value at 32mag = 0angle = 0 mag angle = polar_value(1,-1)mag = 1.4142angle = -455-4 使用共用記憶體分享資料global var1 var2 var3存放在共用記憶體內的變數共用記憶體(Global memory)是一種特別型態的記憶體,能在任何的工作區內存

14、取如果一個變數在函式中被宣告為全域變數,則這個變數將被存放在共用記憶體內,而不會存放在局部的工作區。如果在另一個函式中有相同名稱的變數被宣告為全域變數,則這個變數也將對應到與第一個函式中變數相同的記憶體位址。 全域變數的使用原則1.盡量少用全域變數 全域變數使程式的流程不透明,造成程式除錯或維護的困 難2.使用全域變數,請遵循下列兩原則 (1)使用前一定要宣告 (2)使用全部大寫或較長的變數名稱,以資區別3.檢視工作空間的變數,輸入whos global4.清除所有工作空間的全域變數 X,需使用 clear global X範例:亂數產生器 Modulo function: 是非負整數產生一個

15、介於0到134455的數列若未知8121, 28411及134456,則無法猜測此數列每個產生的數字出現在數列的機率均相等(均勻分佈) 利用modulo function來設計一個亂數產生器,以輸出在0, 1)範圍的實數問題方法要求給予modulo function之初始值 以下列方式產生介於0與1之間的實數:函式能夠產生並回傳亂數數列的陣列函式必須有一個或兩個輸入引數(n及m),用以設定傳回的陣列大小。如果只有一個引數,函式傳回 的陣列。如果有兩個引數,函式傳回 的陣列。亂數之初始值 ,由另一函式來指定。function ran = random0(n,m)%RANDOM0 Generate

16、 uniform random numbers in 0,1) % Declare global valuesglobal ISEED % Seed for random number generator % Check for a legal number of input arguments.narginchk(1,2); % If the m argument is missing, set it to n.if nargin seed(1024) random0(4)ans = 0.0598 1.0000 0.0905 0.2060 0.2620 0.6432 0.6325 0.839

17、2 0.6278 0.5463 0.7551 0.4554 0.3177 0.9105 0.1289 0.6230 ISEED? Undefined function or variable ISEED. random0(4)ans = 0.2266 0.3858 0.5876 0.7880 0.8415 0.9287 0.9855 0.1314 0.0982 0.6585 0.0543 0.4256 0.2387 0.7153 0.2606 0.89225-6 含函式的函式是一種輸入引數包含其他函式名稱的函式,而這些傳入函式名稱的函式,會在含函式的函式執行過程中,被呼叫來使用。fzero:內

18、建函式,找出只含一個變數函式之零點 fzero(cos,0 pi)ans = 1.5708 fzero(x)exp_2(x),0 pi)ans = 0.6931function val = exp_2(x)val = exp(x) - 2; fzero( exp_2,0 pi )ans = 0.6931 fzero(exp_2 ,0 pi )ans = 0.6931內建函式 evaleval(string)eval 會針對string字元字串求值 x= 1; str = exp( num2str(x) ) - 1; res = eval(str)res = 1.7183for d=1:10 s

19、 = load August int2str(d) .mat eval(s)end x = eval(sin(pi/4)x = 0.7071s = load August1.mats = load August2.mats = load August3.mat - etc. -內建函式 fevalfeval(fun,value)feval 會針對M檔案中的(fun)函式,在特定輸入值(value)下計算其函式值 feval(sin,pi/4)ans = 0.7071 feval(exp_2 , 0)ans = -1 feval( exp_2, 0)ans = -1function val =

20、exp_2(x)val = exp(x) - 2; feval(x)exp_2(x), 0)ans = -1範例問題要求步驟在特定的數值區間,產生一個含函式的函式,用來畫出任一個MATLAB單一變數的函式圖形。函式需要兩個輸入引數第一個引數為被畫出圖形的函式名稱第二個引數為兩個元素的向量,此為函式圖形的範圍檢查合法的引數個數檢查第二個引數是否擁有兩個元素在初始點與結束點之間,計算函式的數值繪圖並標示函式圖形msg = nargchk(2,2,nargin);error(msg);if ( size(xlim,1) = 1 & size(xlim,2) = 2 ) | . ( size(xlim

21、,1) = 2 & size(xlim,2) = 1 ) statementselse error(Incorrect number of elements in xlim.);endfunction quickplot(fun,xlim)%QUICKPLOT Generate quick plot of a function % Check for a legal number of input arguments.narginchk(2,2); % Check the second argument to see if it has two elements. if ( size(xlim

22、,1) = 1 & size(xlim,2) = 2 ) | . ( size(xlim,1) = 2 & size(xlim,2) = 1 ) n_steps = 100; step_size = (xlim(2) - xlim(1) / n_steps; x = xlim(1):step_size:xlim(2); y = feval(fun,x); plot(x,y); title(bfPlot of function fun (x); xlabel(bfx); ylabel(bf fun (x);else error(Incorrect number of elements in xl

23、im.);end quickplot(sin)Error using quickplot (line 5)Not enough input arguments. quickplot(sin, -2*pi)Error using quickplot (line 19)Incorrect number of elements in xlim. quickplot(sin, -2*pi 2*pi)5-7 子函式、專用函式與巢狀函式一個M檔案可以包含一個以上的函數主函數與子函數的位置一個主函數(Primary Function)其他則為子函數(Subfunctions)主函數必須與檔案名稱一樣子函數只

24、能被同檔案中的函數(主函數或子函數)呼叫,但不可被不同檔案的其他函數呼叫主函數必需出現在最上方其後接上任意數目的子函數子函數的次序並無任何限制圖例私有化目錄(Private Directory)在目錄中建立名稱為 private 的私有化目錄存放與這目錄相關的函數目錄 private 之下的函數,只能被其父目錄函數所呼叫,不能被其他目錄的函數來呼叫函 數 搜 尋 次 序從 M 檔案呼叫一個函數時,MATLAB 搜尋函數的次序檢查此函數是否為子函數檢查此函數是否為私有化目錄的函數從系統所設定的搜尋路徑找尋此函數MATLAB 找到第一個檔名相符的函數,即會立即取用巢 狀 函 式圖例NEEDfunction res = test_nested_1% Define some variables.a = 1; b = 2; x = 0; y = 9; % Display variables before call to fun1fprintf

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论