
clc,clear

%% 数据
x = -5:1:5;
y = 1./(1+x.^2);
xx = -5:0.1:5;
yy = 1./(1+xx.^2);

%% 分段线性插值
Y_linear = interp1(x,y,xx,'linear');

%% 分段Hermite插值
% 分段三次插值方法：pchip, cubic, makima
Y_Hermite = interp1(x,y,xx,'makima');


%% 画图对比
hold on;
plot(xx,yy,'r','Linewidth',2);
plot(xx,Y_linear,'b-','Linewidth',1);
plot(xx,Y_Hermite,'k-','Linewidth',1);
plot(x,y,'ro','Linewidth',2)

legend('f(x)','linear','Hermite','data')

axis([-5 5 -0.5 1.5])