📄 ch4_5.htm
字号:
<! Made by Html Translation Ver 1.0>
<HTML>
<HEAD>
<TITLE> 使用者自定函数 </TITLE>
</HEAD>
<BODY BACKGROUND="bg0000.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/bg0000.gif">
<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>
<FONT COLOR=#FF0000>% M-file function, cirarea.m</FONT>
<P>
<FONT COLOR=#FF0000>% Calculate the area of a circle with raduis
r </FONT>
<P>
<FONT COLOR=#FF0000>% r can be a scalar or an array</FONT>
<P>
<FONT COLOR=#FF0000>function c=cirarea(r)</FONT>
<P>
<FONT COLOR=#FF0000>c=pi*r.^2;<BR>
</FONT>
<P>
令一个例子是MATLAB内建的函数linspace
<P>
<FONT COLOR=#FF0000>function y = linspace(d1, d2, n)</FONT>
<P>
<FONT COLOR=#FF0000>% LINSPACE Linearly spaced vector.</FONT>
<P>
<FONT COLOR=#FF0000>% LINSPACE(x1, x2) generates a row vector
of 100 linearly</FONT>
<P>
<FONT COLOR=#FF0000>% equally spaced points between x1 and x2.</FONT>
<P>
<FONT COLOR=#FF0000>% LINSPACE(x1, x2, N) generates N points between
x1 and x2.</FONT>
<P>
<FONT COLOR=#FF0000>%</FONT>
<P>
<FONT COLOR=#FF0000>% See also LOGSPACE, :.</FONT>
<P>
<FONT COLOR=#FF0000>% Copyright (c) 1984-94 by The MathWorks,
Inc.<BR>
</FONT>
<P>
<FONT COLOR=#FF0000>if nargin == 2</FONT>
<P>
<FONT COLOR=#FF0000>n = 100;</FONT>
<P>
<FONT COLOR=#FF0000>end</FONT>
<P>
<FONT COLOR=#FF0000>y = [d1+(0:n-2)*(d2-d1)/(n-1) d2];<BR>
</FONT>
<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>在定义函数程式之前,最好加上注解行来说明这个函数的特色及如何使用,如此的话使用指令如<FONT COLOR=#FF0000>help
cirarea</FONT>,该函数的注解行会出现在指令视窗。
</OL>
<P>
<P>
<FONT COLOR=#FF0000>>> r=1:3;</FONT>
<P>
<FONT COLOR=#FF0000>>> ar=cirarea(r) % 呼叫 cirarea.m 函数,以阵列
r 为输入变数</FONT>
<P>
<FONT COLOR=#FF0000>ar =</FONT>
<P>
<FONT COLOR=#FF0000>3.1416 12.5664 28.2743<BR>
</FONT>
<P>
<FONT COLOR=#FF0000>>> disp(ar) % 指令 disp 可以将变数值直接列出</FONT>
<P>
<FONT COLOR=#FF0000>3.1416 12.5664 28.2743<BR>
</FONT><HR>
<A HREF="ch4_4.htm" tppabs="http://166.111.167.223/computer/cai/matlabjc/ch4_4.htm"><IMG SRC="lastpage.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/lastpage.gif" BORDER=0></A>
<A HREF="ch4_6.htm" tppabs="http://166.111.167.223/computer/cai/matlabjc/ch4_6.htm"><IMG SRC="nextpage-1.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/nextpage.gif" BORDER=0 HSPACE=10></A>
<A HREF="index.html" tppabs="http://166.111.167.223/computer/cai/matlabjc/index.html"><IMG SRC="outline-1.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/outline.gif" BORDER=0 HSPACE=6></A><BR>
<FONT SIZE=2 COLOR=#AA55FF> 上一页 下一页 讲义大纲 </FONT>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -