1、,xj);%定义多项式quan=inline(1%权函数A=zeros(n+1);y=zeros(1,n+1);for k=1:(n+1) for j=1:(n+1) syms x A(k,j)=int(base(x,k)*base(x,j)*quan(x),x,a,b);%构建希尔伯特矩阵 end y(k)=int(base(x,k)*(x2+3*x+2),x,a,b);%求dend% A;% y;c=vpa(inv(A)*y,3)%求系数%vpa控制精度保存3位有效数字 ,digits()与vpa()合用,控制精度S=0;for i=1: S=S+c(i)*base(x,i);(1)一次最
2、佳平方逼近命令行: zjpfbj(1,0,1) c = 1.83 4.0ans =4.0*x + 1.8333333333430346101522445678711画图: fun=x2+3*x+2fplot(fun,0,1)hold onxi=0:0.1:1;yi=4.0*xi + 1.8333333333430346101522445678711;plot(xi,yi,r:)(2)二次最佳平方逼近 zjpfbj(2,0,1) 2.0 3.0 1.0ans =1.0*x2 + 3.0*x + 2.0yi=xi.2 + 3*xi + 2;18. 用最小二乘法求。function S=zuixia
3、o(xi,yi,m)%xi-自变量%yi-应变量%m-拟合次数%a-解超定方程组的最小二乘解A=zeros(m+1,m+1);for i=0:m for j=0: A(i+1,j+1)=sum(xi.(i+j); b(i+1)=sum(xi.i.*yi);a=AbaS=fliplr(a%使翻转%c=p;%c=0.0000,-0.0049,0.2557,0.0442%f=polyval(c,xi);%拟合f=polyval(p,xi);b*plot(xi,f,r-disp(拟和方程系数按照降幂排列如下(1)二次拟合 xi=0,5,10,15,20,25,30,35,40,45,50,55;yi=0,1.27,2.16,2.86,3.44,3.87,4.15,4.37,4.51,4.58,4.62,4.64;zuixiao(xi,yi,2)拟和方程系数按照降幂排列如下 -0.0022 0.1992 0.2453(2)三次拟合 zuixiao(xi,yi,3) 0.0000 -0.0049 0.2557 0.0442问题,同是三次拟合效果不同:function p=zuixiao(xi,yi,m)p=fliplr(ac=0.0000,-0.0049,0.2557,0.0442f=polyval(c,xi)