我的问题可能很简单,但我想不出对我的问题的合理解释:
当我使用
时rref(hilb(8)), rref(hilb(9)), rref(hilb(10)), rref(hilb(11))
它给了我预期的结果,一个单位矩阵。
然而,当谈到
rref(hilb(12))
它没有按预期给出非奇异矩阵。我使用了 Wolfram ,它为同一个案例提供了单位矩阵,所以我确信它应该给出一个单位矩阵。可能有一个舍入错误或类似的东西,但然后1/11或1/7也有一些麻烦的小数
那么为什么Matlab在12个时表现得像这样呢?
答案 0 :(得分:1)
这确实看起来像是精确错误。这是有道理的,因为n
的Hilbert矩阵的行列式倾向于0,因为n
倾向于无穷大(http://reddit.com/r/front.json)。但是,您可以使用see here:
[R,jb] = rref(A,tol)
并将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;
如果所有元素的绝对值都小于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