问题
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.
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?
回答1:
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.)
来源:https://stackoverflow.com/questions/37648152/symmetric-matrix-is-not-symmetric