Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Any idea why I'm ALWAYS getting a nonsingular matrix warning?

  1. Mar 1, 2013 #1
    Every time I run this, I'm getting the error that my random matrix is poorly conditioned. It's weird because I don't get the same error if I just create one in the command window.

    My script:

    clc
    clear all

    % part 1

    x = rand(20, 1);
    save('x.mat', 'x');
    a = rand(20, 20);
    save('a.mat', 'a');
    y = a*x;
    c = [a, y];
    x1 = diag(rref(c));
    x2 = inv(a) * y;
    x3 = a\y;
    ex1 = norm(x - x1);
    ex2 = norm(x - x2);
    ex3 = norm(x - x3);
    fprintf('Using random matrix: \n \n')
    fprintf('Error using rref function: %f \n', ex1)
    fprintf('Error using inv function: %f \n', ex2)
    fprintf('Error using backslash operator: %f \n', ex3)

    clear a y c x1 x2 x3 ex1 ex2 ex3

    % part 2
    a = hilb(20);
    y = a*x;
    c = [a, y];
    x1 = diag(rref(c));
    x2 = inv(a) * y;
    x3 = a\y;
    ex1 = norm(x - x1);
    ex2 = norm(x - x2);
    ex3 = norm(x - x3);
    fprintf('\n Using hilbert matrix: \n \n')
    fprintf('Error using rref function: %f \n', ex1)
    fprintf('Error using inv functoin: %f \n', ex2)
    fprintf('Error using backslash operator: %f \n', ex3)

    Output:

    Using random matrix:

    Error using rref function: 2.722469
    Error using inv function: 0.000000
    Error using backslash operator: 0.000000
    Warning: Matrix is close to singular or badly scaled. Results
    may be inaccurate. RCOND = 1.155429e-19.
    > In hw6 at 30
    Warning: Matrix is close to singular or badly scaled. Results
    may be inaccurate. RCOND = 1.155429e-19.
    > In hw6 at 31

    Using hilbert matrix:

    Error using rref function: 2.745181
    Error using inv functoin: 134.434224
    Error using backslash operator: 103.192355
     
  2. jcsd
  3. Mar 2, 2013 #2

    Borek

    User Avatar

    Staff: Mentor

    No idea what it is all about, but... have you tried to initialize random numbers generator?
     
  4. Mar 2, 2013 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    The warnings refer to lines 30 and 31 which are inverting the hilbert matrix, not the random matrix.

    The Hilbert matrix is badly conditioned. In fact the estimates in the your error messages are about 10^10 times too big compared with its true condition number (i.e. it is really a lot worse than Matlab said). http://en.wikipedia.org/wiki/Hilbert_matrix
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook