資源描述:
《基于matlab的數(shù)值分析(6)》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、第六章數(shù)值逼近6.1關(guān)于多項(xiàng)式的MATLAB命令一、多項(xiàng)式表達(dá)方式的約定多項(xiàng)式用行向量表示用比較習(xí)慣的方式顯示多項(xiàng)式:pp=poly2str(p,'x')【例】多項(xiàng)式可表示為p=[2145]pp=poly2str(p,'x')二、多項(xiàng)式運(yùn)算函數(shù)r=roots(p):求多項(xiàng)式的零點(diǎn)p=poly(r):以r為零點(diǎn)的多項(xiàng)式p=poly(A):A的特征多項(xiàng)式PA=polyval(p,S):按數(shù)組運(yùn)算規(guī)則,計(jì)算多項(xiàng)式的值其中S,PA為矩陣PM=polyvalm(p,S):按矩陣運(yùn)算規(guī)則,計(jì)算多項(xiàng)式的值,其中S,PM為矩陣p=conv(p1,p2):多項(xiàng)式的乘積[q,r]=deconv(p1,
2、p2):多項(xiàng)式的除法,p1/p2p1(x)=p2(x)q(x)+r(x)【例】由給定根向量求多項(xiàng)式系數(shù)向量。R=[-0.5,-0.3+0.4*i,-0.3-0.4*i];P=poly(R)PPR=poly2str(P,'x')P=1.00001.10000.55000.1250PPR=x^3+1.1x^2+0.55x+0.125【例】求多項(xiàng)式的零點(diǎn)。r=roots([1-615-2015-61])r=1.0042+0.0025i1.0042-0.0025i1.0000+0.0049i1.0000-0.0049i0.9958+0.0024i0.9958-0.0024i注:盡管利用MA
3、TLAB使得從系數(shù)轉(zhuǎn)換到零點(diǎn)或從零點(diǎn)轉(zhuǎn)換到系數(shù)都非常容易,但是使用時(shí)一定要注意計(jì)算的精度。如果存在重根,這種轉(zhuǎn)換可能會(huì)降低精度。對(duì)于數(shù)值計(jì)算,計(jì)算重根是最困難的問(wèn)題之一?!纠壳?階方陣A的特征多項(xiàng)式。A=[111213;141516;171819];PA=poly(A)PPA=poly2str(PA,'x')PA=1.0000-45.0000-18.0000-0.0000PPA=x^3-45x^2-18x-2.8387e-015【例】求的“商”及“余”多項(xiàng)式。p1=conv([1,0,2],conv([1,4],[1,1]));p2=[1011];[q,r]=deconv(p1,
4、p2);cq='商多項(xiàng)式為';cr='余多項(xiàng)式為';disp([cq,poly2str(q,'x')])disp([cr,poly2str(r,'x')])商多項(xiàng)式為x+5余多項(xiàng)式為5x^2+4x+3【例】?jī)煞N多項(xiàng)式求值指令的差別。S=pascal(4)P=poly(S);PP=poly2str(P,'s')PA=polyval(P,S)PM=polyvalm(P,S)S=1111123413610141020PP=s^4-29s^3+72s^2-29s+1PM=1.0e-010*0.00160.00330.00900.02050.00450.01010.02860.06970.0
5、0950.02100.06530.15960.01630.03870.12260.3019PA=1.0e+004*0.00160.00160.00160.00160.00160.0015-0.0140-0.05630.0016-0.0140-0.2549-1.20890.0016-0.0563-1.2089-4.3779可以用命令polyval或polyvalm計(jì)算多項(xiàng)式的值p=[2145];xi=2.5;yi=polyval(p,xi)yi=52.5000yi=polyvalm(p,xi)yi=52.5000【例】求多項(xiàng)式的積分poly_itg.mfunctionpy=poly_
6、itg(p)n=length(p)py=[p.*[n:-1:1].^(-1),0]【例】將Chebyshev多項(xiàng)式展開(kāi)為冪級(jí)數(shù)形式Cheby_pw.mfunctionpn=Cheby_pw(n)pbb=[1];ifn==0,pn=pbb;break;endpb=[10];ifn==1,pn=pb;break;endfori=2:npn=2*[pb,0]-[0,0,pbb];pbb=pb;pb=pn;end6.2Lageange插值多項(xiàng)式求由n+1維向量x,y給定數(shù)據(jù)的n次插值多項(xiàng)式pp=polyfit(x,y,n)【例】觀察Lagrange插值基函數(shù)的形狀x=[12345678];
7、y=[01000000];p=polyfit(x,y,7);x0=1:0.2:8;y0=polyval(p,x0);plot(x,y,'ro',x0,y0)Lagr1.mfunctiony=lagr1(x0,y0,x)n=length(x0);m=length(x);fori=1:mz=x(i);s=0.0;fork=1:np=1.0;forj=1:nifj~=kp=p*(z-x0(j))/(x0(k)-x0(j));endends=p*y0(k)+s;endy(i)