MATLAB: Want a random matrix which has a negative eigenvalues

MATLABrandom matrices of integers

I want to generate an mxm (here I am showing a 3×3) matrix such that each element is an integer of some reasonable magnitude (let's say between 1 and 10) and who's eigenvalues are all negative. At first I did this to generate a random matix:
C = randi(10,3,3)
eig(C)
I reran this about 40 times and never got three negative eigenvalues. That seems odd. I could get every combination except all three being negative. Wouldn't the odds of that would be low. Is there something I don't understand about "randi" or worse, something I don't undertand about eigenvalues?

Best Answer

Too late to really matter, but lets give this a try. First, if all of your matrix elements are positive integers, then you can never have all negative eigenvalues. (This is not difficult to prove.) So the simplest solution is to set all negative integers for your matrix. Then at least you have a chance.
As for what C'+C does, that just ensures that you have REAL eigenvalues. A symmetic matrix with real elements will have real eigenvalues.
A = -randi(10,3,3);eig(A'+A)
ans =
-28.9713024273796
-5.26725767914827
6.23856010652789
So that gives us real eigenvalues, but some negative, some positive.
Now, you might decide to just keep trying repeatedly to see if SOME matrix will eventually arise, but there is a trivial solution, whereby you can ensure the result ALWAYS on the first pass. (Hint: Gershgorin disks.)
So first create a 3x3 matrix with random off diagonal elements.
A = zeros(3);
A(logical(triu(ones(3),1))) = randi(5,3,1);
A
A =
0 2 1
0 0 4
0 0 0
A = A + A'
A =
0 2 1
2 0 4
1 4 0
It is symmetric, so it will have real eigenvalues. Some will be positive, as shown before. Now, use Gershgorin disks to ensure all eigenvalues wiill be negative and real.
Since ALL elements of A are no larger than 5, then no row sum can possibly be greater than 10. But we can do even better, by using the existing row sums of A, .
A = A - diag(sum(A,2) + randi(5,3,1))
A =
-6 2 1
2 -9 4
1 4 -9
eig(A)
ans =
-13.0766925998031
-7.62276318027942
-3.30054421991745
You can even prove that the matrix as created will always have negative eigenvalues, with no need to try a second time.