[英]Sparse diagonal matrix solver
I want to solve, in MatLab, a linear system (corresponding to a PDE system of two equations written in finite difference scheme). 我想在MatLab中求解一个线性系统(对应于以有限差分方案编写的两个方程的PDE系统)。 The action of the system matrix (corresponding to one of the diffusive terms of the PDE system) reads, symbolically (
u
is one of the unknown fields, n
is the time step, j
is the grid point): 系统矩阵的动作(对应于PDE系统的扩散项之一)以符号方式读取(
u
是未知字段之一, n
是时间步长, j
是网格点):
and fully: 并充分:
The above matrix has to be intended as A
, where A*U^n+1 = B is the system. 上面的矩阵必须是
A
,其中A * U ^ n + 1 = B是系统。 U
contains the 'u' and the 'v' (second unknown field of the PDE system) alternatively: U = [u_1,v_1,u_2,v_2,...,u_J,v_J]. U
交替包含“ u”和“ v”(PDE系统的第二个未知字段):U = [u_1,v_1,u_2,v_2,...,u_J,v_J]。 So far I have been filling this matrix using spdiags
and diag
in the following expensive way: 到目前为止,我一直以下列昂贵的方式使用
spdiags
和diag
填充此矩阵:
E=zeros(2*J,1);
E(1:2:2*J) = 1;
E(2:2:2*J) = 0;
Dvec=zeros(2*J,1);
for i=3:2:2*J-3
Dvec(i)=D_11((i+1)/2);
end
for i=4:2:2*J-2
Dvec(i)=D_21(i/2);
end
A = diag(Dvec)*spdiags([-E,-E,2*E,2*E,-E,-E],[-3,-2,-1,0,1,2],2*J,2*J)/(dx^2);`
and for the solution 和解决方案
[L,U]=lu(A);
y = L\B;
U(:) =U\y;
where B
is the right hand side vector. 其中
B
是右侧向量。
This is obviously unreasonably expensive because it needs to build a JxJ matrix, do a JxJ matrix multiplication, etc. 这显然是不合理的昂贵,因为它需要构建JxJ矩阵,进行JxJ矩阵乘法等。
Then comes my question: is there a way to solve the system without passing MatLab a matrix, eg, by passing the vector Dvec
or alternatively directly D_11
and D_22
? 接下来是我的问题:是否有一种方法可以解决该系统而无需将MatLab传递给矩阵,例如通过传递矢量
Dvec
或直接D_11
和D_22
? This would spare me a lot of memory and processing time! 这将节省我很多内存和处理时间!
Matlab doesn't store sparse matrices as JxJ arrays but as lists of size O(J). Matlab不会将稀疏矩阵存储为JxJ数组,而是存储为大小为O(J)的列表。 See http://au.mathworks.com/help/matlab/math/constructing-sparse-matrices.html Since you are using the spdiags function to construct A, Matlab should already recognize A as sparse and you should indeed see such a list if you display A in console view.
参见http://au.mathworks.com/help/matlab/math/constructing-sparse-matrices.html由于您使用spdiags函数构造A,因此Matlab应该已经将A识别为稀疏,因此您确实应该看到这样的列表如果在控制台视图中显示A。
For a tridiagonal matrix like yours, the L and U matrices should already be sparse. 对于像您这样的三对角矩阵,L和U矩阵应该已经稀疏。
So you just need to ensure that the \\ operator uses the appropriate sparse algorithm according to the rules in http://au.mathworks.com/help/matlab/ref/mldivide.html . 因此,您只需要确保\\运算符即可根据http://au.mathworks.com/help/matlab/ref/mldivide.html中的规则使用适当的稀疏算法。 It's not clear whether the vector B will already be considered sparse, but you could recast it as a diagonal matrix which should certainly be considered sparse.
尚不清楚向量B是否已被视为稀疏,但您可以将其重铸为对角矩阵,当然应该将其视为稀疏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.