Matlab rref()函数希尔伯特矩阵第12列后的精度误差

Matlab rref()函数希尔伯特矩阵第12列后的精度误差,matlab,precision,Matlab,Precision,我的问题可能很简单,但我想不出一个合乎逻辑的解释: 当我使用 rref(hilb(8)), rref(hilb(9)), rref(hilb(10)), rref(hilb(11)) 它给出了我所期望的结果,一个单位矩阵 然而,当涉及到 rref(hilb(12)) 它没有给出预期的非奇异矩阵。我使用了Wolfram,它给出了相同情况下的单位矩阵,因此我确信它应该给出单位矩阵。可能有舍入错误或类似的错误,但1/11或1/7也有一些麻烦的小数 那么为什么Matlab在计算12时会这样呢 这似

我的问题可能很简单,但我想不出一个合乎逻辑的解释:

当我使用

rref(hilb(8)), rref(hilb(9)), rref(hilb(10)), rref(hilb(11)) 
它给出了我所期望的结果,一个单位矩阵

然而,当涉及到

rref(hilb(12))
它没有给出预期的非奇异矩阵。我使用了Wolfram,它给出了相同情况下的单位矩阵,因此我确信它应该给出单位矩阵。可能有舍入错误或类似的错误,但1/11或1/7也有一些麻烦的小数


那么为什么Matlab在计算12时会这样呢

这似乎确实是一个精度错误。这是有意义的,因为希尔伯特矩阵的行列式顺序
n
趋向于0,而
n
趋向于无穷大()。但是,您可以使用:

并将
tol
设置为非常小的值,以获得更精确的结果。例如,
rref(hilb(12),1e-20)
会给你单位矩阵

编辑-有关
tol
参数角色的更多详细信息

答案底部提供了
rref
的源代码。
tol
用于在列的某个部分搜索绝对值中的最大元素,以查找轴心行

% Find value and index of largest element in the remainder of column j.
[p,k] = max(abs(A(i:m,j))); k = k+i-1;
   if (p <= tol)
      % The column is negligible, zero it out.
      A(i:m,j) = zeros(m-i+1,1);
      j = j + 1;
% Find value and index of largest element in the remainder of column j.
[p,k] = max(abs(A(i:m,j))); k = k+i-1;
   if (p <= tol)
      % The column is negligible, zero it out.
      A(i:m,j) = zeros(m-i+1,1);
      j = j + 1;
function [A,jb] = rref(A,tol)
%RREF   Reduced row echelon form.
%   R = RREF(A) produces the reduced row echelon form of A.
%
%   [R,jb] = RREF(A) also returns a vector, jb, so that:
%       r = length(jb) is this algorithm's idea of the rank of A,
%       x(jb) are the bound variables in a linear system, Ax = b,
%       A(:,jb) is a basis for the range of A,
%       R(1:r,jb) is the r-by-r identity matrix.
%
%   [R,jb] = RREF(A,TOL) uses the given tolerance in the rank tests.
%
%   Roundoff errors may cause this algorithm to compute a different
%   value for the rank than RANK, ORTH and NULL.
%
%   Class support for input A:
%      float: double, single
%
%   See also RANK, ORTH, NULL, QR, SVD.

%   Copyright 1984-2005 The MathWorks, Inc. 
%   $Revision: 5.9.4.3 $  $Date: 2006/01/18 21:58:54 $

[m,n] = size(A);

% Does it appear that elements of A are ratios of small integers?
[num, den] = rat(A);
rats = isequal(A,num./den);

% Compute the default tolerance if none was provided.
if (nargin < 2), tol = max(m,n)*eps(class(A))*norm(A,'inf'); end

% Loop over the entire matrix.
i = 1;
j = 1;
jb = [];
while (i <= m) && (j <= n)
   % Find value and index of largest element in the remainder of column j.
   [p,k] = max(abs(A(i:m,j))); k = k+i-1;
   if (p <= tol)
      % The column is negligible, zero it out.
      A(i:m,j) = zeros(m-i+1,1);
      j = j + 1;
   else
      % Remember column index
      jb = [jb j];
      % Swap i-th and k-th rows.
      A([i k],j:n) = A([k i],j:n);
      % Divide the pivot row by the pivot element.
      A(i,j:n) = A(i,j:n)/A(i,j);
      % Subtract multiples of the pivot row from all the other rows.
      for k = [1:i-1 i+1:m]
         A(k,j:n) = A(k,j:n) - A(k,j)*A(i,j:n);
      end
      i = i + 1;
      j = j + 1;
   end
end

% Return "rational" numbers if appropriate.
if rats
    [num,den] = rat(A);
    A=num./den;
end