我正在尝试求解线性系统 (A*x=B),其中 B 是 64 位长度或更长。我在Matlab中使用linsolve函数来求解方程组。我也使用过 (inv(A)*B)、A\B 和 ttimes(A,B),它们也遇到同样的问题。
我面临两个问题:
- 如果( A 和 B)没有符号,linslove 函数无法找到精确解。
- 如果 A 和 B 是符号性的,linsolve 可以找到精确解,但需要花费太多时间。
有什么方法可以快速找到精确的解决方案。
time=[]
i=50
a=magic(i);
% B is a rendom numbers where each number is 64 bit length
B=double(bi2de(randi([0 1],i,64)));
%%****************************************
% to make sure th matrix is not **ill-conditioned***
C = 1; % desired condition number
[u s v] = svd(a);
s = diag(s); % s is vector
% ===== linear stretch of existing s
s = s(1)*( 1-((C-1)/C)*(s(1)-s)/(s(1)-s(end)));
% =====
s = diag(s); % back to matrix
A = u*s*v';
%%****************************************
tic
x1=linsolve(A,B);
time(1,1)=toc;
%-------------------------------------
% convert A and B into symbolic
Aa=sym(A); Bb=sym(B);
tic
x2=linsolve(Aa,Bb);
time(1,2)=toc;
%-------------------------------------
% Show the accuracy of the first B(1), exact vs computed
Exact=sym(double(B(1)))
Computed=[ A(1,:)*x1 Aa(1,:)*x2]
time
x1 和 x2 是两个解。 x2 是求和式 A 和 B 的解。
只有 X2 才能给我们精确的解
Exact solution = 2350911785583947776
Computed using x1= 2350911785583965184
Computed using x2= 2350911785583947776
所需时间(以秒为单位):
x1 time = 0.0007
x2 time = 6.7242
最佳答案
这不是您问题的答案,而是演示了为什么您的“精确”解决方案不精确:输入 B
是近似的。在 MATLAB 中尝试一下:
a = randi([0 1],1,64);
a(1) = 0;
a1 = bi2de(a);
a(1) = 1;
a2 = bi2de(a);
a1-a2
您会注意到 a1
和 a2
是相同的,即使我翻转了两个数字中的最低有效位。这是因为 double float 无法容纳 64 位精度。它仅保存 52。64 位表示中的其他 12 位用于存储符号位和指数(用于缩放数字)。
关于matlab - 找到线性系统精确解的最快方法是什么,matlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60610933/