6

I have two matrices in the form of the variables A and b that are inputs to my matlab function (posted below). I would like to calculate the significant figures used for the matrix inverse operation (matrix division) from the result A, b matrices. However, I have no idea where to start (matlab or mathematically) to go about this approach. Help?

More context, using a square linear system (Ax=b) and I am seeing if its singular or nonsingular and trying to find a solution(s).

% x = answer
% y = 0 if no solution, 1 if nonsingular, 2 if many solutions
% z = p is number of sig figs
%
function [ x, y, z ] = squareLinSysSolv(A, b)


if det(A) == 0
    % Matrix is singular and therefor many solutions
    x = A\b;
    y = 0; % Used as place holder to compile
    z = 5; % Used as place holder to compile
elseif det(A) ~= 0
    % Matrix does not equal to zero (perhaps a number very close to it or
    % far from it) and therefor has a unique solution.
    x = A\b;
    y = 1; % Used as place holder to compile
    z = 5; % Used as place holder to compile
end
end

Edit: To make it a bit clear, z should be some integer which approximates (ceiling or floor value) a decimal number of significant figures at which A\b were calculated at.

Test Cases: Test/Spec sheet of what is expected. Both A and b are matrices and the result should be something like so.

A =
    1.5000    2.3000    7.9000
    6.1000    3.2000   13.0000
   13.0000   21.0000   76.0000

b =
     1
     3
     5
>> [x,y,z] = squareLinSysSolv(A,b)
% the result of x = A\b
x =

    0.8580
    3.0118
   -0.9132
% determinant is not equal to zero
y =

     1
% Amount of sig figs/precision in calculation
z =

     15
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Adam
  • 510
  • 1
  • 5
  • 21
  • Maybe I'm being dense, but I don't understand what you mean by 'the significant figures used for the matrix inverse operation". Do you mean something like "how much I can vary A and b without changing the answer by more than some (given) amount"? – Dan Becker Oct 25 '12 at 18:21
  • @DanBecker Hopefully this makes it easier to understand (I also updated my original post) **z should be some integer which approximates (ceiling or floor value) a decimal number of significant figures at which `A\b` were calculated at.** – Adam Oct 25 '12 at 18:27
  • Is the idea that if you ignore every digit (in all elements) of A and b after the first z digits, then you will still get *exactly* the same answer for x? – Dan Becker Oct 25 '12 at 18:43
  • @DanBecker not exactly. Just want to determine to what extend how many figures of precision are in my answer (even if I chose only to display 5 figures - or what have you). Updated original post to include a spec sheet with test cases. – Adam Oct 25 '12 at 18:50
  • Hmm, this still doesn't make sense to me ... is the idea that you want a value z, such that all the decimal digits after the first z are equal to zero? – Dan Becker Oct 25 '12 at 19:24
  • If my last comment is an accurate description of what you are trying to do, then this SO question looks like it will be helpful:http://stackoverflow.com/questions/11695233/how-to-calculate-the-number-of-significant-decimal-digits-of-a-c-double – Dan Becker Oct 25 '12 at 19:39
  • 1
    Given that at least two people don't even understand what this question is asking, why are people upvoting? Could an upvoter give us their interpretation of what this question is asking, and why they think it is interesting? I keep feeling like I must be missing something! – Dan Becker Oct 25 '12 at 21:22

3 Answers3

1

I'm with Dan here. I don't understand the question, nor do I see how/where you're calculating z. First of all, the number digits in the display of your answer is independent of the number of significant digits in the calculation of the answer. Second, you have two cases: det(A)==0, and det(A)~=0. In both cases, it appears that you are setting z = 5. (You must be doing something elsewhere that you're not showing to calculate z = 15? How did you calculate your z?)

Also, recognize that the number of significant digits will be a function of the class of your data. Double>single>integer...

And...just for efficiency, I don't know the size of A (nor how much overhead is entailed in computing its determinant), but there's no reason to compute it twice:

if det(A)==0
    % case 1
else % NOT: elseif det(A)~=0
    % case 2
end

I guess I'm being dense, too. :)

Brett

Brett Shoelson
  • 316
  • 1
  • 3
1

How close a matrix is to being singular is typically quantified by a "condition number". There are several ways to estimate the condition number of a matrix. One common technique is to calculate the ratio of the largest and smallest magnitude eigenvalues.

Matlab has a built-in function called cond that gives the condition number for a matrix (with respect to inversion). Values near 1 are "good"--I'm not sure how to relate it to anything concrete like "significant figures".

nibot
  • 14,428
  • 8
  • 54
  • 58
0

If the question that you are asking is "Given matlab's decimal representation of a floating point number, what is the smallest number z such that after the first z digits all digits are zero", then I think the following function will more-or-less work.

I will add the caveat that although I've tested this on a bunch of numbers, it's quite possible that I've missed a case, so you will want to test it carefully. This could be done more efficiently in a single pass through the string.

function n = sig_decimal_digits(f)
    % get string representation, any decimal point, and any leading zeros
    str = num2str(f,20);

    % strip any exponent part
    loc = regexp(str, 'e');
    if ~isempty(loc)
        str = str(1:(loc-1));
    end

    % strip any decimal point
    str = strrep(str, '.', '');

    % strip any leading zeros (and minus sign!)
    loc = regexp(str, '[1-9]');
    if isempty(loc)
        %if *only* leading zeros, f is 0, so n = 1
        n = 1;
        return;
    else
        str = str(loc:end);
    end

    % count length of string, excluding trailing zeros
    loc = regexp(str, '0+$');
    if isempty(loc)
        n = length(str);
    else
        n = loc-1;
    end
end

However, I will add two comments:

  1. This clearly has nothing to do with matrix multiplication, so I'm not sure why you brought that into it.
  2. This is a strange quantity to want to calculate. Unless your matrices have very special numbers in them, the answer is nearly always going to be 17, since most floating point numbers don't have short decimal expansions. For example, for the A and b that you give in your question, all three numbers in x have 17 significant digits according to my function.
Dan Becker
  • 1,196
  • 10
  • 18