【问题标题】:MATLAB; Lagrange Polynomial, an Interesting ErrorMATLAB;拉格朗日多项式,一个有趣的错误
【发布时间】: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


    【解决方案1】:

    恭喜!您是遇到此问题的第 10 百万(和第 3)个人。 :)

    这是精确的。通过 21 个点的拉格朗日多项式将是 20 次多项式。

    因此,您将数字提高到 20 的 20 次方。然后,您从其他可能为 1 的数字中添加和减去它们。(无论您评估该多项式,它都会导致这些相同的问题。)

    double 可以处理的数字范围是多少?大约 16 位十进制数字。期望看到数字垃圾。毫不奇怪,你做到了。

    你如何避免它?不要使用高阶多项式! (顺便说一下,给你这个例子的原因是为了准确地看到那个结果。数值方法中的每个第一类都有这样的例子。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      相关资源
      最近更新 更多