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?