MATLAB 埃尔米特插值、样条插值、插值算法人口预测
程序员文章站
2022-05-22 12:46:54
...
分段三次埃尔米特插值
% 分段三次埃尔米特插值
x = -pi:pi; %导入数据
y = sin(x);
new_x = -pi:0.1:pi;
p = pchip(x,y,new_x);
figure(1);%同一脚本文件,想要画多个图,给每个图编号
plot(x,y,'o',new_x,p,'r-')
% plot函数用法:
% plot(x1,y1, x2, y2)
%线方式: -实线 :点线 -.虚点线 - -波折线
%点方式: .圆点 +加号 *星号 x X形 o小圆
%颜色: y黄; r红; g绿; b蓝; w白; k黑; m紫; c青
三次样条插值
%三次样条插值和分段三次埃尔米特插值的对比
x = -pi:pi; %导入数据
y = sin(x);
new_x = -pi:0.1:pi;
p1 = pchip(x,y,new_x); %分段三次埃尔米特插值
p2 = spline(x,y,new_x); % 三次样条插值
figure(2);
plot(x,y,'o',new_x,p1,'r-',new_x,p2,'b-')
legend('样本点','三次埃尔米特插值','三次样条插值','Location','SouthEast');
插值算法人口预测
人口数据:
预测2019到2021年人口:
%插值算法人口预测
population=[133126, 133770, 134413, 135069, 135738, 136427, 137122,137866,138639,139538];
year = 2009:2018;
p1 = pchip (year, population, 2019:2021); %分段三次埃尔米特插值预测
p2 = spline (year, population, 2019:2021); %三次祥条插值预测
figure(3);
plot (year, population,'o' , 2019: 2021,p1,' r*-' ,2019:2021 ,p2,'bx-');
legend('样本点','三次埃尔米特插值','三次样条插值','Location','SouthEast');