【问题标题】:Matlab rref() function precision error after 12th column of hilbert matrices希尔伯特矩阵第 12 列后的 Matlab rref() 函数精度误差
【发布时间】:2017-03-19 22:34:31
【问题描述】:

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

当我使用时

rref(hilb(8)), rref(hilb(9)), rref(hilb(10)), rref(hilb(11)) 

它给了我预期的结果,一个单位矩阵。

然而当涉及到

rref(hilb(12))

它没有像预期的那样给出一个非奇异矩阵。我使用了 Wolfram,它给出了相同情况的单位矩阵,所以我确信它应该给出一个单位矩阵。可能存在舍入错误或类似的错误,但 1/11 或 1/7 也有一些麻烦的小数

那么为什么 Matlab 在 12 时会表现得这样呢?

【问题讨论】:

    标签: matlab precision


    【解决方案1】:

    这确实看起来像一个精度错误。这是有道理的,因为n 阶希尔伯特矩阵的行列式趋于 0,而 n 趋于无穷大 (see here)。不过,你可以使用rref with tol parameter

    [R,jb] = rref(A,tol)
    

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

    EDIT - 有关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;
    

    如果所有元素的绝对值都小于tol,则列的相关部分用零填充。这似乎是 rref(hilb(12)) 发生精度错误的地方。通过减少tol,我们避免了rref(hilb(12),1e-20) 中的这个问题。

    源代码:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-18
      • 2016-02-09
      • 2016-04-08
      • 2012-08-10
      • 1970-01-01
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多