如何用matlab进行数据拟合,在进行数据的估计?

Here are two vectors representing the census years from 1810 to 1900 and the corresponding population of a certain country in millions of people:
1810 到 1900 年每隔10年人口如下
[74.875 92.552 107.231 120.153 130.879 152.427 180.383 202.352 227.485 250.597];
Please use MATLAB to estimate the population in 1865, you must hand in your source code and plot the result.
最好有matlab的源代码

clear

clf

x=1810:10:1900;

y=[74.875 92.552 107.231 120.153 130.879 152.427 180.383 202.352 227.485 250.597];

plot(x,y,'s','markersize',3)

grid on

%画图并观察离散数据的特性

p=polyfit(x,y,1);

%用1次多项式进行拟合

f = polyval(p,x);

hold on

plot(x,f,'r');

xlabel('年份')

ylabel('人口')

title('拟合曲线')

%在同一个坐标内画出拟合曲线和原有离散数据

p

%显示拟合多项式系数

p1865=polyval(p,[1865])

%估计1865年的人口数量

#################################################################

运行结果:

p =

  1.0e+003 *

    0.0019   -3.4480

p1865 =

  173.3108

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-05-12
一般的拟合:p=curvefit(‘Fun’,p0,xdata,ydata)
其中Fun表示函数Fun(p,data)的M函数文件,p0表示函数的初值.curvefit()命令的求解问题形式是
若要求解点x处的函数值可用程序f=Fun(p,x)计算.
例如已知函数形式 ,并且已知数据点 要确定四个未知参数a,b,c,d.
使用curvefit命令,数据输入 ;初值输 ;并且建立函数 的M文件(Fun.m).若定义 ,则输出
又如引例的求解,MATLAB程序:
t=[l:16]; %数据输人
y=[ 4 6.4 8 8.4 9.28 9.5 9.7 9.86 10.2 10.32 10.42 10.5 10.55 10.58 10.6] ;
plot(t,y,’o’) %画散点图
p=polyfit(t,y,2) (二次多项式拟合)
计算结果:
p=-0.0445 1.0711 4.3252 %二次多项式的系数
由此得到某化合物的浓度y与时间t的拟合函数。本回答被网友采纳