问题描述
我最近编写了用于有限元方法的代码.由于我的算法生成的是对称矩阵,因此在为每个元素收集数据之后,所得矩阵应该是对称的.
I have recently written code for finite element method. Since my algorithm is generating a symmetric matrix, after gathering data for each element the resulting matrix should be symmetric.
但是,在元素上循环运行之后,所得的全局矩阵不对称.基本的代码结构是这样的:
However, after I run loop over element, the resulting global matrix is not symmetric. The basic code structure is like this:
A=zeros(dof,dof)
for (each element)
loc_A = v'*(diagonal matrix)*v
% (v is 1xN row vector)
% loc_A is symmetric matrix
A(K,K) = A(K,K)+loc_A
% (K is Nx1 column vector)
end
由于我添加了对称矩阵,因此生成的矩阵也应该是对称的.
Since I add symmetric matrices, the resulting matrix should also be symmetric.
但是,所得矩阵不是对称的(我用issymmetric(A)
对其进行了检查).我认为这是由于舍入错误.如果我加上A =(A + A')/2,则得到的矩阵是对称的(当然...).但是我不想做这样的事情.还有其他补救方法可以使A
自然地对称而不进行任何后处理吗?
However, the resulting matrix is not symmetric (I checked it with issymmetric(A)
). I think this is because of round-off error. If I add A=(A+A')/2, resulting matrix is symmetric (of course...). However I don't want to do such thing. Is there any other remedy which makes A
symmetric naturally without any post-processing?
推荐答案
这是由于舍入错误(除非您的代码中存在明显的错误). A = (A+A')/2
通常可以解决此问题(这不是一项昂贵的操作),或者您可以告诉求解器您的全局矩阵是对称的(某些求解器(例如MUMPS)具有这种选择).
This is due to round-off error (unless you have an obvious bug in your code). This issue is normally solved by A = (A+A')/2
(this is not an expensive operation) or you can tell the solver that your global matrix is symmetric (some solvers (i.e. MUMPS) have such an option.)
这篇关于对称矩阵不对称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!