【发布时间】:2013-05-08 20:53:22
【问题描述】:
在我的学校作业中,我遇到了一个问题,要求我计算拉格朗日多项式。数据集 (x,y) 涉及 x = 0,1,2....20, 而 y = x - 0.3*rand() + 1; 然后我们被要求观察Runge's Phenomenon。 之后,我们被要求在边缘周围计算Chebyshev Nodes 以消除这种现象。好吧,这就是我卡住的地方。目前,我正在计算的拉格朗日多项式并未遍历每个数据点。
我使用的拉格朗日多项式函数是,
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 8.0000 8.0000 8.0000 11.000011.000011.0000 13.0000 13.0000 13.000011.0000 16.0000 17.0000 18.0000 19.0000 19.5000 20.0000];
yn = [0.7363 1.4701 1.7802 2.8232 3.9698 4.9698 4.9747 6.9747 6.9435 7.9435 8.9435 8.9435 8.9013 10.9318 11.8988 12.8343 13.709511111113.709511111113.8800 16.7028 17.8614111119.8505 20.2849 20.7036];
我得到的曲线可以看到
蓝色曲线给出原始数据集,绿色曲线给出从多项式评估的点。 谁能给我关于如何消除此错误的建议。我认为这是某种精度错误。 提前致谢。
【问题讨论】:
标签: matlab math numerical-methods numerical polynomial-math