?? ch4_5.htm
字號(hào):
<! Made by Html Translation Ver 1.0><html><head><title> 使用者自定函數(shù) </title></head><body BACKGROUND="../img1/bg0000.gif" tppabs="http://webclass.ncu.edu.tw/~junwu/img/bg0000.gif"><script language="JAVASCRIPT"><!--if (navigator.onLine){document.write("<!-- Spidersoft WebZIP Ad Banner Insert -->");document.write("<TABLE width=100% border=0 cellpadding=0 cellspacing=0>");document.write("<TR>");document.write("<TD>");document.write("<ILAYER id=ad1 visibility=hidden height=60></ILAYER>");document.write("<NOLAYER>");document.write("<IFRAME SRC='http://www.spidersoft.com/ads/bwz468_60.htm' width=100% height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no></IFRAME>");document.write("</NOLAYER>");document.write("</TD>");document.write("</TR>");document.write("</TABLE>");document.write("<!-- End of Spidersoft WebZIP Ad Banner Insert-->");} //--></script><!-- Spidersoft WebZIP Ad Banner Insert --><!-- End of Spidersoft WebZIP Ad Banner Insert--><font COLOR="#0000FF"><h1>4.5 使用者自定函數(shù)</h1></font><hr><p>我們?cè)诘诙绿徇^的 M-file 除了可以撰寫程式外,還有另一個(gè)重要的用途,就是可以用來定義函數(shù)。這樣的函數(shù)稱為M-檔定義的函數(shù), 然后儲(chǔ)存起來,就可以和那些內(nèi)建的函數(shù)(如<font COLOR="#FF0000">sin</font>, <font COLOR="#FF0000">cos</font>,<font COLOR="#FF0000">log</font>等)一樣的自由使用。舉例來說,我們可以定義一函 數(shù)cirarea是計(jì)算圓的面積,以下的 M-file: <font COLOR="#FF0000">cirarea.m</font>就是定義這個(gè)函數(shù) </p><p><font COLOR="#FF0000">% M-file function, cirarea.m</font> </p><p><font COLOR="#FF0000">% Calculate the area of a circle with raduis r </font></p><p><font COLOR="#FF0000">% r can be a scalar or an array</font> </p><p><font COLOR="#FF0000">function c=cirarea(r)</font> </p><p><font COLOR="#FF0000">c=pi*r.^2;<br></font></p><p>令一個(gè)例子是MATLAB內(nèi)建的函數(shù)linspace </p><p><font COLOR="#FF0000">function y = linspace(d1, d2, n)</font> </p><p><font COLOR="#FF0000">% LINSPACE Linearly spaced vector.</font> </p><p><font COLOR="#FF0000">% LINSPACE(x1, x2) generates a row vector of 100 linearly</font> </p><p><font COLOR="#FF0000">% equally spaced points between x1 and x2.</font> </p><p><font COLOR="#FF0000">% LINSPACE(x1, x2, N) generates N points between x1 and x2.</font> </p><p><font COLOR="#FF0000">%</font> </p><p><font COLOR="#FF0000">% See also LOGSPACE, :.</font> </p><p><font COLOR="#FF0000">% Copyright (c) 1984-94 by The MathWorks, Inc.<br></font></p><p><font COLOR="#FF0000">if nargin == 2</font> </p><p><font COLOR="#FF0000">n = 100;</font> </p><p><font COLOR="#FF0000">end</font> </p><p><font COLOR="#FF0000">y = [d1+(0:n-2)*(d2-d1)/(n-1) d2];<br></font></p><p>M-file定義的函數(shù)有其語法的一些規(guī)定: <ol> <li>第一行指令以<font COLOR="#FF0000">function</font>這個(gè)字做為起頭,接著是輸出的變數(shù),等號(hào),函數(shù)名稱,輸入的變數(shù)是接著函數(shù)名 稱放在括號(hào)之內(nèi)。<font COLOR="#FF0000">function out1=userfun(in1)</font>,這行的<font COLOR="#FF0000">out1</font>是輸出的變數(shù),<font COLOR="#FF0000">userfun</font>是函數(shù)名稱,<font COLOR="#FF0000">in1</font>是輸入的 變數(shù)。<font COLOR="#FF0000">function [out1, out2]= serfun(in1, in2) </font>如果輸出變數(shù) <font COLOR="#FF0000">[out1,out2]</font> 和輸入變數(shù) <font COLOR="#FF0000">(in1, in2)</font>不只一個(gè)時(shí),則在 輸出變數(shù)部份須加上 <font COLOR="#FF0000">[ ]</font>。 </li> <li>上述的輸入變數(shù)是經(jīng)由使用函數(shù)時(shí)輸入的,而輸出的變數(shù)即是函數(shù)傳回的值。 </li> <li>函數(shù)名稱的取法的規(guī)定與一般變數(shù)相同。 </li> <li>在定義函數(shù)程式之前,最好加上注解行來說明這個(gè)函數(shù)的特色及如何使用,如此的話使用指令如<font COLOR="#FF0000">help cirarea</font>,該函數(shù)的注解行會(huì)出現(xiàn)在指令視窗。 </li></ol><p><font COLOR="#FF0000">>> r=1:3;</font> </p><p><font COLOR="#FF0000">>> ar=cirarea(r) % 呼叫 cirarea.m 函數(shù),以陣列 r 為輸入變數(shù)</font> </p><p><font COLOR="#FF0000">ar =</font> </p><p><font COLOR="#FF0000">3.1416 12.5664 28.2743<br></font></p><p><font COLOR="#FF0000">>> disp(ar) % 指令 disp 可以將變數(shù)值直接列出</font> </p><p><font COLOR="#FF0000">3.1416 12.5664 28.2743<br></font></p><hr><a HREF="ch4_4.htm" tppabs="http://webclass.ncu.edu.tw/~junwu/ch4_4.htm"><p><img SRC="../img1/lastpage.gif" tppabs="http://webclass.ncu.edu.tw/~junwu/img/lastpage.gif" BORDER="0" WIDTH="42" HEIGHT="42"></a> <a HREF="ch4_6.htm" tppabs="http://webclass.ncu.edu.tw/~junwu/ch4_6.htm"><img SRC="../img1/nextpage.gif" tppabs="http://webclass.ncu.edu.tw/~junwu/img/nextpage.gif" BORDER="0" HSPACE="10" WIDTH="42" HEIGHT="42"></a> <a HREF="../index.html" tppabs="http://webclass.ncu.edu.tw/~junwu/index.html"><img SRC="../img1/outline.gif" tppabs="http://webclass.ncu.edu.tw/~junwu/img/outline.gif" BORDER="0" HSPACE="6" WIDTH="42" HEIGHT="42"></a><br><font SIZE="2" COLOR="#AA55FF">上一頁 下一頁 講義大綱 </font><layer src="http://www.spidersoft.com/ads/bwz468_60.htm" visibility="hidden" id="a1" width="600" onload="moveToAbsolute(ad1.pageX,ad1.pageY); a1.clip.height=60;visibility='show';"></layer> </p></body></html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -