0

在下面的代码中,我遵循了一个过程来创建一个随机正定矩阵P

首先,我创建了一个随机数组A的奇异值分解[U,S,V]并且我试图验证实际上U'*U==U*U'=I(其中I是单位矩阵)这是从理论上知道的。问题是,如果我自己检查矩阵的内容,我可以验证这一点,但 Matlab 会生成一个逻辑矩阵,它不能验证因为零表示为 -0.000 或 0.0000,所以只有当符号匹配时结果才为 1。这是为什么?

但是更大的问题出现在下面的几行中,其中产生了正定(其所有特征值都是正的)矩阵P,我只是想检查P=P'。通过单击 x 和 y,我可以看到它们的内容完全相同,但 Matlab 也无法验证这一点。我不明白为什么会发生这种情况,因为在这种情况下,我们在这里谈论的是相同的变量P,它只是简单地转置。

这是代码:

%Dimension of the problem
n = 100;

%Random matrix A
A = rand(n, n)*10 - 5;

%Singular value decomposition
[U, S, V] = svd(A);

%Verification that U*U'=U'*U=I
U'*U == U*U'

%Minimum eigenvalue of S
l_min = min(diag(S));

%Maximum eigenvalue of S
l_max = max(diag(S));

%The rest of the eigenvalues are distributed randomly between the minimum
%and the maximum value
z = l_min + (l_max - l_min)*rand(n - 2, 1);

%The vector of the eigenvalues
eig_p = [l_min; l_max; z];

%The Lambda diagonal matrix
Lambda = diag(eig_p);

%The P matrix 
P = U*Lambda*U';

%Verification that P is positive definite
all(eig(P) > 0)

%Verification that P=P'
x=P;
y=P';
x==y
4

1 回答 1

2

您的问题不是0表示为-0.000or 0.0000(您可以通过运行来验证这一点(-0) == 0),而是这些值实际上非常小(相对于矩阵中的其他数字)。从浮点数的表示和操作方式来看,像您正在做的操作总是会出现小错误。

而不是验证那个P=P',尝试验证那个abs(P-P') < 0.000000001,或者任何适合你给定应用程序的阈值。

于 2015-11-01T10:53:37.983 回答