0

In my school homework I was given a problem, that required me to calculate Lagrange polynomial. The data set (x,y) involved x = 0,1,2....20, while y = x - 0.3*rand() + 1; Then we were asked to observed Runge's Phenomenon. After that, we were asked to compute Chebyshev Nodes around the edge to remove this phenomenon. Well, this is where I somehow got stuck. The Lagrange polynomial I'm calculating, at this moment, is not going through every data point.

The lagrange polynomial function I used is,

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

And the dataset I'm using for this, is: 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];

yn =[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];

The curve I'm getting can be seen here

The blue curve gives the original dataset, and the green curve gives the points evaluated from the polynomial. Can anyone give me suggestion on how do I remove this error. I think this is some kind of precision error. Thanks in advance.

4

1 回答 1

2

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

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

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

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

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

于 2013-05-08T21:23:38.003 回答