在我的学校作业中,我被给了一个问题,那就要求我计算拉格朗日多项式。数据集(x,y)涉及x = 0,1,2 .... 20, 而y = x-0.3 * rand()+1; 然后我们被要求观察Runge's Phenomenon。 之后,我们被要求计算边缘周围的Chebyshev Nodes以消除这种现象。那么,这是我不知何故被卡住的地方。我正在计算的拉格朗日多项式在这个时刻并不经过每个数据点。MATLAB;拉格朗日多项式,一个有趣的错误
我使用的拉格朗日多项式函数是,
function P = lagrangepoly(x, y)
%checks if two inputs vectors are obtained
if nargin ~= 2
error('The function accepts only two arguments of equal length');
end
sz = size(x); %size of x
sz2 = size(y); % size of y
%checks if size of x and size of y matches and they are row vectors
if (sz(1) ~= sz2(1)) || (sz(2) ~= sz2(2)) || (sz(1) ~= 1)
error('Mismatch in length or unsupported arguments.');
end
%takes the length of thevectors
len = sz(2);
%creating a emplt 1xlen container
P = zeros(1, len);
for i=1:1:len
%poly evaluates all the convolution of [1 -x(j)] except at x(i)
%prod evaluates all the product of (x(i) - x(j)) except at x(i)
P = P + (poly(x((1:len)~=i)) ./ prod(x(i)-x((1:len)~=i))) .* y(i);
end
end
而且我使用此数据集,方法是: XN = [0 0.5000 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 19.5000 20.0000];
炔= [0.7363 1.4701 1.7802 2.8232 3.9698 4.9934 5.9747 6.8635 7.9435 8.9775 9.9013 10.9318 11.8988 12.8343 13.7095 14.9318 15.8800 16.7028 17.8614 18.7046 19.8505 20.2849 20.7036];
我得到的曲线可以看出
蓝色曲线给人的原始数据集,绿色曲线给出了多项式评估的要点。 任何人都可以给我建议我如何消除这个错误。我认为这是某种精确度错误。 在此先感谢。
感谢您的帮助。 :) – ponir 2013-05-09 04:54:56