Matlab中的Lagrange插值摄动

时间:2019-05-05 15:12:46

标签: matlab interpolation stability

这是我的代码,用于查找拉格朗日多项式插值的中心系数:

% INPUT
% f f scalar - valued function
% interval interpolation interval [a, b]
% n interpolation order
%
% OUTPUT
% coeff centered coefficients of Lagrange interpolant
function coeff = lagrangeInterp (f, interval , n)

a = interval(1);
b = interval(2);

x = linspace(a,b,n+1);
y = f(x);

coeff(1,:) = polyfit(x,y,n);
end

在以下脚本中被称为

%Plot lagrangeInterp and sin(x) together
hold on
x = 0:0.1*pi:2*pi;
for n = 1:1:4
    coeff = lagrangeInterp(@(x)sin(x),[0,2*pi],n);
    plot(x,polyval(coeff,x,'-'));
end
y = sin(x);
plot(x,y);
legend('1st order','2nd order','3rd order','4th order','sin(x)');

要检查稳定性,我想干扰函数(例如g(x)= f(x)+ epsilon)。我该怎么办?

1 个答案:

答案 0 :(得分:0)

嗯,给你个小把戏。

您知道Matlab中的randn([m,n])会生成m * n个随机矩阵。关键是要生成一个随机向量,并将interp1转换为x的函数。像这样:

x = linspace(a,b,n+1); % Your range of input
g = @(ep,xx)f(xx)+interp1(x,ep*randn([length(x),1]),xx);