📄 ch2_7.htm
字号:
<! Made by Html Translation Ver 1.0>
<HTML>
<HEAD>
<TITLE> 如何撰写 MATLAB 程式 </TITLE>
</HEAD>
<BODY BACKGROUND="bg0000.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/bg0000.gif">
<FONT COLOR="#0000FF">
<H1>2.7 如何撰写 MATLAB 程式</H1>
</FONT>
<HR>
<P>
我们前面各节所介绍在MATLAB所做的运算,是适合于所要计算的算式不太长或是想以交谈式方式做运算,如
果要计算的算式很长有数十行或是须要一再执行的算式,则那样的方式就行不通了。MATLAB提供了所谓的
M-file 的方式,可让使用者自行将指令及算式写成巨集程式然后储存成个特别的档案,其附加档是<FONT COLOR=#FF0000>m</FONT>,譬如
<FONT COLOR=#FF0000>test.m</FONT>,其中的<FONT COLOR=#FF0000>test</FONT>就是档案名称。至于要撰写程式可以用任何一种编辑软体(如
Win3.1的记事本或是Dos下的 PE2)或是文书处理软体(如 Word, AmiPro),但是储存格式必须是 Ascii 的格式。在指令视窗中的功能选单
可以选择<B>File</B> 再选择<B>New</B>,即进入指定的编辑软体或是文书处理软体。当程式写完后要存档时,必须以.m
档名称储存。要执行 M-file 可以在指令视窗下直接键入该档名如<FONT COLOR=#FF0000>test</FONT>;或是选择功能表上的<B>Run
M-file</B>来找到 M-file 的所在目录再执行<B>M-file</B>。<B>Open M-file, Run M-file</B>。如果要修改 M-file 可以选择功能表上的<B>Open M-file</B>
,即可搜寻要修改的 M-file,修改后再存档。<BR>
<P>
以下的<FONT COLOR=#FF0000>tutex1.m</FONT>档是一个简易绘图程式做为示范使用M-file
<P>
<FONT COLOR=#FF0000>% M-file, tutex1.m</FONT>
<P>
<FONT COLOR=#FF0000>% Simple plot for illustration of using M-file.</FONT>
<P>
<FONT COLOR=#FF0000>% 简易绘图以做为示范使用M-file</FONT>
<P>
<FONT COLOR=#FF0000>x=linspace(0,2*pi,20); y=sin(x);</FONT>
<P>
<FONT COLOR=#FF0000>plot(x,y,'r+')</FONT>
<P>
<FONT COLOR=#FF0000>xlabel('x-value')</FONT>
<P>
<FONT COLOR=#FF0000>ylabel('y-value')</FONT>
<P>
<FONT COLOR=#FF0000>title('2D plot')</FONT>
<P>
写好上述程式后即可在指令视窗下键入<FONT COLOR=#FF0000>tutex1</FONT>,即可执行已建立的<FONT COLOR=#FF0000>tutex1.m</FONT><TT><FONT FACE="Courier New">
</FONT></TT>程式。<BR>
<P>
再来看另一个<TT><FONT FACE="Courier New"> </FONT></TT>M-file:
<FONT COLOR=#FF0000>tutex2.m</FONT><TT><FONT FACE="Courier New">
</FONT></TT>的例子
<P>
<FONT COLOR=#FF0000>% M-file, tutex2.m</FONT>
<P>
<FONT COLOR=#FF0000>% 计算一个球的体积</FONT>
<P>
<FONT COLOR=#FF0000>r = input('Type radius:');</FONT>
<P>
<FONT COLOR=#FF0000>area=pi*r^2;</FONT>
<P>
<FONT COLOR=#FF0000>volume=(4/3)*pi*r^3;</FONT>
<P>
<FONT COLOR=#FF0000>fprintf('The radius is %12.5f\n',r)</FONT>
<P>
<FONT COLOR=#FF0000>fprintf('The area of a circle is %12.5f\n',area)</FONT>
<P>
<FONT COLOR=#FF0000>fprintf('The volume of a sphere is %12.5f\n',volume)
<BR>
</FONT><HR>
<UL>
<LI> 2.7 如何撰写 MATLAB 程式
<UL>
<LI> 2.7.1 <A HREF="ch2_7_1.htm" tppabs="http://166.111.167.223/computer/cai/matlabjc/ch2_7_1.htm"> 如何在自己的目录执行程式</A>
</UL>
</UL>
<HR>
<A HREF="ch2_6_2.htm" tppabs="http://166.111.167.223/computer/cai/matlabjc/ch2_6_2.htm"><IMG SRC="lastpage.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/lastpage.gif" BORDER=0></A>
<A HREF="ch2_7_1.htm" tppabs="http://166.111.167.223/computer/cai/matlabjc/ch2_7_1.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 + -