Solving Linear equation using LU Factorization and COLAMD
Show older comments
I am trying to solve the Poisson equation on a rectangular/square domain with homogeneous Neuman boundary conditions. When discretized using central finite differences, I end up with an equation Ax = b, where A is sparse and singular. A can be rendered invertible by the following modification A(1,:)=0; A(1,1)=fixed value (e.g., 1000), such that we specify the value of x at a location in the domain.Typically, the equation is efficiently solved by prefactoring A using LU factorization. i.e., x = U\(L\b).
By permuting the columns of A (using COLAMD), however, the fill-ins of the resulting L,U matrices can be decreased, which can speed up solving the equation. So, p = COLAMD(A), [L,U] = lu(A(:,p));x(p) = U\(L\b(p)). Here, p indicates the column permutations.
Here is the problem I have.Let x1 and x2 be the solutions computed without and with column permutations. I compute ||LU-A(:,p)|| <1E-10, to test the COLAMD is doing what it is supposed to do. However, the solutions x1 and x2 are very different. ||x1-x2||. I am pasting the code below. Please let me know.
nx = 10; ny = 10;
% Construct matrix A
d2Qdx2 = spdiags(ones(nx,1)*[1, -2, 1],[-1,0,1],ny,nx);
d2Qdx2(1,1) = -1; d2Qdx2(nx,nx) = -1;
d2Qdx2 = kron(d2Qdx2,speye(ny));
d2Qdy2 = spdiags(ones(ny,1)*[1, -2, 1],[-1,0,1],ny,ny);
d2Qdy2(1,1) = -1; d2Qdy2(ny,ny) = -1;
d2Qdy2 = kron(speye(nx),d2Qdy2);
A = d2Qdx2 + d2Qdy2;
A(1,:)=0; A(1,1) = 1;
% generate the right hand side vector
B = rand(ny,nx); b = B(:)-mean(B(:));
% Solution without COLAMD
[L,U] = lu(A); x1 = U\(L\b);
% solution with COLAMD
p = colamd(A); [L,U] = lu(A(:,p)); x2(p) = U\(L\b(p));
norm(L*U-A(:,p),'fro')
norm(x1-x2,'fro')
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!