Matlab之用最小二乘建立模型预测值以下例子使用1960,1970,1990和2000的人口估计1980的人口。分别用了直线估计和抛物线估计
2014-06-30 01:05
465 查看
以下例子使用1960,1970,1990和2000的人口估计1980的人口。分别用了直线估计和抛物线估计。
% page 199 3
% this problem is to estimate the populatipn in 1980 and
% compare the error using different method to estimate
% one use line and the other is parabale
% input:none
% output:plot the figure and display error
function page_199_3_script
format long;
x0 = [0 10 30 40];
y0 = [3039585530 3707475887 5281653820 6079603571];
x1 = 0:.01:450;
c1 = polyfit(x0,y0,1);
error1 = norm(polyval(c1,x0)-y0,2);
fprintf('使用直线拟合所得的RMSE为 %f\n',error1/2);
y1_1980 = polyval(c1,20);
fprintf('使用直线拟合的1980的人口 %f\n',y1_1980);
fprintf('拟合与实际上人口的误差为 %f\n\n',y1_1980-4452584592);
y1 = polyval(c1,x1);
figure(1);
plot(x0,y0,'o',x1,y1);
xlabel('let 1960 = 0/year');
ylabel('population');
title('用直线拟合最小二乘所得的结果');
c2 = polyfit(x0,y0,2);
error2 = norm(polyval(c2,x0)-y0,2);
fprintf('使用抛物线拟合所得的RMSE为 %f\n',error2/2);
y2_1980 = polyval(c2,20);
fprintf('使用抛物线拟合的1980的人口 %f\n',y2_1980);
fprintf('拟合与实际上人口的误差为: %f\n',y2_1980-4452584592);
fprintf('我们从误差来看,使用抛物线的拟合效果更好\n\n');
y2 = polyval(c2,x1);
figure(2);
plot(x0,y0,'o',x1,y2);
xlabel('let 1960 = 0/year');
ylabel('population');
title('用抛物线拟合最小二乘所得的结果');
参考函数:
函数一:polyfit(x0,y0,2);得到对应点的最小二乘后的系数,2次拟合。
函数二:polyval(c2,x1);得到插值系数为c2数组的插值多项式,得到的是一个x1代入后的对应值组成的一维数组。
% page 199 3
% this problem is to estimate the populatipn in 1980 and
% compare the error using different method to estimate
% one use line and the other is parabale
% input:none
% output:plot the figure and display error
function page_199_3_script
format long;
x0 = [0 10 30 40];
y0 = [3039585530 3707475887 5281653820 6079603571];
x1 = 0:.01:450;
c1 = polyfit(x0,y0,1);
error1 = norm(polyval(c1,x0)-y0,2);
fprintf('使用直线拟合所得的RMSE为 %f\n',error1/2);
y1_1980 = polyval(c1,20);
fprintf('使用直线拟合的1980的人口 %f\n',y1_1980);
fprintf('拟合与实际上人口的误差为 %f\n\n',y1_1980-4452584592);
y1 = polyval(c1,x1);
figure(1);
plot(x0,y0,'o',x1,y1);
xlabel('let 1960 = 0/year');
ylabel('population');
title('用直线拟合最小二乘所得的结果');
c2 = polyfit(x0,y0,2);
error2 = norm(polyval(c2,x0)-y0,2);
fprintf('使用抛物线拟合所得的RMSE为 %f\n',error2/2);
y2_1980 = polyval(c2,20);
fprintf('使用抛物线拟合的1980的人口 %f\n',y2_1980);
fprintf('拟合与实际上人口的误差为: %f\n',y2_1980-4452584592);
fprintf('我们从误差来看,使用抛物线的拟合效果更好\n\n');
y2 = polyval(c2,x1);
figure(2);
plot(x0,y0,'o',x1,y2);
xlabel('let 1960 = 0/year');
ylabel('population');
title('用抛物线拟合最小二乘所得的结果');
参考函数:
函数一:polyfit(x0,y0,2);得到对应点的最小二乘后的系数,2次拟合。
函数二:polyval(c2,x1);得到插值系数为c2数组的插值多项式,得到的是一个x1代入后的对应值组成的一维数组。
相关文章推荐
- 使用随机梯度算法对高斯核模型进行最小二乘学习法的MATLAB程序源码分析
- Day Four——分别使用Keras和Sklearn训练模型预测结果
- 机械臂(3)——使用MATLAB建立机械臂模型
- Matlab/Simulink——使用SimMechanics建立单摆模型
- 使用LSTM-RNN建立股票预测模型
- Matlab之使用linearization估计人口数量
- 人口预测模型Matlab实现Logistic曲线模型
- matlab premnmx归一化函数的使用 1、premnmx 预处理数据使数据的最小值和最大值分别为-1和1. [PN,minp,maxp,TN,mint,maxt] = premnmx(P,T)
- select模型使用例子
- 一个Excel导入SQL server的例子(分别使用游标、CTE、master..spt_values实现)
- WSAEventSelect模型例子(使用线程池)
- 使用预定义模型QDirModel+Treeview的例子
- SPSS Clementines 预测分析模型----啤酒+尿片故事的实现机理(使用11版本实现)
- 分别使用ADO.Net Entity Data Model 和Linq to Sql 建立数据访问层
- 有数据库apps权限,但是没有oracle EBS 帐户, 可以用以下方法建立一个用户使用:
- 建立精确地股票趋势预测模型
- 发一个使用 GridView 对数据小类进行分别汇总的例子
- 一个Excel导入SQL server的例子(分别使用游标、CTE、master..spt_values实现)
- SQL临时表建立和使用的一个例子
- 使用GUI建立两图标之间连线的例子