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