MATLAB: Solving a system of equation (Ax=b) with a block of zeros

dgesv

Dear All, I recently converted my FORTRAN code for contact-impact problems to MATLAB code. IN any contact problem, I have to solve a system of equation Ax=b for the contact node. I am using the Lagrange multiplier method (LMM) to enforce the contact constraints. Now, due to the way LMM method is constructed, a block of zeros is obtained in A which also leads to some of the diagonal entries of A becoming zero. I cannot use x=A\b since I have zeros on the diagonal. To overcome this, I use a perturbed Lagrangian approach where a small positive value (1e-11 or 1e-10) is added to the diagonal so that now the invention is possible. However, still, the condition number is very bad. When I was using FORTRAN version of the MATLAB code, I used DGESV without any problem. So, what should I do. I see there is https://in.mathworks.com/matlabcentral/fileexchange/8006-lapack-function-dgesv-in-matlab but I have no prior experience in mex file generation.
Any help in this regard would be highly appreciated.
PS: For matrix A the size is (ID+ROWS_Q1) x (ID+ROWS_Q1) where A(1:ID,1:ID) entires are all zeros.

Best Answer

This is simply not true that having zeros on the diagonal prevents you from using A\b.
A= rand(3);
A = A - diag(diag(A))
A =
0 0.19566 0.10139
0.1568 0 0.34752
0.24236 0.54753 0
As you can see, the entire diagonal is zero, identically so. Yet A is full rank.
rank(A)
ans =
3
The condition number is even quite low.
cond(A)
ans =
5.9645
b = rand(3,1);
x = A\b
x =
2.9739
-0.010528
1.1608
So A\b is quite well posed.
Likewise, having a block of zeros is not a problem, unless the block is seriously large.
A = rand(10,10);
A(1:3,1:3) = 0;
rank(A)
ans =
10
So if we go overboard, then of course things will fail.
A(1:6,1:6) = 0;
rank(A)
ans =
8
My guess is you need to use a tool like pinv. It is appropriate, since you are essentially trying to regularize the problam anyway. In that case, just use
x = pinv(A)*b;
There is no need to hack with A by adding tiny numbers to the diagonal.
Note that DGESV (see this link for doc) does nothing special. It just computes a partially pivoted LU decomposition to solve a linear system. So if you were getting results from a rank deficient matrix, they were probably garbage anyway.