?? fun_fun.htm
字號:
<html><head><title>學用MatLab</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css"><!--body { font-family: "宋體"; font-size: 9pt; text-decoration: none}h2 { font-family: "楷體_GB2312"; font-size: 18pt; text-decoration: underline; color: #FF9999}h1 { font-family: "隸書"; font-size: 24pt; font-style: italic; font-weight: bolder; color: #CC66CC; text-decoration: blink}.explain { border-color: black black #00FF00; font-weight: bold; color: #333333}.code { font-family: "Arial", "Helvetica", "sans-serif"; font-size: 12pt; background-color: #FFFFFF; line-height: 24pt}h3 { font-size: 12pt; font-style: italic; font-weight: bold; color: #9999FF}--></style></head><body bgcolor="#CCFFCC" text="#666600" link="#009900" alink="#00FF00" vlink="#006600"><h1 align="center">數值分析</h1><p>本節介紹的是關于函數的函數(function functions),這些函數是用來處理函數而非數值的;</p><table width="100%" border="1" cellspacing="0" cellpadding="0" height="317"> <tr> <td width="24%" height="41">類別</td> <td width="76%" height="41"> <table width="100%" border="1" cellspacing="0" cellpadding="0" height="100%"> <tr> <td width="25%">函數</td> <td width="75%">描述</td> </tr> </table> </td> </tr> <tr> <td width="24%" height="120"> <p>繪圖</p> <p>優化</p> <p>求解</p> </td> <td width="76%" valign="top" height="120"> <table width="100%" border="1" cellspacing="0" cellpadding="0" height="100%"> <tr> <td width="25%" height="27">fplot</td> <td width="75%" height="27">畫出函數</td> </tr> <tr> <td width="25%" height="32">fminbnd</td> <td width="75%" height="32">由一有范圍限制的變量找出函數的最小值</td> </tr> <tr> <td height="34" width="25%">fminsearch</td> <td height="34" width="75%">由幾個變量找出函數的最小值</td> </tr> <tr> <td width="25%">fzero</td> <td width="75%">找出函數的解(零值)</td> </tr> </table> </td> </tr> <tr> <td width="24%" height="107">數值積分</td> <td width="76%" height="107"> <table width="100%" border="1" cellspacing="0" cellpadding="0" height="100%"> <tr> <td width="25%" height="35">quad </td> <td width="75%" height="35">低階數值估計積分</td> </tr> <tr> <td width="25%" height="32">quad8</td> <td width="75%" height="32">高階數值估計積分</td> </tr> <tr> <td width="25%">dblquad</td> <td width="75%">二重積分</td> </tr> </table> </td> </tr> <tr> <td width="24%" height="32">數值微分</td> <td width="76%" height="32">見下一章</td> </tr></table><h2>MatLab中的函數表達</h2><p>MatLab中<span class="explain">用M文件來表示函數</span>,設有如下函數:</p><p><img src="image/fun1.jpg" width="342" height="63"></p><p>他別表示為一稱為hump.m的文件中:</p><p class="code">function y = humps(x) <br> y = 1./((x – 0.3).^2 + 0.01) + 1./((x – 0.9).^2 + 0.04) – 6;</p><p>這個函數文件可用于數值分析的函數中.<br> 第二種方法就是<span class="explain">創造一個行內對象(inline())</span>,方法如下:</p><p class="code">f = inline(‘1./((x–0.3).^2 + 0.01) + 1./((x–0.9).^2 + 0.04)–6’);</p><p>用了上面的方法創造了函數文件,我們就可以找出函數在2的值:</p><p class="code">f(2.0) <br> ans = <br> –4.8552 </p><p>用創造<span class="explain">行內對象</span>的方法還可以<span class="explain">創造多參數的函數</span>,如下:</p><p class="code">f= inline('y*sin(x)+x*cos(y)','x','y') <br> f(pi,2*pi) <br> ans = <br> 3.1416</p><h2>把函數畫出來</h2><p>fplot()可畫出在給定范圍內的函數值,如下</p><p class="code">fplot('humps',[–5 5]) <br> grid on</p><p><img src="image/fun2.jpg" width="511" height="408"></p><p>可通過限制y軸來放大圖形</p><p>fplot('humps',[–5 5 –10 25]) <br> grid on</p><p><img src="image/fun3.jpg" width="506" height="390"></p><p>你也可直接<span class="explain">在fplot()中傳遞表達式</span>,如:</p><p class="code">fplot('2*sin(x+3)',[–1 1])</p><p>更可在一附圖中畫多個函數,如下</p><p class="code">fplot('[2*sin(x+3), humps(x)]',[–1 1])</p><p>式中,[2*sin(x+3), humps(x)]組成了一個矩陣,每一列都是對應于x的函數</p><h2>函數的最小值與解</h2><h3>找出一變量的函數的極值</h3><p class="code">x = fminbnd(’humps’,0.3,1)<br> x = <br> 0.6370 </p><p>你可通過向fminbnd()函數傳遞一個函數optimset()作為參數來把此過程顯示為列表形式:</p><p class="code">x = fminbnd(’humps’,0.3,1,optimset(’Display’,’iter’))<br> Func-count x f(x) Procedure <br> 1 0.567376 12.9098 initial <br> 2 0.732624 13.7746 golden <br> 3 0.465248 25.1714 golden <br> 4 0.644416 11.2693 parabolic <br> 5 0.6413 11.2583 parabolic <br> 6 0.637618 11.2529 parabolic <br> 7 0.636985 11.2528 parabolic <br> 8 0.637019 11.2528 parabolic <br> 9 0.637052 11.2528 parabolic <br> x = <br> 0.6370 </p><h3>多變量函數極值</h3><p>先創造一個m文件,three_var.m:</p><p class="code">function b = three_var(v) <br> x = v(1); <br> y = v(2); <br> z = v(3); <br> b = x.^2 + 2.5*sin(y) – z^2*x^2*y^2;</p><p>現在,以x = –0.6, y = –1.2,z = 0.135為起始點找出函數的極值:</p><p class="code">v = [–0.6 –1.2 0.135]; <br> a = fminsearch('three_var',v) <br> a = <br> 0.0000 –1.5708 0.1803</p><h3>設置尋找極值的參數</h3><p><span class="explain">x = fminbnd(fun,x1,x2,options)</span>或<br> <span class="explain">x = fminsearch(fun,x0,options) </span></p><p>其中,options是優化工具箱中(Optimization Toolbox)中的函數所用的一個結構,可如下設置</p><p class="explain">options = optimset('Display','iter');</p><p>options.Display用來設置是否顯示中間過程,如為:"iter"則顯示,為"off"則不顯示,為"final"則只顯示最后結果;<br> options.To1X設置結果的誤差范圍,默認值是:1.e–4.<br> options.MaxFunEval設置函數運行次數的上限,默認fminbnd()是500次,fminsearch()是200*length(x0)次</p><h3>找出函數的解(零點值)</h3><p><span class="explain">fzero()</span>找出函數的零點值,你可以給出一個<span class="explain">起始點</span>,函數會從點開始搜索直到找到一個異號的值,最終給出解;<br> 如果你知道函數會于哪兩點異號,你可以給出一個<span class="explain">兩點的向量,表明起始值和起始搜索步長</span></p><p class="code">a = fzero('humps',–0.2) <br> a = <br> –0.1316 </p><p>驗證一下,此函數值的確很接近0,</p><p class="code">humps(a) <br> ans = <br> 8.8818e –16</p><p>再看看下面的命令,看看你是否能看懂!</p><p class="code">humps(1) <br> ans = <br> 16 <br> humps(–1) <br> ans = <br> –5.1378<br> options = optimset('Display','iter'); <br> a = fzero('humps',[–1 1],options) <br> Func-count x f(x) Procedure <br> 1 –1 –5.13779 initial <br> 1 1 16 initial <br> 2 –0.513876 –4.02235 interpolation <br> 3 0.243062 71.6382 bisection <br> 4 –0.473635 –3.83767 interpolation <br> 5 –0.115287 0.414441 bisection <br> 6 –0.150214 –0.423446 interpolation <br> 7 –0.132562 –0.0226907 interpolation <br> 8 –0.131666 –0.0011492 interpolation <br> 9 –0.131618 1.88371e–07 interpolation <br> 10 –0.131618 –2.7935e–11
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -