📄 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 + -