matlab多项式7M文件举例

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

M文件举例
本章说明了在精通MATLAB工具箱中两个函数。

这些函数说明了本章所论述的多项式概念和如何写M文件函数。

关于M文件的更多信息,参阅第8章。

在讨论M文件函数的内部结构之前,我们考虑这些函数做些什么。

» n%earlier data
n =
0.000010.000020.0000
» b%earlier data
b =
-20-140-320-260
» mmpsim(n)%strip away negligible leading term
ans =
10.000020.0000
» m mp2str(b)%convert polynomial to string
ans =
-20s^3 - 140s^2 - 320s^1 - 260
» mmp2str(b , ' x ')
ans =
-20x^3 - 140x^2 - 320x^1 - 260
» mmp2str(b , [] , 1)
ans =
-20*(s^3 + 7s^2 + 16s^1 + 13)
» mmp2str(b , ' x ' , 1)
ans =
-20*(x^3 + 7x^2 + 16x^1 + 13)
这里函数mmpsim删除在多项式n中近似为零的第一个系数。

函数mmp2str把数值多项式变换成等价形式的字符串表达式。

该两个函数的主体是:
function y=mmpsim(x,tol)
%MMPSIM Polynomial Simplification,Strip Leading Zero Terms.
%MMPSIM(A) Delets leading zeros and small coefficients in the %polynomial A(s). Coefficients are considered small if their %magnitude is less than both one and norm(A)*1000*eps.
%MMPSIM(A,TOL) uses TOL for its smallness tolerance.
%Copyright (c) 1996 by Prentice-Hall,Inc.
if nargin<2, tol=norm(x)*1000*eps; end
x=x(:).' ;%make sure input is a row
i=find(abs(x)<.99&abs(x)<tol) ;%find insignificant indices
x(i)=zeros(1, length(i)) ;%set them to zero
i=find(x~=0) ;%find significant indices
if isempty(i)
y=0 ;%extreme case: nothing left!
else
y=x(i(1) : length(x)) ;%start with first term
end%and go to end of polynomial
function s=mmp2str(p,v,ff)
%MMP2STR Polynomial Vector to String Conversion.
%MMP2STR(P) converts the polynomial vector P into a string.
%For example: P = [234] becomes the string ' 2s^2 + 3s + 4 ' %
%MMP2STR(P,V) generates the string using the variable V
%instead of s. MMP2STR([234],' z ') becomes ' 2z^2 + 3z + 4 ' %
%MMP2STR(P,V,1) factors the polynomial into the product of a %constant and a monic polynomial.
%MMP2STR([234],[],1) becomes ' 2(s^2 + 1.5s + 2) '
%Copyright (c) 1996 by Prentice-Hall,Inc.
if nargin<3, ff=0; end%factored form is False
if nargin <2, v=' s ' ; end%default variable is ' s ' if isempty(v), v=' s ' ; end%default variable is ' s ' v=v(1) ;%variable must be scalar
p=mmpsim(p) ;%strip insignificant terms
n=length(p) ;
if ff%put in factored form
K=p(1) ; Ka=abs(K) ; p=p/K;
if abs(K-1)<1e-4
pp=[]; pe=[] ;
elseif abs(K+1)<1e-4
pp=' -(' ; pe= ') ' ;
elseif abs(Ka-round(Ka))<=1e-5*Ka
pp=[sprintf(' %.0f ', K) '*( ' ] ; pe= ') ' ;
else
pp=[sprintf(' %.4g ' , K) '*(' ] ; pe= ') ' ;
end
else%not factored form
K=p(1);
pp=sprintf(' %.4g ' , K) ;
pe=[];
end
if n==1%polynomial is just a constant
s=sprintf(' %.4g ',K);
return
end
s=[pp v ' ^ ' sprintf(' %.0f ',n-1)];%begin string construction for i=2:n-1%catenate center terms in polynomial
if p(i)<0, pm= ' -' ;else,if p(i)<0,pm= ' ;end
if p(i)= =1,pp=[] ; else,pp=sprintf(' %.4g ', abs(p(i))) ;end if p(i)~ =0,s=[spmppv' ^ ' sprintf(' %.0f ',n-i)] ;end
end
if p(n)~ =0,pp=sprintf(' %.4g ',abs(p(n))); else,pp=[];end
if p(n)<0 , pm= ' -' ; elseifp(n)>0 , pm= ' +' ; else,pm=[];end s=[spmpppe];%add final terms。

相关文档
最新文档