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