?? ch4_5.htm
字號:
<! Made by Html Translation Ver 1.0><html><head><title> 使用者自定函數 </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 使用者自定函數</h1></font><hr><p>我們在第二章提過的 M-file 除了可以撰寫程式外,還有另一個重要的用途,就是可以用來定義函數。這樣的函數稱為M-檔定義的函數, 然后儲存起來,就可以和那些內建的函數(如<font COLOR="#FF0000">sin</font>, <font COLOR="#FF0000">cos</font>,<font COLOR="#FF0000">log</font>等)一樣的自由使用。舉例來說,我們可以定義一函 數cirarea是計算圓的面積,以下的 M-file: <font COLOR="#FF0000">cirarea.m</font>就是定義這個函數 </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>令一個例子是MATLAB內建的函數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定義的函數有其語法的一些規定: <ol> <li>第一行指令以<font COLOR="#FF0000">function</font>這個字做為起頭,接著是輸出的變數,等號,函數名稱,輸入的變數是接著函數名 稱放在括號之內。<font COLOR="#FF0000">function out1=userfun(in1)</font>,這行的<font COLOR="#FF0000">out1</font>是輸出的變數,<font COLOR="#FF0000">userfun</font>是函數名稱,<font COLOR="#FF0000">in1</font>是輸入的 變數。<font COLOR="#FF0000">function [out1, out2]= serfun(in1, in2) </font>如果輸出變數 <font COLOR="#FF0000">[out1,out2]</font> 和輸入變數 <font COLOR="#FF0000">(in1, in2)</font>不只一個時,則在 輸出變數部份須加上 <font COLOR="#FF0000">[ ]</font>。 </li> <li>上述的輸入變數是經由使用函數時輸入的,而輸出的變數即是函數傳回的值。 </li> <li>函數名稱的取法的規定與一般變數相同。 </li> <li>在定義函數程式之前,最好加上注解行來說明這個函數的特色及如何使用,如此的話使用指令如<font COLOR="#FF0000">help cirarea</font>,該函數的注解行會出現在指令視窗。 </li></ol><p><font COLOR="#FF0000">>> r=1:3;</font> </p><p><font COLOR="#FF0000">>> ar=cirarea(r) % 呼叫 cirarea.m 函數,以陣列 r 為輸入變數</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 可以將變數值直接列出</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>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -