在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ Matlab函數(shù)
Matlab代數(shù)(方程求解)
Matlab開發(fā)環(huán)境設(shè)置
Matlab GNU Octave
Matlab字符串
Matlab矩陣
Matlab微分和導(dǎo)數(shù)
Matlab數(shù)字
Matlab數(shù)據(jù)導(dǎo)入
Matlab整合集成
Matlab冒號(hào)符號(hào)
Matlab變量
Matlab數(shù)組
Matlab運(yùn)算符
Matlab數(shù)據(jù)導(dǎo)出
Matlab向量
Matlab命令
Matlab決策
Matlab微積分
Matlab圖形
Matlab教程
Matlab繪圖
Matlab多項(xiàng)式
Matlab .m腳本文件
Matlab循環(huán)
Matlab基礎(chǔ)語(yǔ)法
Matlab函數(shù)
Matlab轉(zhuǎn)換
Matlab概述
Matlab數(shù)據(jù)類型

Matlab函數(shù)

一個(gè)函數(shù)是一組在一起執(zhí)行任務(wù)的語(yǔ)句。 在MATLAB中,函數(shù)在單獨(dú)的文件中定義。文件的名稱和函數(shù)的名稱應(yīng)該是一樣的。

函數(shù)在自己的工作空間內(nèi)的變量上運(yùn)行,這個(gè)變量也稱為本地工作空間,與在MATLAB命令提示符下訪問(wèn)的工作區(qū)(稱為基本工作區(qū))不同。

函數(shù)可以接受多個(gè)輸入?yún)?shù),并可能返回多個(gè)輸出參數(shù)。

函數(shù)語(yǔ)句的語(yǔ)法是 -

function [out1,out2, ..., outN] = myfun(in1,in2,in3, ..., inN)

示例

以下名稱為mymax的函數(shù)應(yīng)寫入名稱為mymax.m的文件中。它需要五個(gè)數(shù)字作為參數(shù),并返回參數(shù)數(shù)字值的最大值。

創(chuàng)建名為mymax.m的函數(shù)文件,從左上角菜單中點(diǎn)擊新建->函數(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ù)的順序。在這個(gè)例子中,mymax函數(shù)有五個(gè)輸入?yún)?shù)和一個(gè)輸出參數(shù)。

函數(shù)語(yǔ)句之后的注釋行提供了幫助文本。當(dāng)鍵入時(shí),這些行被打印 -

Trial>> help mymax
 This function calculates the maximum of the
  five numbers given as input

現(xiàn)在,我們來(lái)調(diào)用這個(gè)函數(shù) -

mymax(11,22,35,81,198)

執(zhí)行上面代碼,得到以下結(jié)果 -

Trial>> mymax(11,22,35,81,198)

ans =

   198

匿名函數(shù)

匿名函數(shù)就像傳統(tǒng)編程語(yǔ)言中的內(nèi)聯(lián)函數(shù),在單個(gè)MATLAB語(yǔ)句中定義。 它由單個(gè)MATLAB表達(dá)式和任意數(shù)量的輸入和輸出參數(shù)組成。

可以在MATLAB命令行或函數(shù)或腳本中定義一個(gè)匿名函數(shù)。

這樣就可以創(chuàng)建簡(jiǎn)單的函數(shù),而無(wú)需為它們創(chuàng)建一個(gè)文件。

從表達(dá)式創(chuàng)建匿名函數(shù)的語(yǔ)法是 -

f = @(arglist)expression

示例

在這個(gè)例子中,編寫一個(gè)名為power的匿名函數(shù),它將使用兩個(gè)數(shù)字作為輸入,并將第一個(gè)數(shù)字返回到第二個(gè)數(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)運(yùn)行該文件,得到以下結(jié)果 -

result1 =  343
result2 =  7
result3 =  1.0000e-10
result4 =  9.5459

主函數(shù)和次函數(shù)

必須在文件中定義除了匿名函數(shù)以外的其它任何函數(shù)。每個(gè)函數(shù)文件包含主要出現(xiàn)的必需的主函數(shù),以及主函數(shù)之后的任意數(shù)量的可選子函數(shù)。

可以從命令行或其他函數(shù)的文件外部調(diào)用主函數(shù),但不能從命令行或函數(shù)文件外的其他函數(shù)調(diào)用子函數(shù)。

子函數(shù)僅對(duì)函數(shù)文件中的主函數(shù)和其他子函數(shù)可見。

示例

下面編寫一個(gè)名為quadratic的函數(shù)來(lái)計(jì)算二次方程的根。該函數(shù)需要三個(gè)輸入?yún)?shù):二次系數(shù),線性系數(shù)和常數(shù)項(xiàng)。計(jì)算并會(huì)返回根。

函數(shù)文件quadratic.m將包含主函數(shù)quadratic和次函數(shù)和子函數(shù)disc,它計(jì)算判別式。

創(chuàng)建一個(gè)函數(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ù)

可以在一個(gè)函數(shù)的主體內(nèi)定義另一個(gè)函數(shù)。這樣的函數(shù)被稱為嵌套函數(shù)。嵌套函數(shù)包含任何其他函數(shù)的部分或全部組件。

嵌套函數(shù)在另一個(gè)函數(shù)的范圍內(nèi)定義,并且它們共享對(duì)包含函數(shù)的工作空間的訪問(wèn)。

嵌套函數(shù)遵循以下語(yǔ)法 -

function x = A(p1, p2)
...
B(p2)
   function y = B(p3)
   ...
   end
...
end

示例

下面來(lái)重寫quadratic函數(shù),從上一個(gè)例子來(lái)看,然而這次disc函數(shù)將是一個(gè)嵌套函數(shù)。

創(chuàng)建一個(gè)函數(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ù)。如果不想公開函數(shù)的實(shí)現(xiàn),則可以將其創(chuàng)建為私有函數(shù)。

私有函數(shù)處在在專用名稱為私有的子文件夾中。

它們只對(duì)父文件夾中的函數(shù)可見。

示例

下面來(lái)重寫二次函數(shù)。這一次,計(jì)算判別式的disc函數(shù)將是私有函數(shù)。

在工作目錄中創(chuàng)建一個(gè)名為private的子文件夾(F:\worksp\matlab\private)。存儲(chǔ)以下函數(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)建一個(gè)函數(shù)quadratic3.m(對(duì)應(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

全局變量

全局變量可由多個(gè)函數(shù)共享。 為此,需要在所有函數(shù)中聲明變量為全局變量。

如果要從基本工作區(qū)訪問(wèn)該變量,則在命令行中聲明該變量。

全局聲明必須在函數(shù)實(shí)際使用變量之前進(jìn)行。 將大寫字母用于全局變量的名稱是一個(gè)很好的做法,以區(qū)別于其他變量。

示例

下面創(chuàng)建一個(gè)名為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

上一篇:Matlab矩陣下一篇:Matlab .m腳本文件