三次样条插值与多项式插值
Cubic spline interpolation vs polynomial interpolation
我被要求针对以下几点使用 Matlab 研究不同类型的插值:
x = [32 34 35 36 37 38]
y = [26 28 31 30 29 25]
并找到 f(33)
、f(33.5)
和 f(35)
的值。
绘制 x 和 y 时,我可以看到 f(33)
应该在 27 左右,这也是我使用 interp1(x,y,33)
.
得到的值
我不确定这是否是使用三次样条插值函数的正确方法,但我使用了 spline(x,y,33)
并得到了 ans = 24.3906
。
无论我使用哪种类型的插值,我都不应该为 f(33)
获得相同的值吗?
插值确保插值函数的值与您提供的点处的原始函数值相同。查看您的代码,这意味着 f(35)
将相同并且对于每种插值方法都等于 31 。
但是,根据插值方法,每个连续方法之间的曲线会有所不同,因此会给出不同的值,这是预期的。
想将此添加到我投票支持的@hazeiio 的 。你可以看到这很好地说明了这一点。
插值方法极大地影响了数据点之间获得的值(见下图)。您会发现在不检查可能出错的情况下盲目调用插值方法是很危险的。
% MATLAB R2017a
x = [32 34 35 36 37 38];
y = [26 28 31 30 29 25];
xTgts = [33 33.5 35 37.25 37.5 37.75];
% Interpolation between data points depends on method
Linear = interp1(x,y,xTgts)
Spline = interp1(x,y,xTgts,'spline') % Equivalent to spline(x,y,xTgts) yet faster somehow
Cubic = interp1(x,y,xTgts,'pchip')
正如所指出的,它们将完全匹配数据(见下图)。
% Interpolation of data points will match
Linear = interp1(x,y,x)
Spline = interp1(x,y,x,'spline')
Cubic = interp1(x,y,x,'pchip')
说明代码
step = 0.01;
xTest = (32:step:38)';
figure, hold on, box on
p(1) = plot(x,y,'ks','DisplayName','Data')
p(2) = plot(xTest,interp1(x,y,xTest),'b-','DisplayName','Linear')
p(3) = plot(xTest,interp1(x,y,xTest,'spline'),'r-','DisplayName','Spline')
p(4) = plot(xTest,interp1(x,y,xTest,'pchip'),'g-','DisplayName','Cubic')
legend('show')
% Options
xlabel('X')
ylabel('Y')
title('Interpolation Example')
for k = 1:4, p(k).LineWidth = 2; end
axis equal
xlim([31 39])
ylim([24 32])
参考:
Interpolation (wiki)
Interpolation Methods
Dangers of Interpolation
Higher Order Interpolation is a Bad Idea
我被要求针对以下几点使用 Matlab 研究不同类型的插值:
x = [32 34 35 36 37 38]
y = [26 28 31 30 29 25]
并找到 f(33)
、f(33.5)
和 f(35)
的值。
绘制 x 和 y 时,我可以看到 f(33)
应该在 27 左右,这也是我使用 interp1(x,y,33)
.
我不确定这是否是使用三次样条插值函数的正确方法,但我使用了 spline(x,y,33)
并得到了 ans = 24.3906
。
无论我使用哪种类型的插值,我都不应该为 f(33)
获得相同的值吗?
插值确保插值函数的值与您提供的点处的原始函数值相同。查看您的代码,这意味着 f(35)
将相同并且对于每种插值方法都等于 31 。
但是,根据插值方法,每个连续方法之间的曲线会有所不同,因此会给出不同的值,这是预期的。
想将此添加到我投票支持的@hazeiio 的
插值方法极大地影响了数据点之间获得的值(见下图)。您会发现在不检查可能出错的情况下盲目调用插值方法是很危险的。
% MATLAB R2017a
x = [32 34 35 36 37 38];
y = [26 28 31 30 29 25];
xTgts = [33 33.5 35 37.25 37.5 37.75];
% Interpolation between data points depends on method
Linear = interp1(x,y,xTgts)
Spline = interp1(x,y,xTgts,'spline') % Equivalent to spline(x,y,xTgts) yet faster somehow
Cubic = interp1(x,y,xTgts,'pchip')
正如所指出的,它们将完全匹配数据(见下图)。
% Interpolation of data points will match
Linear = interp1(x,y,x)
Spline = interp1(x,y,x,'spline')
Cubic = interp1(x,y,x,'pchip')
说明代码
step = 0.01;
xTest = (32:step:38)';
figure, hold on, box on
p(1) = plot(x,y,'ks','DisplayName','Data')
p(2) = plot(xTest,interp1(x,y,xTest),'b-','DisplayName','Linear')
p(3) = plot(xTest,interp1(x,y,xTest,'spline'),'r-','DisplayName','Spline')
p(4) = plot(xTest,interp1(x,y,xTest,'pchip'),'g-','DisplayName','Cubic')
legend('show')
% Options
xlabel('X')
ylabel('Y')
title('Interpolation Example')
for k = 1:4, p(k).LineWidth = 2; end
axis equal
xlim([31 39])
ylim([24 32])
参考:
Interpolation (wiki)
Interpolation Methods
Dangers of Interpolation
Higher Order Interpolation is a Bad Idea