模式识别_周晓勇_Z2011020211期末作业1.给定一个方程组2334642zyxzyxzyx试建立一个收敛的迭代格式,并说明收敛的理由解:A=[214;141;311];b=[632]';%对A|b进行行变换,使A为严格对角占优阵%如果A为严格对角占优阵,则解Ax=b的Jacobi迭代法和Gauss-Seidel迭代法均收敛A=[311;141;214];b=[236]';A\b%正确答案w=[1:20]*0.1;fori=1:20[x,k,flag]=SOR(A,b,1e-5,w(i),100);%函数见SOR.mresult(:,i)=[w(i);flag;k;x];end[CI]=min(result(3,:));plot(w,result(2,:).*result(3,:),w(I),C,'r*');xlabel('w');ylabel('k');legend('k-w曲线',['k=',num2str(C),',\omega=',num2str(w(I))]);title(['方程组解为:',num2str(result(4:end,I)','%10.4f')]);结果:00.20.40.60.811.21.41.61.82010203040506070wk方程组解为:0.08330.38891.3611k-w曲线k=8,w=12.证明方程0523xx在区间]20[内有根,并建立收敛的迭代格式,使用任意初值]20[0x都收敛,并说明理由模式识别_周晓勇_Z2011020212解:[x,err,k,y]=NewtonDownhill('x^3+2*x-5',1,10^(-5),30)%函数见NewtonDownhill.m结果:-2.5-2-1.5-1-0.500.511.522.5-30-25-20-15-10-505101520f(x)=x3+2*x-5根x=1.3283迭代次数k=43.当X=-1,1,2时f(x)分别为-3,0,4利用基函数法,求f(x)的二次插值多项式解:x=[-112];y=[-304];p=polyfit(x,y,2);f=poly2str(p,'x');x1=-5:0.01:5;y1=polyval(p,x1);plot(x1,y1,x,y,'o');legend(['f(x)=',f],'节点');结果:-5-4-3-2-1012345-5051015202530f(x)=0.83333x2+1.5x-2.3333节点模式识别_周晓勇_Z20110202134.已知x、y的值,用线性插值和二次插值计算x=0.54的近似值x0.40.50.60.70.8y-0.916291-0.693147-0.510826-0.357765-0.223144解:x=[0.40.50.60.70.8];y=[-0.916291-0.693147-0.510826-0.357765-0.223144];x0=0:0.01:1;p1=newpoly(x,y,1,0.54);%函数见newpoly.my1=polyval(p1,0.54);p2=newpoly(x,y,2,0.54);y2=polyval(p2,0.54);y01=polyval(p1,x0);y02=polyval(p2,x0);subplot(1,2,1),plot(x,y,'o',x0,y01,0.54,y1,'r*');h1=legend('插值节点',['N1(x)=',poly2str(p1,'x')],['N1(0.54)=',num2str(y1)]);set(h1,'Location','SouthEast','FontSize',20);subplot(1,2,2),plot(x,y,'o',x0,y02,0.54,y2,'r*');h2=legend('插值节点',['N2(x)=',poly2str(p2,'x')],['N2(0.54)=',num2str(y2)]);set(h2,'Location','SouthEast','FontSize',20);结果:00.10.20.30.40.50.60.70.80.91-2-1.5-1-0.500.5插值节点N1(x)=1.8232x-1.6048N1(0.54)=-0.6202200.10.20.30.40.50.60.70.80.91-2.5-2-1.5-1-0.50插值节点N2(x)=-2.0412x2+4.0685x-2.2171N2(0.54)=-0.615325.用最小二乘法求一个二阶方程2bxay,使其与下列数据相拟合,请求均方误差,绘图x-1.0-0.50.00.51.01.52.0y-4.447-0.4520.551-0.0480.4470.5494.552解:%%期末作业No.5:方法1用函数lsqcurvefit实现xdata=[-1.0-0.50.00.51.01.52.0];模式识别_周晓勇_Z2011020214ydata=[-4.447-0.4520.5510.048-0.4470.5494.552];F=inline('x(1)+x(2).*xdata.^2','x','xdata');x0=[0;0];[x,resnorm]=lsqcurvefit(F,x0,xdata,ydata);plot(xdata,x(1)+x(2).*xdata.^2,xdata,ydata,'*r');legend(['y=',poly2str([x(2)0x(1)],'x'),...',\delta=',num2str(sqrt(resnorm))]);%%期末作业No.5:方法2求内积解方程组xdata=[-1.0-0.50.00.51.01.52.0];ydata=[-4.447-0.4520.5510.048-0.4470.5494.552];A=zeros(2,2);B=zeros(2,1);fori=1:7A(1,1)=A(1,1)+1^2;A(1,2)=A(1,2)+xdata(i)^2;A(2,2)=A(2,2)+xdata(i)^4;B(1,1)=B(1,1)+ydata(i);B(2,1)=B(2,1)+xdata(i)^2*ydata(i);endA(2,1)=A(1,2);x=A\B;%等价于inv(A)*Bdelta=0;fori=1:7delta=delta+((x(1)+x(2)*xdata(i)^2)-ydata(i))^2;enddelta=sqrt(delta);plot(xdata,x(1)+x(2).*xdata.^2,xdata,ydata,'*r');legend(['y=',poly2str([x(2)0x(1)],'x'),...',\delta=',num2str(delta)]);结果:-1-0.500.511.52-5-4-3-2-1012345y=1.1433x2-1.3786,=5.0475模式识别_周晓勇_Z20110202156.利用(1)科特斯算法,(2)高斯算法,(3)龙贝格算法计算1024xx解:%(1)NewtonCotes[y,Ck,Ak]=NewtonCotes(@(x)(x./(4+x.^2)),0,1,4)%函数见NewtonCotes.m结果:y=0.1116Ck=0.07780.35560.13330.35560.0778Ak=0.07780.35560.13330.35560.0778%(2)GaussLegendrey=GaussLegendre('x./(4+x.^2)',0,1,4)%函数见GaussLegendre.m结果:y=0.1116%(3)Romberg[y,T,err,h]=Romberg('x./(4+x.^2)',0,1,4)%函数见Romberg.m结果:y=0.1116T=0.10000000.10880.1118000.11090.11160.111600.11140.11160.11160.1116err=2.1240e-006h=0.12507.利用牛顿弦切法求非线性方程xey1在0.2附近的根解:[x,err,k,y]=NewtonDownhill('1-exp(-x^2)',0.2,10^(-5),30)%函数见NewtonDownhill.m结果:-2.5-2-1.5-1-0.500.511.522.500.10.20.30.40.50.60.70.80.91f(x)=1-exp(-x2)根x=5.9416e-006迭代次数k=15模式识别_周晓勇_Z20110202168.已知31022021225321321321xxxxxxxxx实现高斯-赛德尔迭代,要求精度10-4解:A=[521;-112;2-310];b=[-12;20;3];A\b%精确解[x,k,flag]=SOR(A,b,1e-4,1,100)%函数见附件SOR.m,w=1时即为高斯塞德尔迭代结果:ans=-5.89916.96333.5688x=-5.89916.96333.5688k=66flag=1平时作业1.已知函数)(xf值及差商表,求(1))(4xN来近似)596.0(f(2))(2xN和)(3xN来近似)596.0(f,并比较(1)和(2)的误差0x0.41x0.5541075.0)(0xf2x0.6511600.1),(10xxf3x0.828000.0),,(210xxxf4x0.919733.0),,,(3210xxxxf5x1.0503134.0),,,,(43210xxxxxf解:见作业本Page12.已知)7,4,3,1(ix和)12,15,2,0()(ixf,计算)7,4,3,1(f,并求)(3xN解:模式识别_周晓勇_Z2011020217x=[1347];y=[021512];p=newpoly(x,y,3);%函数见newpoly.mx1=0:0.1:10;y1=polyval(p,x1);plot(x1,y1,x,y,'ro');h=legend(['N3(x)=',poly2str(p,'x')],'插值节点');set(h,'Location','SouthWest');结果:012345678910-250-200-150-100-50050N3(x)=-1.25x3+14x2-38.75x+26插值节点3.已知离散数据点)6.2,4.2,2.2,0.2,8.1(ix)46675.10,03014.8,04241.6,42569.4,12014.3()(ixf用(1)左矩形公式(2)中矩形公式(3)右矩形公式(4)Newton-Cotes公式,求6.28.1)()(xdxf解:x=[1.82.02.22.42.6];y=[3.120144.425696.042418.0301410.46675];qleft=rectint(x,y,x(1),x(end),'left')%函数见rectint.mqright=rectint(x,y,x(1),x(end),'right')qmiddle=rectint(x,y,x(1),x(end))[y,Ck,Ak]=NewtonCotes([x;y]')%函数见NewtonCotes.m结果:qleft=4.3237qright=5.7930qmiddle=5.0583y=5.0329模式识别_周晓勇_Z20110202184.用(1)复化梯形法(2)复化辛普森法,求10)(xdex,要求具有6位有效数字解:y1=mulNewtonCotes('exp(x)',0,1,1,Nan,1e-6)%函数见mulNewtonCotes.my2=mulNewtonCotes('exp(x)',0,1,2,NaN,1e-6)结果:y1=1.7183y2=1.71835.用