📄 ch9_3_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>9.3.2 差分函数</H1>
</FONT>
<HR>
<P>
上述提及的后向差分式<IMG SRC="img00019-2.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img9/img00019.gif">,在<FONT FACE="Times New Roman">
MATLAB </FONT>有对应的<FONT COLOR=#FF0000 FACE="Times New Roman">diff</FONT><FONT FACE="Times New Roman">
</FONT>函数来计算二相临点的差值,
它的语法为<FONT FACE="Times New Roman">
</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">diff(x)</FONT>,其中<FONT FACE="Times New Roman">
</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">x</FONT>代表一组离散点<FONT FACE="Times New Roman">
<IMG SRC="img00020-2.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img9/img00020.gif"></FONT>。假设有<I><FONT FACE="Times New Roman">x</FONT></I><FONT FACE="Times New Roman">,
<I>y</I>(<I>x</I>)</FONT>的数据为
<P>
<I>x</I>=[1 3 5 7 9], <I>y</I>=[1 4 9 16 25]
<P>
则<FONT FACE="Times New Roman"> </FONT><FONT COLOR=#FF0000 FACE="Times New Roman">diff(x)=[2 2 2 2]</FONT><TT><FONT FACE="Courier New">, </FONT></TT><FONT COLOR=#FF0000 FACE="Times New Roman">diff(y)=[3 5 7 9]</FONT>,注意二者皆以后向差分计算且数据点只剩<FONT FACE="Times New Roman"> 4 </FONT>个而不是<FONT FACE="Times New Roman">5</FONT>个。而<IMG SRC="img00021-2.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img9/img00021.gif">的
数值微分则为<FONT FACE="Times New Roman">
</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">dy=diff(y)./diff(x)</FONT>。
<BR>
<P>
因此要计算下列多项式在<FONT FACE="Times New Roman"> [-4, 5] </FONT>区间的微分
<P>
<IMG SRC="img00022-1.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img9/img00022.gif">
<P>
可依以下方式求解
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> x=linspace(-4,5);
% </FONT><FONT COLOR=#FF0000>产生</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">100</FONT><FONT COLOR=#FF0000>个</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">x</FONT><FONT COLOR=#FF0000>的离散点</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> p=[1 -3 -11
27 10 -24];</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> f=polyval(p,x);</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> plot(x,f)
% </FONT><FONT COLOR=#FF0000>将多项式函数绘图</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> title('Fifth-deg.
equation')</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> dfb=diff(f)./diff(x);
% </FONT><FONT COLOR=#FF0000>注意要分别计算</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">diff(f)</FONT><FONT COLOR=#FF0000>和</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">diff(x)</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> xd=x(2:length(x));
% </FONT><FONT COLOR=#FF0000>注意只有</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">99</FONT><FONT COLOR=#FF0000>个</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">df</FONT><FONT COLOR=#FF0000>值,而且是对应</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">x2,x3,...,x100</FONT><FONT COLOR=#FF0000>的点</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> plot(xd,dfb
) % </FONT><FONT COLOR=#FF0000>将多项式的微分项绘图</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> title('Derivative
of fifth-deg. equation')<BR>
</FONT>
<P>
假设我们要找出上述多项式的局部极值<FONT FACE="Times New Roman">(local critical value)</FONT>,可以利用局部极值所在点的微分为零,所以在
这个点的左侧及右侧的二点微分值一定是一正一负,因此这二点的微分值相乘为负值,可针对此性质找
出局部极值所在点。以原多项式的数据示范如下:
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> product=dfb(1:length(dfb)-1).*dfb(2:length(dfb));
% </FONT><FONT COLOR=#FF0000>注意二相临点值相乘的写法</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> crit=xd(find(product<0))
% </FONT><FONT COLOR=#FF0000>注意用到</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">find</FONT><FONT COLOR=#FF0000>指令
<BR>
</FONT>
<P>
若上述多项式以中央差分方式计算其函数微分项,就不能以<FONT COLOR=#FF0000 FACE="Times New Roman">diff</FONT>计算,而须自行计算如下:
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> num=f(3:length(f))-f(1:length(f)-2);
% </FONT><FONT COLOR=#FF0000>注意中央差分是</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">
f(k+1)-f(k-1)</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> deno=x(3:length(f))-x(1:length(f)-2);
% </FONT><FONT COLOR=#FF0000>注意中央差分是</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">
x(k+1)-x(k-1)</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> df_c=num./deno;</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> xd=x(2:length(x)-1);
% xd</FONT><FONT COLOR=#FF0000>的点数只有</FONT><FONT COLOR=#FF0000 FACE="Times New Roman">98</FONT><FONT COLOR=#FF0000>个</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> plot(xd,df_c)</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> title('Derivative
of fifth-deg. polynomial')<BR>
</FONT>
<P>
以下的例子是针对数据组为离散型态,要注意的是原数据所代表的函数分布并无明显的折角,但是它的
一阶微分经数值微分计算后的微分函数分布却有极大的曲折变化。
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> x=0:0.1:1;</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> y=[-.447 1.978
3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2];</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> plot(x,y,'o',x,y)</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> title('y(x)
data plot')</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> ylabel('y(x)'),
xlabel('x')</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> dy=diff(y)./diff(x);</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> xd=x(1:length(x)-1);</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> plot(xd,dy)</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> title('Approximate
derivative using diff')</FONT>
<P>
<FONT COLOR=#FF0000 FACE="Times New Roman">>> ylabel('dy/dx'),
xlabel('x')<BR>
</FONT><HR>
<A HREF="ch9_3_1.htm" tppabs="http://166.111.167.223/computer/cai/matlabjc/ch9_3_1.htm"><IMG SRC="lastpage.gif" tppabs="http://166.111.167.223/computer/cai/matlabjc/img/lastpage.gif" BORDER=0></A>
<A HREF="ch10.htm" tppabs="http://166.111.167.223/computer/cai/matlabjc/ch10.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 + -