?? ch2_2_2.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>2.2.2 簡易數學</H1>
</FONT>
<HR>
<P>
我們先從MATLAB的數學運算開始說明。就像你的計算器一樣,數學式的計算是直接了當。如果我們要算
1+2+3及1×10+2×20+3×30這二個式子,以下例子接著提示符號>>之后的是要鍵入的算式,MATLAB將計算
的結果以<FONT COLOR=#FF0000>ans</FONT>顯示。如果算式是x=1+2+3,MATLAB將計算的結果以<FONT COLOR=#FF0000>x</FONT>顯示。
<P>
<FONT COLOR=#FF0000>>> 1+2+3</FONT>
<P>
<FONT COLOR=#FF0000>ans =</FONT>
<P>
<FONT COLOR=#FF0000>6</FONT>
<P>
<FONT COLOR=#FF0000>>> 1*10 + 2*20 + 3*30</FONT>
<P>
<FONT COLOR=#FF0000>ans =</FONT>
<P>
<FONT COLOR=#FF0000>140</FONT>
<P>
<FONT COLOR=#FF0000>>> x=1+2+3</FONT>
<P>
<FONT COLOR=#FF0000>x =</FONT>
<P>
<FONT COLOR=#FF0000>6</FONT>
<P>
如果在上述的例子結尾加上<FONT COLOR=#FF0000>;</FONT>,則計算結果不會顯示在指令視窗上,要得知計算值只須鍵入該變數值即可
<P>
<FONT COLOR=#FF0000>>> x=1+2+3;</FONT>
<P>
<FONT COLOR=#FF0000>>> x</FONT>
<P>
<FONT COLOR=#FF0000>x = </FONT>
<P>
<FONT COLOR=#FF0000>6<BR>
</FONT>
<P>
以下的例子,顯示MATLAB對使用變數的彈性
<P>
<FONT COLOR=#FF0000>>> apple=5</FONT>
<P>
<FONT COLOR=#FF0000>apples =</FONT>
<P>
<FONT COLOR=#FF0000>5</FONT>
<P>
<FONT COLOR=#FF0000>>> orange=10</FONT>
<P>
<FONT COLOR=#FF0000>orange =</FONT>
<P>
<FONT COLOR=#FF0000>10</FONT>
<P>
<FONT COLOR=#FF0000>>> total_cost=apple*2+orange*4</FONT>
<P>
<FONT COLOR=#FF0000>total_cost =</FONT>
<P>
<FONT COLOR=#FF0000>50</FONT>
<P>
<FONT COLOR=#FF0000>>> average_cost=total_cost/(apple+orange)</FONT>
<P>
<FONT COLOR=#FF0000>average_cost =</FONT>
<P>
<FONT COLOR=#FF0000> 3.33334<BR>
</FONT>
<P>
MATLAB提供基本的算術運算有:
<P>
<FONT FACE="華康仿宋體">加</FONT> (<FONT COLOR=#FF0000>+</FONT>)、<FONT FACE="華康仿宋體">減</FONT>
(<FONT COLOR=#FF0000>-</FONT>)、<FONT FACE="華康仿宋體">乘</FONT>
(<FONT COLOR=#FF0000>*</FONT>)、<FONT FACE="華康仿宋體">除</FONT>
(<FONT COLOR=#FF0000>/</FONT>)、<FONT FACE="華康仿宋體">冪次方</FONT>
(<FONT COLOR=#FF0000>^</FONT>),<FONT FACE="華康仿宋體">范例為:</FONT><FONT COLOR=#FF0000>5+3</FONT>,
<FONT COLOR=#FF0000>5-3</FONT>, <FONT COLOR=#FF0000>5*3</FONT>,
<FONT COLOR=#FF0000>5/3</FONT>, <FONT COLOR=#FF0000>5^3<BR>
</FONT>
<P>
其它在計算常用的功能我們來看一個算式來說明。要計算面積Area = <IMG SRC="img00001.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img2/img00001.gif"><TT><FONT FACE="Courier New">,</FONT></TT>半徑<I>r
</I>= 2,則可鍵入
<P>
<FONT COLOR=#FF0000>>> r=2;</FONT>
<P>
<FONT COLOR=#FF0000>>> area=pi*r^2;</FONT>
<P>
<FONT COLOR=#FF0000>>> area =</FONT>
<P>
<FONT COLOR=#FF0000>12.5664<BR>
</FONT>
<P>
我們也可以將上述指令打在同一行,以<FONT COLOR=#FF0000>, </FONT>或是<FONT COLOR=#FF0000>;
</FONT>分開,例如
<P>
<FONT COLOR=#FF0000>>> r=2, area=pi*r^2</FONT>
<P>
<FONT COLOR=#FF0000>>> r=2; area=pi*r^2;<BR>
</FONT>
<P>
請注意上述二式的差異,前者有計算值顯示,而后者則無。如果一個指令過長可以在結尾加上<FONT COLOR=#FF0000>...</FONT>(代表此行指
令與下一行連續),例如
<P>
<FONT COLOR=#FF0000>>> r=2;</FONT>
<P>
<FONT COLOR=#FF0000>>> area = pi ...</FONT>
<P>
<FONT COLOR=#FF0000>*r^2<BR>
</FONT>
<P>
另外一個符號注解是由<FONT COLOR=#FF0000>%</FONT>起頭,也就是說在<FONT COLOR=#FF0000>%</FONT>之后的任何文字都被視為程式的注解。注解的功能是簡要的說明
程式的內容,過多的注解在程式中或許沒有必要,但是我們寫程式時往往用了太少的注解。任何可能產生混淆
的地方都應該省用注解,將適量的注解可在往后想了解程式時能節省一些不必要的時間與「有看沒有懂」的痛
苦。例如
<P>
<FONT COLOR=#FF0000>>> r=2; % </FONT><FONT COLOR=#FF0000 FACE="華康仿宋體">鍵入半徑</FONT>
<P>
<FONT COLOR=#FF0000>>> area=pi*r^2; % </FONT><FONT COLOR=#FF0000 FACE="華康仿宋體">計算面積
<BR>
</FONT>
<P>
MATLAB可以將計算結果以不同的精確度的數字格式顯示,我們可以在指令視窗上的功能選單上的<B>Options</B>下
選 <B>Numerical Format</B>,或者直接在指令視窗鍵入以下的各個數字顯示格式的指令,以π值為例
<TABLE BORDER=1>
<TR><TD WIDTH=106>指令</TD><TD WIDTH=160>數字值</TD><TD WIDTH=272>說明
</TD></TR>
<TR><TD WIDTH=106><FONT COLOR=#FF0000>format short</FONT></TD>
<TD WIDTH=160>3.1416</TD><TD WIDTH=272>預設的 4 位有效小數位數
</TD></TR>
<TR><TD WIDTH=106><FONT COLOR=#FF0000>format long</FONT></TD>
<TD WIDTH=160>3.14159265358979</TD><TD WIDTH=272>15 位有效小數位數
</TD></TR>
<TR><TD WIDTH=106><FONT COLOR=#FF0000>format short e</FONT></TD>
<TD WIDTH=160>3.1416e+000</TD><TD WIDTH=272>4 位有效小數位數加上指數表格式
</TD></TR>
</TABLE><HR>
<A HREF="ch2_2_1.htm" tppabs="http://166.111.167.223/computer/cai/matlabjc/ch2_2_1.htm"><IMG SRC="lastpage.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/lastpage.gif" BORDER=0></A>
<A HREF="ch2_2_3.htm" tppabs="http://166.111.167.223/computer/cai/matlabjc/ch2_2_3.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="javascript:if(confirm('http://166.111.167.223/computer/tppmsgs/msgs0.htm \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://166.111.167.223/computer/tppmsgs/msgs0.htm#67'" tppabs="http://166.111.167.223/computer/tppmsgs/msgs0.htm#67"><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 + -