一個函數(shù)是一組在一起執(zhí)行任務(wù)的語句。 在MATLAB中,函數(shù)在單獨的文件中定義。文件的名稱和函數(shù)的名稱應(yīng)該是一樣的。
函數(shù)在自己的工作空間內(nèi)的變量上運行,這個變量也稱為本地工作空間,與在MATLAB命令提示符下訪問的工作區(qū)(稱為基本工作區(qū))不同。
函數(shù)可以接受多個輸入?yún)?shù),并可能返回多個輸出參數(shù)。
函數(shù)語句的語法是 -
function [out1,out2, ..., outN] = myfun(in1,in2,in3, ..., inN)
以下名稱為mymax
的函數(shù)應(yīng)寫入名稱為mymax.m
的文件中。它需要五個數(shù)字作為參數(shù),并返回參數(shù)數(shù)字值的最大值。
創(chuàng)建名為mymax.m
的函數(shù)文件,從左上角菜單中點擊新建->函數(shù),并在其中鍵入以下代碼 -
function max = mymax(n1, n2, n3, n4, n5)
% This function calculates the maximum of the
% five numbers given as input
max = n1;
if(n2 > max)
max = n2;
end
if(n3 > max)
max = n3;
end
if(n4 > max)
max = n4;
end
if(n5 > max)
max = n5;
end
上面示例代碼中,函數(shù)的第一行以關(guān)鍵字function
開頭。它給出了函數(shù)的名稱和參數(shù)的順序。在這個例子中,mymax
函數(shù)有五個輸入?yún)?shù)和一個輸出參數(shù)。
函數(shù)語句之后的注釋行提供了幫助文本。當(dāng)鍵入時,這些行被打印 -
Trial>> help mymax
This function calculates the maximum of the
five numbers given as input
現(xiàn)在,我們來調(diào)用這個函數(shù) -
mymax(11,22,35,81,198)
執(zhí)行上面代碼,得到以下結(jié)果 -
Trial>> mymax(11,22,35,81,198)
ans =
198
匿名函數(shù)就像傳統(tǒng)編程語言中的內(nèi)聯(lián)函數(shù),在單個MATLAB語句中定義。 它由單個MATLAB表達(dá)式和任意數(shù)量的輸入和輸出參數(shù)組成。
可以在MATLAB命令行或函數(shù)或腳本中定義一個匿名函數(shù)。
這樣就可以創(chuàng)建簡單的函數(shù),而無需為它們創(chuàng)建一個文件。
從表達(dá)式創(chuàng)建匿名函數(shù)的語法是 -
f = @(arglist)expression
示例
在這個例子中,編寫一個名為power
的匿名函數(shù),它將使用兩個數(shù)字作為輸入,并將第一個數(shù)字返回到第二個數(shù)字的冪值。
創(chuàng)建腳本文件并在其中鍵入以下代碼 -
power = @(x, n) x.^n;
result1 = power(7, 3)
result2 = power(49, 0.5)
result3 = power(10, -10)
result4 = power (4.5, 1.5)
當(dāng)運行該文件,得到以下結(jié)果 -
result1 = 343
result2 = 7
result3 = 1.0000e-10
result4 = 9.5459
必須在文件中定義除了匿名函數(shù)以外的其它任何函數(shù)。每個函數(shù)文件包含主要出現(xiàn)的必需的主函數(shù),以及主函數(shù)之后的任意數(shù)量的可選子函數(shù)。
可以從命令行或其他函數(shù)的文件外部調(diào)用主函數(shù),但不能從命令行或函數(shù)文件外的其他函數(shù)調(diào)用子函數(shù)。
子函數(shù)僅對函數(shù)文件中的主函數(shù)和其他子函數(shù)可見。
下面編寫一個名為quadratic
的函數(shù)來計算二次方程的根。該函數(shù)需要三個輸入?yún)?shù):二次系數(shù),線性系數(shù)和常數(shù)項。計算并會返回根。
函數(shù)文件quadratic.m
將包含主函數(shù)quadratic
和次函數(shù)和子函數(shù)disc
,它計算判別式。
創(chuàng)建一個函數(shù)文件quadratic.m
并在其中鍵入以下代碼 -
function [x1,x2] = quadratic(a,b,c)
%this function returns the roots of
% a quadratic equation.
% It takes 3 input arguments
% which are the co-efficients of x2, x and the
%constant term
% It returns the roots
d = disc(a,b,c);
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
end % end of quadratic
function dis = disc(a,b,c)
%function calculates the discriminant
dis = sqrt(b^2 - 4*a*c);
end % end of sub-function
可以從命令提示符調(diào)用上述函數(shù) -
Trial>> quadratic(2,4,-4)
ans =
0.7321
可以在一個函數(shù)的主體內(nèi)定義另一個函數(shù)。這樣的函數(shù)被稱為嵌套函數(shù)。嵌套函數(shù)包含任何其他函數(shù)的部分或全部組件。
嵌套函數(shù)在另一個函數(shù)的范圍內(nèi)定義,并且它們共享對包含函數(shù)的工作空間的訪問。
嵌套函數(shù)遵循以下語法 -
function x = A(p1, p2)
...
B(p2)
function y = B(p3)
...
end
...
end
示例
下面來重寫quadratic
函數(shù),從上一個例子來看,然而這次disc
函數(shù)將是一個嵌套函數(shù)。
創(chuàng)建一個函數(shù)文件quadratic2.m
并在其中鍵入以下代碼 -
function [x1,x2] = quadratic2(a,b,c)
function disc % nested function
d = sqrt(b^2 - 4*a*c);
end % end of function disc
disc;
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
end % end of function quadratic2
在命令行窗口中調(diào)用quadratic2.m
函數(shù),得到以下結(jié)果 -
Trial>> quadratic2(2,4,-4)
ans =
0.7321
私有函數(shù)是僅在有限的其他函數(shù)組中可見的主函數(shù)。如果不想公開函數(shù)的實現(xiàn),則可以將其創(chuàng)建為私有函數(shù)。
私有函數(shù)處在在專用名稱為私有的子文件夾中。
它們只對父文件夾中的函數(shù)可見。
下面來重寫二次函數(shù)。這一次,計算判別式的disc
函數(shù)將是私有函數(shù)。
在工作目錄中創(chuàng)建一個名為private
的子文件夾(F:\worksp\matlab\private)。存儲以下函數(shù)在文件disc.m
中 -
function dis = disc(a,b,c)
%function calculates the discriminant
dis = sqrt(b^2 - 4*a*c);
end % end of sub-function
在工作目錄中創(chuàng)建一個函數(shù)quadratic3.m
(對應(yīng)目錄為:F:\worksp\matlab),并在其中鍵入以下代碼:
function [x1,x2] = quadratic3(a,b,c)
%this function returns the roots of
% a quadratic equation.
% It takes 3 input arguments
% which are the co-efficient of x2, x and the
%constant term
% It returns the roots
d = disc(a,b,c);
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
end % end of quadratic3
在Matlab命令行中,調(diào)用以上定義的函數(shù) -
Trial>> quadratic3(2,4,-4)
ans =
0.7321
全局變量可由多個函數(shù)共享。 為此,需要在所有函數(shù)中聲明變量為全局變量。
如果要從基本工作區(qū)訪問該變量,則在命令行中聲明該變量。
全局聲明必須在函數(shù)實際使用變量之前進(jìn)行。 將大寫字母用于全局變量的名稱是一個很好的做法,以區(qū)別于其他變量。
示例
下面創(chuàng)建一個名為average.m
的函數(shù)文件,并在其中鍵入以下代碼 -
function avg = average(nums)
global TOTAL
avg = sum(nums)/TOTAL;
end
創(chuàng)建腳本文件并在其中鍵入以下代碼 -
global TOTAL;
TOTAL = 10;
n = [34, 45, 25, 45, 33, 19, 40, 34, 38, 42];
av = average(n)
執(zhí)行上面示例代碼,得到以下結(jié)果 -
Trial>> global TOTAL;
TOTAL = 10;
n = [36, 45, 35, 45, 33, 29, 50, 41, 58, 47];
av = average(n)
av =
41.9000