How to easily solve an equation of the form AX =B where A is a large sparse matrix???
8 views (last 30 days)
Show older comments
I am using MATLAB R2014b
I am trying to solve a Partial Differential Equation (PDE) by Finite Difference (FD) technique. In this method the PDE can be represented in matrix form as AX = B where A is an operator matrix, X is the solution matrix and B is the RHS matrix. X = inverse (A)*B gives the solution of PDE. A is usually a large sparse matrix whose entries may take different values depending upon the order of PDE we want to solve.
I am facing two problems
P1 : In some cases, when I am trying to solve the equation (AX = B) MATLAB is showing a warning that “Matrix is singular to working prescision” meaning that MATLAB is not able to calculate the inverse of A because the determinant of A is zero. In order to calculate the inverse of A, the determinant of A must be non-zero. But the matrix that I obtained is having determinant zero. Some people have suggested on modifying the A matrix such that its determinant becomes non-zero. The problem is, A matrix is mathematically obtained and there is no provision for modifying the A matrix in any manner. So I want to know whether there is any way of solving equation involving a singular (determinant (A) = 0) sparse A matrix. I have heard about sparse matrix functions in MATLAB but I could not understand much about it.
P2 : In some cases, the obtained A matrix is such that its determinant is non-zero. In this case it is possible to directly calculate the solution of PDE. So the above problem does not exist here. But the second problem is the shortage of memory. I might want to solve equations involving large A matrices of size typically of the order of 1000000000 X 1000000000. The problem here is that when I run the code MATLAB is showing an error that “Insufficient storage space” which is obvious due to the large size of A matrix and the lower capacity of the computer (RAM – 8GB & HDD - 1TB) that I am using. Is there a way of solving matrix equations involving large sparse matrices consuming less storage space?
(I have used A\B, mldivide() and pinv(). The warning and error remains the same and is displayed in all these cases.)
I would be very thankful if anyone could help me out… please.. THANKS in advance….
2 Comments
Walter Roberson
on 24 May 2017
For the non-singular case you should be using X = A\B -- do not take the inverse if you can avoid it.
For the singular case, which of the answers do you want? pinv() ?
Stephen23
on 24 May 2017
P1: using
X = inverse (A)*B
is very bad way to solve these equations. Much better is to use either of:
As the documentation for inv clearly states, "It is seldom necessary to form the explicit inverse of a matrix. A frequent misuse of inv arises when solving the system of linear equations Ax = b. One way to solve the equation is with x = inv(A)*b. A better way, from the standpoint of both execution time and numerical accuracy, is to use the matrix backslash operator x = A\b. This produces the solution using Gaussian elimination, without explicitly forming the inverse. See mldivide for further information"
P2: " I might want to solve equations involving large A matrices of size typically of the order of 1000000000 X 1000000000"
You need to rethink your algorithm then.
Answers (2)
Steven Lord
on 24 May 2017
If your system has some structure you can exploit you may be able to solve this system without explicitly creating the coefficient matrices using the iterative methods included in MATLAB. See for example the "Using pcg with Large Matrices" example on the documentation page for pcg. It avoids having to create an n2-by-n2 matrix by writing a function that computes A*x without actually constructing A.
0 Comments
Christine Tobler
on 24 May 2017
P1: With the Finite Difference Method, the matrix will often be singular if there aren't sufficient boundary conditions added to the problem (since the boundary conditions are what makes a PDE have a unique solution). It may be a good idea to double-check that you have correctly added your boundary conditions.
If this doesn't help, you could take a very small (<4000) example of a singular matrix A, compute null(full(A)), and plot the column vectors of the result in your model: These give you all possible solutions for A*x = b, which may help in determining why the solution is not unique.
P2: A matrix of size 1e9-by-1e9 is quite large, and may just require a larger machine. Is it possible to solve your problem with a coarser discretization? If yes, there are methods for computing a finer discretization iteratively, and only directly solving the coarse discretization (see wikipedia: Multigrid method, for example).
0 Comments
See Also
Categories
Find more on Eigenvalue Problems in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!