一、理解多项式拟合实验内容:对给定的数据如下表x-0.75-0.5-0.2500.250.50.75y0.330.881.442.02.563.133.71分别用MATLAB中的函数polyfit坐一次、二次、三次多项式拟合,并比较优劣。练习1:一次多项式拟合程序:x=-0.75:0.25:0.75;y=[0.330.881.442.002.563.133.71];a=polyfit(x,y,1);xx=-0.75:0.1:0.75;yy=polyval(a,xx);ee1=sum((polyval(a,x)-y).^2)plot(xx,yy,x,y,'*')ee1=5.1429e-004图形:-0.8-0.6-0.4-0.200.20.40.60.800.511.522.533.54练习2:二次多项式拟合程序:x=-0.75:0.25:0.75;y=[0.330.881.442.002.563.133.71];a=polyfit(x,y,2);xx=-0.75:0.1:0.75;yy=polyval(a,xx);ee2=sum((polyval(a,x)-y).^2)plot(xx,yy,x,y,'*')ee2=3.8095e-005图形:-0.8-0.6-0.4-0.200.20.40.60.800.511.522.533.54练习3:三次多项式拟合程序:x=-0.75:0.25:0.75;y=[0.330.881.442.002.563.133.71];a=polyfit(x,y,3);xx=-0.75:0.1:0.75;yy=polyval(a,xx);ee3=sum((polyval(a,x)-y).^2)plot(xx,yy,x,y,'*')ee3=2.1429e-005图形:-0.8-0.6-0.4-0.200.20.40.60.800.511.522.533.54因为ee3ee2ee1,所以三次多项式拟合较好。二、最小二乘拟合经验公式实验内容:某类疾病发病率为y‰和年龄段x(每五年为一段,例如0~5岁为第一段,6~10岁为第二段......)之间有形如bxaey的经验关系,观测得到的数据如下表:x123456789y0.8982.383.071.842.021.942.222.774.02101112131415161718194.765.466.5310.916.522.535.750.661.681.8(1)用最小二乘法确定模型中的参数a和b。(2)利用MATLAB画出离散数据及拟合函数bxaey的图形。(3)利用MATLAB画出离散点处的误差图,并计算相应的均方误差。(4)谈一谈你对最小二乘拟合的理解,并举出一个应用此方法的例子。练习4:用最小二乘法确定模型中的参数a和b两边分别取以自然数为底的对数得:bxaylnln。即Y=bx+ln(a),Y=ln(y)。再做线性拟合如下:程序:x=1:19;y=[0.8982.383.071.842.021.942.222.774.024.765.466.5310.916.522.535.750.661.681.8];Y=log(y);c=polyfit(x,Y,1);a=exp(c(2)),b=c(1)a=0.6814b=0.2306练习5:利用MATLAB画出离散数据及拟合函数bxaey的图形程序:x=1:19;y=[0.8982.383.071.842.021.942.222.774.024.765.466.5310.916.522.535.750.661.681.8];Y=log(y);c=polyfit(x,Y,1);a=exp(c(2)),b=c(1)a=0.6814b=0.2306xx=0:0.2:20;yy=a*exp(b.*xx);plot(xx,yy,x,y,'*')图形:024681012141618200102030405060708090练习6:利用MATLAB画出离散点处的误差图,并计算相应的均方误差程序:x=1:19;y=[0.8982.383.071.842.021.942.222.774.024.765.466.5310.916.522.535.750.661.681.8];Y=log(y);c=polyfit(x,Y,1);a=exp(c(2)),b=c(1)a=0.6814b=0.2306ee=a*exp(b.*x)-y;plot(x,ee,'*',[0:0.1:20],0)error=sqrt(sum(ee.^2))均方误差为:error=38.3491图形:02468101214161820-30-25-20-15-10-505